Skip to content

Commit

Permalink
Improve logging for Post to Telegram rules (#231)
Browse files Browse the repository at this point in the history
* Improve logging for Post to Telegram rules

* Update Logger.php
  • Loading branch information
irshadahmad21 authored Dec 21, 2024
1 parent c32b2f2 commit cd56942
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 15 deletions.
133 changes: 119 additions & 14 deletions plugins/wptelegram/src/includes/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,11 @@ protected function hookup_for_p2tg() {
add_action( 'wptelegram_p2tg_delay_post', [ $this, 'add_delay_post' ], 999, 3 );

add_filter( 'wptelegram_p2tg_rules_apply', [ $this, 'add_rules_apply' ], 999, 3 );
add_filter( 'wptelegram_p2tg_bypass_post_date_rules', [ $this, 'add_bypass_post_date_rules' ], 999, 2 );
add_filter( 'wptelegram_p2tg_bypass_post_type_rules', [ $this, 'add_bypass_post_type_rules' ], 999, 2 );
add_filter( 'wptelegram_p2tg_rules_send_new_post', [ $this, 'add_send_new_post' ], 999, 2 );
add_filter( 'wptelegram_p2tg_rules_send_existing_post', [ $this, 'add_send_existing_post' ], 999, 2 );
add_filter( 'wptelegram_p2tg_rules_send_post_type', [ $this, 'add_send_post_type' ], 999, 2 );

add_filter( 'wptelegram_p2tg_featured_image_source', [ $this, 'add_featured_image_source' ], 999, 4 );

Expand All @@ -202,7 +207,7 @@ public function get_key( $post ) {
/**
* Get the current request type.
*
* @param WP_Post $post The post being handled.
* @param WP_Post $post The post being handled.
*/
private function get_request_type( $post ) {

Expand All @@ -225,6 +230,8 @@ private function get_request_type( $post ) {
* @param string $trigger The source trigger.
*/
public function before_p2tg_log( $result, $post, $trigger ) {
// `is_new_post` is in a static class, so we want to add it only when Sending to Telegram.
add_filter( 'wptelegram_p2tg_is_post_new', [ $this, 'add_is_new_post' ], 999, 3 );

// create a an entry from post ID and its status.
$key = $this->get_key( $post );
Expand Down Expand Up @@ -270,9 +277,9 @@ public function add_sv_check( $validity, $post, $trigger ) {
/**
* Add delay post info.
*
* @param float $delay Delay in posting.
* @param WP_Post $post The post being handled.
* @param array $result The result of delay handler.
* @param float $delay Delay in posting.
* @param WP_Post $post The post being handled.
* @param array $result The result of delay handler.
*/
public function add_delay_post( $delay, $post, $result ) {

Expand All @@ -288,29 +295,127 @@ public function add_delay_post( $delay, $post, $result ) {
/**
* Add rules_apply info.
*
* @param boolean $rules_apply The post being handled.
* @param Options $options The post being handled.
* @param boolean $rules_apply Whether the rules apply.
* @param Options $options Settings.
* @param WP_Post $post The post being handled.
*/
public function add_rules_apply( $rules_apply, $options, $post ) {

// create a an entry from post ID and its status.
$key = $this->get_key( $post );

$this->p2tg_post_info[ $key ]['rules'] = [
'apply' => $rules_apply,
'sent2tg' => get_post_meta( $post->ID, P2TGMain::PREFIX . 'sent2tg', true ),
];
$this->p2tg_post_info[ $key ]['rules']['apply'] = $rules_apply;

return $rules_apply;
}

/**
* Add bypass_post_date_rules info.
*
* @param boolean $bypass_date_rules Whether to bypass date rules.
* @param WP_Post $post The post being handled.
*/
public function add_bypass_post_date_rules( $bypass_date_rules, $post ) {

// create a an entry from post ID and its status.
$key = $this->get_key( $post );

$this->p2tg_post_info[ $key ]['rules']['bypass']['date'] = $bypass_date_rules;

return $bypass_date_rules;
}

/**
* Add is_new_post info.
*
* @param boolean $is_new Whether the post is new.
* @param WP_Post $post The post being handled.
* @param boolean $is_more_than_a_day_old Whether the post is more than a day old.
*/
public function add_is_new_post( $is_new, $post, $is_more_than_a_day_old ) {

// create a an entry from post ID and its status.
$key = $this->get_key( $post );

$this->p2tg_post_info[ $key ]['rules']['is'] = [
'new' => $is_new,
'a_day_old' => $is_more_than_a_day_old,
'sent2tg' => get_post_meta( $post->ID, P2TGMain::PREFIX . 'sent2tg', true ),
];

return $is_new;
}

/**
* Add send_new_post info.
*
* @param boolean $send_new Whether to send new post.
* @param WP_Post $post The post being handled.
*/
public function add_send_new_post( $send_new, $post ) {

// create a an entry from post ID and its status.
$key = $this->get_key( $post );

$this->p2tg_post_info[ $key ]['rules']['send']['new'] = $send_new;

return $send_new;
}

/**
* Add send_existing_post info.
*
* @param boolean $send_existing Whether to send new post.
* @param WP_Post $post The post being handled.
*/
public function add_send_existing_post( $send_existing, $post ) {

// create a an entry from post ID and its status.
$key = $this->get_key( $post );

$this->p2tg_post_info[ $key ]['rules']['send']['existing'] = $send_existing;

return $send_existing;
}

/**
* Add bypass_post_type_rules info.
*
* @param boolean $bypass_post_type_rules Whether to bypass post type rules.
* @param WP_Post $post The post being handled.
*/
public function add_bypass_post_type_rules( $bypass_post_type_rules, $post ) {

// create a an entry from post ID and its status.
$key = $this->get_key( $post );

$this->p2tg_post_info[ $key ]['rules']['bypass']['post_type'] = $bypass_post_type_rules;

return $bypass_post_type_rules;
}

/**
* Add send_post_type info.
*
* @param boolean $send_post_type Whether the post is new.
* @param WP_Post $post The post being handled.
*/
public function add_send_post_type( $send_post_type, $post ) {

// create a an entry from post ID and its status.
$key = $this->get_key( $post );

$this->p2tg_post_info[ $key ]['rules']['send']['post_type'] = $send_post_type;

return $send_post_type;
}

/**
* Add rules_apply info.
*
* @param string $source The featured image source.
* @param WP_Post $post The post being handled.
* @param Options $options The post being handled.
* @param Options $options Settings.
* @param boolean $send_files_by_url The featured image source.
*/
public function add_featured_image_source( $source, $post, $options, $send_files_by_url ) {
Expand All @@ -334,7 +439,7 @@ public function add_featured_image_source( $source, $post, $options, $send_files
* @param WP_Post $post The post being handled.
* @param string $trigger The source trigger.
* @param boolean $ok The featured image source.
* @param Options $options The post being handled.
* @param Options $options Settings.
* @param array $processed_posts The featured image source.
*/
public function add_post_finish( $post, $trigger, $ok, $options, $processed_posts ) {
Expand Down Expand Up @@ -394,8 +499,8 @@ function ( $response ) {
/**
* Handle the debug action.
*
* @param Response $response The API response.
* @param API $tg_api The post being handled.
* @param Response $response The API response.
* @param API $tg_api The post being handled.
*/
public function add_bot_api_debug( $response, $tg_api ) {

Expand Down
2 changes: 1 addition & 1 deletion plugins/wptelegram/src/modules/p2tg/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static function is_post_new( $post ) {
// if the meta value is empty - it's new.
$is_new = empty( $sent2tg ) && ! $is_more_than_a_day_old;

return (bool) apply_filters( 'wptelegram_p2tg_is_post_new', $is_new, $post );
return (bool) apply_filters( 'wptelegram_p2tg_is_post_new', $is_new, $post, $is_more_than_a_day_old );
}

/**
Expand Down

0 comments on commit cd56942

Please sign in to comment.