Skip to content

Commit 7fc6b8c

Browse files
Improve excerpt formatting to allow plain text (#65)
* Improve message template sanitization to prevent breaking the markup * Update donate link * Update format-text * Improve excerpt formatting to allow plain text * Prepare for v4.0.9 --------- Co-authored-by: irshadahmad21 <[email protected]>
1 parent 7dc4605 commit 7fc6b8c

File tree

13 files changed

+146
-26
lines changed

13 files changed

+146
-26
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
**Requires at least:** 5.8
66
**Requires PHP:** 7.2
77
**Tested up to:** 6.1.1
8-
**Stable tag:** 4.0.8
8+
**Stable tag:** 4.0.9
99
**License:** GPLv2 or later
10-
**License URI:** [http://www.gnu.org/licenses/gpl-2.0.html](http://www.gnu.org/licenses/gpl-2.0.html)
10+
**License URI:** [http://www.gnu.org/licenses/gpl-2.0.html](http://www.gnu.org/licenses/gpl-2.0.html)
11+
**Donate link:** [wpsocio.com/donate](https://wpsocio.com/donate)
1112

1213
[![Wordpress plugin](https://img.shields.io/wordpress/plugin/v/wptelegram.svg)](https://wordpress.org/plugins/wptelegram/)
1314
[![Wordpress](https://img.shields.io/wordpress/plugin/dt/wptelegram.svg)](https://wordpress.org/plugins/wptelegram/)

changelog.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@ All notable changes to this project are documented in this file.
44

55
## Unreleased
66

7+
## [4.0.9 - 2023-03-23](https://github.com/wpsocio/wptelegram/releases/tag/v4.0.9)
8+
9+
### Enhancements
10+
11+
- Improved message template sanitization to prevent breaking the markup
12+
- Added a new filter `wptelegram_p2tg_post_data_plain_excerpt` to allow sending plain text excerpt
13+
14+
### Bug fixes
15+
16+
- Fixed the links added in parenthesis when "Formatting" is set to "None"
17+
718
## [4.0.8 - 2023-03-12](https://github.com/wpsocio/wptelegram/releases/tag/v4.0.8)
819

920
### Bug fixes

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "wptelegram/core",
3-
"version": "4.0.8",
3+
"version": "4.0.9",
44
"description": "Integrate your WordPress site perfectly with Telegram with full control.",
55
"require-dev": {
66
"wp-coding-standards/wpcs": "*"

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "wptelegram",
33
"title": "WP Telegram",
4-
"version": "4.0.8",
4+
"version": "4.0.9",
55
"description": "Integrate your WordPress site perfectly with Telegram with full control.",
66
"repository": {
77
"type": "git",

src/README.txt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
=== WP Telegram (Auto Post and Notifications) ===
22
Contributors: wpsocio, irshadahmad21
3-
Donate link: https://wpsocio.com
3+
Donate link: https://wpsocio.com/donate
44
Tags: telegram, notifications, posts, channel, group
55
Requires at least: 5.8
66
Requires PHP: 7.2
77
Tested up to: 6.1.1
8-
Stable tag: 4.0.8
8+
Stable tag: 4.0.9
99
License: GPLv2 or later
1010
License URI: http://www.gnu.org/licenses/gpl-2.0.html
1111

@@ -205,6 +205,11 @@ Yes, all you need to do is to setup **Private Notifications** module and use the
205205

206206
== Changelog ==
207207

208+
= 4.0.9 =
209+
- Improved message template sanitization to prevent breaking the markup
210+
- Added a new filter `wptelegram_p2tg_post_data_plain_excerpt` to allow sending plain text excerpt
211+
- Fixed the links added in parenthesis when "Formatting" is set to "None"
212+
208213
= 4.0.8 =
209214
- Fixed the broken hyperlinks in Message Template when using custom field link
210215

src/changelog.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@ All notable changes to this project are documented in this file.
44

55
## Unreleased
66

7+
## [4.0.9 - 2023-03-23](https://github.com/wpsocio/wptelegram/releases/tag/v4.0.9)
8+
9+
### Enhancements
10+
11+
- Improved message template sanitization to prevent breaking the markup
12+
- Added a new filter `wptelegram_p2tg_post_data_plain_excerpt` to allow sending plain text excerpt
13+
14+
### Bug fixes
15+
16+
- Fixed the links added in parenthesis when "Formatting" is set to "None"
17+
718
## [4.0.8 - 2023-03-12](https://github.com/wpsocio/wptelegram/releases/tag/v4.0.8)
819

920
### Bug fixes

src/includes/TemplateGuard.php

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
/**
3+
* Template Guard
4+
*
5+
* @link https://wpsocio.com
6+
* @since 4.0.9
7+
*
8+
* @package WPTelegram\Core
9+
* @subpackage WPTelegram\Core\includes
10+
*/
11+
12+
namespace WPTelegram\Core\includes;
13+
14+
/**
15+
* This class is used to guard the template from being broken
16+
* during sanitization.
17+
*
18+
* @link https://wpsocio.com
19+
* @since 4.0.9
20+
*
21+
* @package WPTelegram\Core
22+
* @subpackage WPTelegram\Core\includes
23+
*/
24+
class TemplateGuard {
25+
26+
/**
27+
* The map of macros to their temporary placeholders.
28+
*
29+
* @var array $macro_map The map of macros to their temporary placeholders.
30+
*/
31+
protected $macro_map = [];
32+
33+
/**
34+
* Safeguard the template macros from being broken by wp_kses().
35+
*
36+
* For example wp_kses() can result in malformed template
37+
* For example,
38+
* <a href="{cf:_field_name}">Click here</a>
39+
* gets converted to
40+
* <a href="_field_name}">Click here</a>
41+
* due to ":" in the href being treated as a part of some protocol.
42+
*
43+
* @since 4.0.9
44+
*
45+
* @param string $template The template to safeguard.
46+
*
47+
* @return string The safeguarded template.
48+
*/
49+
public function safeguard_macros( $template ) {
50+
51+
$this->macro_map = [];
52+
53+
// Match all macros in the template.
54+
if ( preg_match_all( '/\{[^\}]+?\}/iu', $template, $matches ) ) {
55+
56+
$total = count( $matches[0] );
57+
// Replace the macros with temporary placeholders.
58+
for ( $i = 0; $i < $total; $i++ ) {
59+
$this->macro_map[ "##MACRO{$i}##" ] = $matches[0][ $i ];
60+
}
61+
}
62+
63+
// Replace the macros with temporary placeholders.
64+
$safe_template = str_replace( array_values( $this->macro_map ), array_keys( $this->macro_map ), $template );
65+
66+
return $safe_template;
67+
}
68+
69+
/**
70+
* Restore the template macros.
71+
*
72+
* @since 4.0.9
73+
*
74+
* @param string $template The template to restore.
75+
*
76+
* @return string The restored template.
77+
*/
78+
public function restore_macros( $template ) {
79+
80+
// Restore the macros with the original values.
81+
$restored_template = str_replace( array_keys( $this->macro_map ), array_values( $this->macro_map ), $template );
82+
83+
return $restored_template;
84+
}
85+
}

src/includes/Utils.php

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -268,20 +268,18 @@ public static function sanitize_message_template( $value, $addslashes = false, $
268268
if ( is_object( $value ) || is_array( $value ) ) {
269269
return '';
270270
}
271+
$value = (string) $value;
271272

272-
$filtered = wp_check_invalid_utf8( (string) $value );
273+
$guard = new TemplateGuard();
273274

274-
$allowed_protocols = [];
275+
$value = $guard->safeguard_macros( $value );
275276

276-
// If the Message Template contains a link with {cf: field as the href,
277-
// We need to allow "{cf" as a protocol to avoid wp_kses() stripping the link.
278-
if ( preg_match( '/<a[^>]href=["\']{cf:/', $filtered ) ) {
279-
$allowed_protocols = array_merge( wp_allowed_protocols(), [ '{cf' ] );
280-
}
277+
$filtered = wp_check_invalid_utf8( $value );
281278

282-
$allowed_protocols = apply_filters( 'wptelegram_message_template_allowed_protocols', $allowed_protocols, $filtered );
279+
$filtered = trim( wp_kses( $filtered, self::SUPPORTED_HTML_TAGS ) );
283280

284-
$filtered = trim( wp_kses( $filtered, self::SUPPORTED_HTML_TAGS, $allowed_protocols ) );
281+
// Restore the macros with the original values.
282+
$filtered = $guard->restore_macros( $filtered );
285283

286284
if ( $json_encode ) {
287285
// json_encode to avoid errors when saving multi-byte emojis into database with no multi-byte support.
@@ -495,12 +493,13 @@ public static function get_html_converter( $options = [], $id = 'default' ) {
495493
public static function prepare_content( $content, $options = [] ) {
496494

497495
$defaults = [
498-
'elipsis' => '',
499-
'format_to' => 'text',
500-
'id' => 'default',
501-
'limit' => 55,
502-
'limit_by' => 'words',
503-
'preserve_eol' => true,
496+
'elipsis' => '',
497+
'format_to' => 'text',
498+
'id' => 'default',
499+
'limit' => 55,
500+
'limit_by' => 'words',
501+
'text_hyperlinks' => 'strip',
502+
'preserve_eol' => true,
504503
];
505504

506505
$options = wp_parse_args( $options, $defaults );

src/languages/wptelegram.pot

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ msgid ""
44
msgstr ""
55
"Project-Id-Version: \n"
66
"Report-Msgid-Bugs-To: https://github.com/wpsocio/wptelegram\n"
7-
"POT-Creation-Date: 2023-03-12 17:29:13+00:00\n"
7+
"POT-Creation-Date: 2023-03-23 12:50:29+00:00\n"
88
"MIME-Version: 1.0\n"
99
"Content-Type: text/plain; charset=utf-8\n"
1010
"Content-Transfer-Encoding: 8bit\n"
11-
"PO-Revision-Date: 2023-03-12 17:290\n"
11+
"PO-Revision-Date: 2023-03-23 12:500\n"
1212
"Last-Translator: WP Telegram\n"
1313
"Language-Team: WP Telegram\n"
1414
"Language: en_US\n"

0 commit comments

Comments
 (0)