diff --git a/.changeset/thirty-insects-kneel.md b/.changeset/thirty-insects-kneel.md new file mode 100644 index 00000000..8aaa531b --- /dev/null +++ b/.changeset/thirty-insects-kneel.md @@ -0,0 +1,5 @@ +--- +"wptelegram": patch +--- + +Added the instant Send to Telegram option on the post list page diff --git a/plugins/wptelegram/.wordpress-org/screenshot-14.png b/plugins/wptelegram/.wordpress-org/screenshot-14.png new file mode 100644 index 00000000..3753c794 Binary files /dev/null and b/plugins/wptelegram/.wordpress-org/screenshot-14.png differ diff --git a/plugins/wptelegram/src/modules/p2tg/Admin.php b/plugins/wptelegram/src/modules/p2tg/Admin.php index c5dd7567..80a623c0 100644 --- a/plugins/wptelegram/src/modules/p2tg/Admin.php +++ b/plugins/wptelegram/src/modules/p2tg/Admin.php @@ -29,6 +29,10 @@ class Admin extends BaseClass { const OVERRIDE_METABOX_ID = 'wptelegram_p2tg_override'; + const INSTANT_POST_ACTION = 'wptelegram_p2tg_instant_post'; + + const INSTANT_POST_NONCE_PREFIX = 'wptelegram_p2tg_instant_post_'; + /** * Saved Settings/Options e.g. in meta * @@ -644,4 +648,73 @@ public function get_override_meta_box_screens() { return (array) apply_filters( 'wptelegram_p2tg_override_meta_box_screens', $screens ); } + + /** + * Add instant send to Telegram action to posts. + * + * @since x.y.z + * + * @param array $actions Array of actions. + * @param WP_Post $post Post object. + * @return array + */ + public function add_instant_post_action( $actions, $post ) { + + $enable_instant_posting = apply_filters( 'wptelegram_p2tg_enable_instant_posting', true, $post, $actions ); + + if ( ! $enable_instant_posting ) { + return $actions; + } + + global $typenow; + $post_types = $this->get_override_meta_box_screens(); + + if ( in_array( $typenow, $post_types, true ) && current_user_can( 'edit_posts' ) && Utils::is_status_of_type( $post, 'live' ) ) { + + $link = add_query_arg( + [ + 'action' => self::INSTANT_POST_ACTION, + 'post' => $post->ID, + + ], + admin_url( 'admin.php' ) + ); + + $link = wp_nonce_url( $link, self::INSTANT_POST_NONCE_PREFIX . $post->ID ); + + $actions[ self::INSTANT_POST_ACTION ] = sprintf( + '%2$s', + $link, + esc_html__( 'Send to Telegram', 'wptelegram' ) + ); + } + + return $actions; + } + + /** + * Handle the instant post action. + */ + public function handle_instant_post() { + if ( empty( $_REQUEST['post'] ) ) { + wp_die( esc_html__( 'No post provided to share!', 'wptelegram' ), 400 ); + } + + $post_id = isset( $_REQUEST['post'] ) ? absint( $_REQUEST['post'] ) : ''; + + check_admin_referer( self::INSTANT_POST_NONCE_PREFIX . $post_id ); + + $post = get_post( $post_id ); + + if ( ! $post ) { + /* translators: %s: post id */ + wp_die( sprintf( esc_html__( 'Sharing failed, could not find the post with ID: %d', 'wptelegram' ), (int) $post_id ) ); + } + + wptelegram_p2tg_send_post( $post, 'instant', true ); + + // Redirect to the post list screen. + wp_safe_redirect( admin_url( 'edit.php?post_type=' . $post->post_type ) ); + exit; + } } diff --git a/plugins/wptelegram/src/modules/p2tg/Main.php b/plugins/wptelegram/src/modules/p2tg/Main.php index 204880c3..b6869574 100644 --- a/plugins/wptelegram/src/modules/p2tg/Main.php +++ b/plugins/wptelegram/src/modules/p2tg/Main.php @@ -63,6 +63,12 @@ protected function define_on_active_hooks() { add_action( 'rest_api_init', [ $admin, 'hook_into_rest_pre_insert' ] ); + // Instant post action. + $action = Admin::INSTANT_POST_ACTION; + add_action( "admin_action_{$action}", [ $admin, 'handle_instant_post' ] ); + + add_filter( 'post_row_actions', [ $admin, 'add_instant_post_action' ], 10, 2 ); + $post_sender = PostSender::instance(); add_action( 'wp_insert_post', [ $post_sender, 'wp_insert_post' ], 20, 2 ); diff --git a/plugins/wptelegram/src/modules/p2tg/PostSender.php b/plugins/wptelegram/src/modules/p2tg/PostSender.php index e0620d4f..06ae4e90 100644 --- a/plugins/wptelegram/src/modules/p2tg/PostSender.php +++ b/plugins/wptelegram/src/modules/p2tg/PostSender.php @@ -497,20 +497,20 @@ public function send_the_post( $post, $trigger, $force ) { } } - if ( 'no' === $this->form_data['send2tg'] && $this->is_valid_status() ) { + if ( 'no' === $this->form_data['send2tg'] && Utils::is_valid_status( $this->post ) ) { $this->clear_scheduled_hook(); $result .= ':' . __LINE__; } // if some rules should be bypassed. - if ( $ok && 'non_wp' === $trigger ) { + if ( $ok && in_array( $trigger, [ 'non_wp', 'instant' ], true ) ) { $this->bypass_rules( $force ); } $rules_apply = $ok && $this->rules_apply(); - if ( $this->is_status_of_type( 'non_live' ) || ( $ok && ( $delay = $this->delay_in_posting( $trigger ) ) ) ) { //phpcs:ignore + if ( Utils::is_status_of_type( $this->post, 'non_live' ) || ( $ok && ( $delay = $this->delay_in_posting( $trigger ) ) ) ) { //phpcs:ignore $this->may_be_save_options(); @@ -547,52 +547,6 @@ public function send_the_post( $post, $trigger, $force ) { return $result; } - /** - * The post statuses that are valid/allowed. - * - * @since 2.1.2 - */ - public function get_valid_post_statuses() { - $valid_statuses = [ - 'live' => [ // The ones that are live/visible. - 'publish', - 'private', - ], - 'non_live' => [ // The that are not yet live for the audience. - 'future', - 'draft', - 'pending', - ], - ]; - return (array) apply_filters( 'wptelegram_p2tg_valid_post_statuses', $valid_statuses, $this->post ); - } - - /** - * If it's a valid status that the should be handled. - * - * @since 2.0.11 - */ - public function is_valid_status() { - - $valid_statuses = call_user_func_array( 'array_merge', array_values( $this->get_valid_post_statuses() ) ); - - return in_array( $this->post->post_status, $valid_statuses, true ); - } - - /** - * If it's a live/non_live status. - * - * @param string $type The status type. - * - * @since 2.1.2 - */ - public function is_status_of_type( $type ) { - - $valid_statuses = $this->get_valid_post_statuses(); - - return in_array( $this->post->post_status, $valid_statuses[ $type ], true ); - } - /** * Clear an existing scheduled event. * @@ -649,8 +603,8 @@ public function delay_post( $delay ) { */ public function delay_in_posting( $trigger = '' ) { - // avoid infinite loop. - if ( 'delayed_post' === $trigger ) { + // No delay for instant posts or delayed posts. + if ( 'delayed_post' === $trigger || 'instant' === $trigger ) { return 0; } @@ -787,7 +741,7 @@ private function security_and_validity_check() { } // If not a valid status. - if ( ! $this->is_valid_status() ) { + if ( ! Utils::is_valid_status( $this->post ) ) { return __LINE__; } @@ -847,7 +801,7 @@ public function may_be_clean_up() { $is_initial_rest_request = RequestCheck::if_is( RequestCheck::REST_PRE_INSERT, $this->post ); - if ( $this->is_status_of_type( 'live' ) && ! $is_gb_metabox && ! $is_initial_rest_request ) { + if ( Utils::is_status_of_type( $this->post, 'live' ) && ! $is_gb_metabox && ! $is_initial_rest_request ) { delete_post_meta( $this->post->ID, Main::PREFIX . 'options' ); delete_post_meta( $this->post->ID, Main::PREFIX . 'send2tg' ); } diff --git a/plugins/wptelegram/src/modules/p2tg/Utils.php b/plugins/wptelegram/src/modules/p2tg/Utils.php index 9a990974..f04f4e64 100644 --- a/plugins/wptelegram/src/modules/p2tg/Utils.php +++ b/plugins/wptelegram/src/modules/p2tg/Utils.php @@ -54,4 +54,61 @@ public static function is_post_new( $post ) { return (bool) apply_filters( 'wptelegram_p2tg_is_post_new', $is_new, $post ); } + + /** + * The post statuses that are valid/allowed. + * + * @since x.y.z + * + * @param WP_Post $post The post object. + * + * @return array[] + */ + public static function get_valid_post_statuses( $post ) { + $valid_statuses = [ + 'live' => [ // The ones that are live/visible. + 'publish', + 'private', + ], + 'non_live' => [ // The that are not yet live for the audience. + 'future', + 'draft', + 'pending', + ], + ]; + return (array) apply_filters( 'wptelegram_p2tg_valid_post_statuses', $valid_statuses, $post ); + } + + /** + * If it's a valid status that the should be handled. + * + * @since x.y.z + * + * @param WP_Post $post The post object. + * + * @return bool + */ + public static function is_valid_status( $post ) { + + $valid_statuses = call_user_func_array( 'array_merge', array_values( self::get_valid_post_statuses( $post ) ) ); + + return in_array( $post->post_status, $valid_statuses, true ); + } + + /** + * If it's a live/non_live status. + * + * @since x.y.z + * + * @param WP_Post $post The post object. + * @param string $status_type The type of status. + * + * @return bool + */ + public static function is_status_of_type( $post, $status_type ) { + + $valid_statuses = self::get_valid_post_statuses( $post ); + + return in_array( $post->post_status, $valid_statuses[ $status_type ], true ); + } } diff --git a/plugins/wptelegram/src/readme.txt b/plugins/wptelegram/src/readme.txt index fd587526..72900c3d 100644 --- a/plugins/wptelegram/src/readme.txt +++ b/plugins/wptelegram/src/readme.txt @@ -179,6 +179,7 @@ Yes, all you need to do is to setup **Private Notifications** module and use the 11. Advanced Settings 12. Post Edit Page (Classic Editor) 13. Post Edit Page (Block Editor) +14. Instant Send to Telegram == Installation ==