Skip to content

Commit

Permalink
WP Telegram | Add the instant Send to Telegram option on the post lis…
Browse files Browse the repository at this point in the history
…t page
  • Loading branch information
irshadahmad21 committed Dec 16, 2024
1 parent c4d49c7 commit fd92f0a
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 53 deletions.
5 changes: 5 additions & 0 deletions .changeset/thirty-insects-kneel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wptelegram": patch
---

Added the instant Send to Telegram option on the post list page
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
73 changes: 73 additions & 0 deletions plugins/wptelegram/src/modules/p2tg/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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(
'<a href="%1$s" >%2$s</a>',
$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;
}
}
6 changes: 6 additions & 0 deletions plugins/wptelegram/src/modules/p2tg/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
60 changes: 7 additions & 53 deletions plugins/wptelegram/src/modules/p2tg/PostSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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__;
}

Expand Down Expand Up @@ -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' );
}
Expand Down
57 changes: 57 additions & 0 deletions plugins/wptelegram/src/modules/p2tg/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
}
1 change: 1 addition & 0 deletions plugins/wptelegram/src/readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 ==

Expand Down

0 comments on commit fd92f0a

Please sign in to comment.