Skip to content

Commit

Permalink
Add multi-group support for legacy widget via custom code (#206)
Browse files Browse the repository at this point in the history
  • Loading branch information
irshadahmad21 authored Nov 21, 2024
1 parent c29c6b5 commit 8a05b5d
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 28 deletions.
5 changes: 5 additions & 0 deletions .changeset/nervous-seahorses-allow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wptelegram-widget": patch
---

Added multi-group support for legacy widget via custom code
1 change: 0 additions & 1 deletion plugins/wptelegram-widget/js/blocks/single-post/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ registerBlockType('wptelegram/widget-single-post', {
return (
<div
className={`wp-block-wptelegram-widget-single-post wptelegram-widget-message align${alignment}`}
// {...useBlockProps.save()}
>
<iframe title={__('Telegram post')} src={iframe_src}>
Your Browser Does Not Support iframes!
Expand Down
53 changes: 34 additions & 19 deletions plugins/wptelegram-widget/src/admin/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,10 @@ private function handle_updates( $updates ) {

foreach ( (array) $updates as $update ) {

$message_id = $this->process_update( $update );
list($message_id, $username) = $this->process_update( $update );

if ( $message_id ) {
$new_messages[] = $message_id;
$new_messages[ $username ][] = $message_id;
}
}

Expand All @@ -257,9 +257,11 @@ private function handle_updates( $updates ) {
$this->plugin()->options()->set( 'last_update_id', $update_id );

if ( ! empty( $new_messages ) ) {
$username = $this->plugin()->options()->get_path( 'legacy_widget.username', '' );

$this->save_messages( $new_messages, $username );
foreach ( $new_messages as $username => $messages ) {
if ( ! empty( $messages ) ) {
$this->save_messages( $messages, $username );
}
}
}

/**
Expand All @@ -273,6 +275,8 @@ private function handle_updates( $updates ) {
*
* @param array $update Update object.
*
* @return array|bool
*
* @since 1.3.0
*/
private function process_update( $update ) {
Expand All @@ -285,23 +289,23 @@ private function process_update( $update ) {
$update_type = $this->get_update_type( $update );

if ( ! $update_type ) {
return false;
return [ 0, '' ];
}

$message = $update[ $update_type ];

$verified = $this->verify_username( $message );
$username = $this->get_verified_username( $message );

if ( ! $verified ) {
return false;
if ( ! $username ) {
return [ 0, '' ];
}

/**
* Fires after doing everything
*/
do_action( 'wptelegram_widget_process_update_finish', $update, $message, $verified );
do_action( 'wptelegram_widget_process_update_finish', $update, $message, $username );

return $message['message_id'];
return [ $message['message_id'], $username ];
}

/**
Expand All @@ -328,6 +332,22 @@ private function get_update_type( $update ) {
return apply_filters( 'wptelegram_widget_update_type', $update_type, $update );
}

/**
* Get the verified username.
*
* @since x.y.z
*
* @param array $message The message object.
*
* @return string
*/
public function get_verified_username( $message ) {

$verified_username = $this->verify_username( $message ) ? $message['chat']['username'] : '';

return apply_filters( 'wptelegram_widget_verified_username', $verified_username, $message );
}

/**
* Verify that the update if from the saved channel.
* Verify by comparing username.
Expand Down Expand Up @@ -415,19 +435,14 @@ public function save_messages_sent_by_p2tg( $res, $responses, $post, $options, $

$result = $res->get_result();

if ( empty( $result['chat']['username'] ) ) {
return;
}

$used_username = strtolower( $result['chat']['username'] );
$saved_username = strtolower( $this->plugin()->options()->get_path( 'legacy_widget.username' ) );
$verified_username = $this->get_verified_username( $result );

if ( $used_username !== $saved_username ) {
if ( ! $verified_username ) {
return;
}

$messages = [ $result['message_id'] ];

$this->save_messages( $messages, $saved_username );
$this->save_messages( $messages, $verified_username );
}
}
20 changes: 12 additions & 8 deletions plugins/wptelegram-widget/src/shared/shortcodes/LegacyWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,13 @@ public static function render( $atts ) {
$atts['width'] = $atts['widget_width'];
}

// fetch messages.
$messages = WPTG_Widget()->options()->get( 'messages', [] );
$username = strtolower( WPTG_Widget()->options()->get_path( 'legacy_widget.username', '' ) );

if ( empty( $messages[ $username ] ) ) {
return;
}

$defaults = [
'num_messages' => 5,
'width' => 100,
'author_photo' => 'auto',
'username' => $username,
];

// use global options.
Expand All @@ -58,6 +53,15 @@ public static function render( $atts ) {

$args = array_map( 'sanitize_text_field', $args );

// Get messages.
$messages = WPTG_Widget()->options()->get( 'messages', [] );

$username = $args['username'];

if ( empty( $messages[ $username ] ) ) {
return;
}

$num_messages = absint( $args['num_messages'] );

if ( ! $num_messages ) {
Expand Down Expand Up @@ -107,14 +111,14 @@ public static function render( $atts ) {
* if either the child theme or the parent theme have overridden the template.
*/
if ( Utils::is_valid_template( $overridden_template ) ) {
load_template( $overridden_template );
load_template( $overridden_template, false );
}
} else {
/*
* If neither the child nor parent theme have overridden the template,
* we load the template from the 'partials' sub-directory of the directory this file is in.
*/
load_template( __DIR__ . '/../partials/legacy-widget.php' );
load_template( __DIR__ . '/../partials/legacy-widget.php', false );
}
$html = ob_get_clean();
return $html;
Expand Down

0 comments on commit 8a05b5d

Please sign in to comment.