Skip to content

Commit

Permalink
Replace Markdown with better HTML formatting (#51)
Browse files Browse the repository at this point in the history
* Deployed from wpsocio/wp-plugins@e7c9f1c

* Deployed from wpsocio/wp-plugins@fe702e5

* Deployed from wpsocio/wp-plugins@7867585

* Update README.txt

* Update format-text

* Update format-text

* Update bot-api

* Update format-text

* Nuke Html2Text

* HTML converter is no longer experimental

* Fix changelog

* Remove support for Markdown formatting

* Add text formatting utility functions

* Update Post to Telegram to use the text better formatting

* Add upgrade function for the major version to remove Markdown support

* Update bot-api

* Use quoted_printable_decode

* Update PostData.phpClean up

* Allow saving of custom HTML

* Update changelog.md

* Deployed from wpsocio/wp-plugins@18ee8b8

* Update format-text

* Update changelog.md

* Update changelog.md

* Update README.txt

* Update changelog.md

* Update changelog.md

* Prepare for v4.0.0

---------

Co-authored-by: irshadahmad21 <[email protected]>
  • Loading branch information
irshadahmad21 and irshadahmad21 authored Feb 13, 2023
1 parent bc4bce0 commit 106f90a
Show file tree
Hide file tree
Showing 43 changed files with 543 additions and 956 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
**Requires at least:** 5.8
**Requires PHP:** 7.0
**Tested up to:** 6.1.1
**Stable tag:** 3.2.2
**Stable tag:** 4.0.0
**License:** GPLv2 or later
**License URI:** [http://www.gnu.org/licenses/gpl-2.0.html](http://www.gnu.org/licenses/gpl-2.0.html)

Expand Down
19 changes: 17 additions & 2 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,24 @@ All notable changes to this project are documented in this file.

## Unreleased

## [3.2.2 - 2023-01-24](https://github.com/wpsocio/wptelegram/releases/tag/v3.2.2)
## [4.0.0 - 2023-02-13](https://github.com/wpsocio/wptelegram/releases/tag/v4.0.0)

### Enhancements

- Added better support for HTML formatting.
- Added support for <u> and <ins> HTML tags.
- Added support for nested tags. You can now use <b> inside <i> and vice versa.
- Intelligently trim `{post_excerpt}` to preserve the other parts of Message Template.

### Breaking changes
- Removed support for Markdown formatting in favour of better HTML formatting

## [3.2.1 - 2023-01-24](https://github.com/wpsocio/wptelegram/releases/tag/v3.2.1)
### Bug fixes

- Fixed the image not being sent "After the text" when "Send files by URL" is disabled
- Fixed the issue of messages not being sent when the markup is not valid

## [3.2.2 - 2023-01-24](https://github.com/wpsocio/wptelegram/releases/tag/v3.2.2)

### Bug fixes

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wptelegram/core",
"version": "3.2.2",
"version": "4.0.0",
"description": "Integrate your WordPress site perfectly with Telegram with full control.",
"require-dev": {
"wp-coding-standards/wpcs": "*"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "wptelegram",
"title": "WP Telegram",
"version": "3.2.2",
"version": "4.0.0",
"description": "Integrate your WordPress site perfectly with Telegram with full control.",
"repository": {
"type": "git",
Expand Down
130 changes: 11 additions & 119 deletions src/README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ Donate link: https://wpsocio.com
Tags: telegram, notifications, posts, channel, group
Requires at least: 5.8
Tested up to: 6.1.1
Requires PHP: 7.0
Stable tag: 3.2.2
Requires PHP: 7.2
Stable tag: 4.0.0
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html

Expand Down Expand Up @@ -205,126 +205,18 @@ Yes, all you need to do is to setup **Private Notifications** module and use the

== Changelog ==

= 3.2.2 =

= 4.0.0 =
- Added better support for HTML formatting.
- Added support for <u> and <ins> HTML tags.
- Added support for nested tags. You can now use <b> inside <i> and vice versa.
- Intelligently trim `{post_excerpt}` to preserve the other parts of Message Template.
- Removed support for Markdown formatting in favour of better HTML formatting
- Fixed the image not being sent "After the text" when "Send files by URL" is disabled
- Fixed the issue of messages not being sent when the markup is not valid

= 3.2.1 =
= 3.2.2 =
- Fixed nesting error in experimental HTML converter

= 3.2.0 =
- Added the new experimental HTML converter for better formatting of email notifications
- The new can be enabled using `add_filter( 'wptelegram_notify_use_experimental_text', '__return_true' );`

= 3.1.15 =
- Fixed PHP warning when not using message thread ID

= 3.1.14 =
- Added support for sending messages to topics with groups
- Fixed messages not sent when replied-to message is not found

= 3.1.13 =
- Improved the default value for Send to Telegram option on post edit page

= 3.1.12 =
- Fixed PHP warnings in logger

= 3.1.11 =
- Fixed warnings in PHP 8.x

= 3.1.10 =
- Improved logging for better understanding of errors

= 3.1.9 =
- Improved logging options to prevent users from mistakes

= 3.1.8 =
- Fixed PHP error on plugin update
- Fixed Post to Telegram rule search bug

= 3.1.7 =
- Added "Protect content" option to Post to Telegram.

= 3.1.6 =
- Misc bug fixes
- Fixed some typos

= 3.1.5 =
- Added override option for Send Featured Image.

= 3.1.4 =
- Fixed posts not sent when Formatting is None

= 3.1.3 =
- Added `{post_slug}` macro

= 3.1.2 =
- Fixed admin page not shown just after upgrade
- Fixed multiple empty lines in post content and excerpt

= 3.1.1 =
- Fixed file upload for CloudFlare proxy

= 3.1.0 =
- Added CloudFlare Proxy support 🚀
- Added `{post_type}` and `{post_type_label}` macros

= 3.0.11 =
- Fixed WooCommerce REST API products not sent to Telegram

= 3.0.10 =
- Added explicit filter for post edit switch
- Further improved logging for better troubleshooting
- Updated FAQ section
- Fixed the issue of settings not saved due to trailing slash redirects

= 3.0.9 =
- Fixed the upgrade for Post to Telegram rules
- Fixed invalid argument error for failed post updates
- Fixed duplicate posts when using bulk import
- Fixed i18n for block editor override settings
- Fixed the Disable Notification settings not being reflected on post edit page

= 3.0.8 =
- Fixed saving empty channels crashing the settings page
- Fixed "Changes could not be saved" error for old upgrades

= 3.0.7 =
- Fixed the issue of scheduled posts being sent regardless of the overrides.
- Removed "Post edit switch" from post edit page when disabled.
- Minor admin UI fixes.

= 3.0.6 =
- Fixed "Send to Telegram" flag not saved for block editor drafts.

= 3.0.5 =
- Fixed the issue of posts being sent from block editor regardless of the rules.
- Fixed the issue of disabled Test Token button.
- Fixed the issue of delayed posts not sent.
- Fixed the empty rules being saved, preventing posts from being sent to Telegram.

= 3.0.4 =
- Fixed wrong template when using CMB2 override settings.

= 3.0.3 =
- Improved logging for better diagnosis
- Fixed the wrong post data when importing posts
- Fixed the issue caused by upgrade for fresh installations
- Fixed the settings not saved issue
- Fixed the Update failed issue in block editor for drafts
- Fixed the fatal error for old block editor posts

= 3.0.2 =
- Fixed the issue of posts not sent when using WP CLI

= 3.0.1 =
- Fixed the last messed up update

= 3.0.0 =
- Switched to PHP namespaces
- Removed CMB2 dependency
- Improved and changed admin UI to single page
- Added option for inline button URL source
- Added option to change the inline button icon
- Better support for block editor override settings
- Fixed the YouTube links being stripped out from the content.
- Minor fixes.
10 changes: 5 additions & 5 deletions src/assets/asset-manifest.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
{
"files": {
"wptelegram--main.php": "/static/js/wptelegram--main.121b54e4.asset.php",
"wptelegram--main.php": "/static/js/wptelegram--main.102b1f20.asset.php",
"wptelegram--main.css": "/static/css/wptelegram--main.2f40af42.css",
"wptelegram--main.js": "/static/js/wptelegram--main.d9856b74.js",
"wptelegram--main.js": "/static/js/wptelegram--main.27056ac9.js",
"wptelegram--p2tg-classic.php": "/static/js/wptelegram--p2tg-classic.4c47884b.asset.php",
"wptelegram--p2tg-classic.js": "/static/js/wptelegram--p2tg-classic.d04aebaa.js",
"wptelegram--p2tg-gb.php": "/static/js/wptelegram--p2tg-gb.eebd2107.asset.php",
"wptelegram--p2tg-gb.js": "/static/js/wptelegram--p2tg-gb.d8152471.js",
"wptelegram--main.2f40af42.css.map": "/static/css/wptelegram--main.2f40af42.css.map",
"wptelegram--main.d9856b74.js.map": "/static/js/wptelegram--main.d9856b74.js.map",
"wptelegram--main.27056ac9.js.map": "/static/js/wptelegram--main.27056ac9.js.map",
"wptelegram--p2tg-classic.d04aebaa.js.map": "/static/js/wptelegram--p2tg-classic.d04aebaa.js.map",
"wptelegram--p2tg-gb.d8152471.js.map": "/static/js/wptelegram--p2tg-gb.d8152471.js.map"
},
"entrypoints": {
"wptelegram--main": [
"static/js/wptelegram--main.121b54e4.asset.php",
"static/js/wptelegram--main.102b1f20.asset.php",
"static/css/wptelegram--main.2f40af42.css",
"static/js/wptelegram--main.d9856b74.js"
"static/js/wptelegram--main.27056ac9.js"
],
"wptelegram--p2tg-classic": [
"static/js/wptelegram--p2tg-classic.4c47884b.asset.php",
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('jquery', 'react', 'react-dom', 'wp-api-fetch', 'wp-i18n', 'wp-polyfill'), 'version' => '0b046618db2a76f2f73aaa7d483e53ff');
<?php return array('dependencies' => array('jquery', 'react', 'react-dom', 'wp-api-fetch', 'wp-i18n', 'wp-polyfill'), 'version' => '7902b0592163b8d42bc615c855c81f08');
3 changes: 3 additions & 0 deletions src/assets/static/js/wptelegram--main.27056ac9.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/assets/static/js/wptelegram--main.27056ac9.js.map

Large diffs are not rendered by default.

3 changes: 0 additions & 3 deletions src/assets/static/js/wptelegram--main.d9856b74.js

This file was deleted.

1 change: 0 additions & 1 deletion src/assets/static/js/wptelegram--main.d9856b74.js.map

This file was deleted.

19 changes: 17 additions & 2 deletions src/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,24 @@ All notable changes to this project are documented in this file.

## Unreleased

## [3.2.2 - 2023-01-24](https://github.com/wpsocio/wptelegram/releases/tag/v3.2.2)
## [4.0.0 - 2023-02-13](https://github.com/wpsocio/wptelegram/releases/tag/v4.0.0)

### Enhancements

- Added better support for HTML formatting.
- Added support for <u> and <ins> HTML tags.
- Added support for nested tags. You can now use <b> inside <i> and vice versa.
- Intelligently trim `{post_excerpt}` to preserve the other parts of Message Template.

### Breaking changes
- Removed support for Markdown formatting in favour of better HTML formatting

## [3.2.1 - 2023-01-24](https://github.com/wpsocio/wptelegram/releases/tag/v3.2.1)
### Bug fixes

- Fixed the image not being sent "After the text" when "Send files by URL" is disabled
- Fixed the issue of messages not being sent when the markup is not valid

## [3.2.2 - 2023-01-24](https://github.com/wpsocio/wptelegram/releases/tag/v3.2.2)

### Bug fixes

Expand Down
4 changes: 2 additions & 2 deletions src/includes/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

namespace WPTelegram\Core\includes;

use ReflectionClass;
use WPTelegram\BotAPI\Response;
use WPTelegram\BotAPI\API;
use WP_Post;
use WPTelegram\Core\modules\p2tg\RequestCheck;
use WPTelegram\Core\modules\p2tg\Main as P2TGMain;
use ReflectionClass;
use WP_Post;

/**
* WPTelegram_Logger class.
Expand Down
5 changes: 0 additions & 5 deletions src/includes/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,6 @@ private function load_dependencies() {
*/
require_once $this->dir( '/includes/format-text/autoload-wp.php' );

/**
* The library responsible for converting HTML to plain text
*/
require_once $this->dir( '/includes/html2text/html2text.php' );

}

/**
Expand Down
85 changes: 85 additions & 0 deletions src/includes/Upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public function do_upgrade() {
'2.1.9',
'2.2.0',
'3.0.0',
'3.0.8',
'4.0.0',
];
}

Expand Down Expand Up @@ -581,4 +583,87 @@ protected function upgrade_to_3_0_8() {
WPTG()->options()->set( 'advanced', $advanced );
}
}

/**
* Upgrade to 4.0.0
*
* - Change parse_mode from Markdown to HTML.
*
* @since 4.0.0
*/
protected function upgrade_to_4_0_0() {
$sections = [ 'p2tg', 'notify' ];

$markdown_v1_to_html_map = [
'*' => 'b',
'_' => 'i',
'```' => 'pre',
'`' => 'code',
];

foreach ( $sections as $section ) {
$options = WPTG()->options()->get( $section );

if ( isset( $options['parse_mode'] ) && 'Markdown' === $options['parse_mode'] ) {

$template = $options['message_template'];

// Escape the HTML chars.
$template = htmlspecialchars( $template, ENT_NOQUOTES, 'UTF-8' );

$macro_map = [];

if ( preg_match_all( '/\{[^\}]+?\}/iu', $template, $matches ) ) {

$total = count( $matches[0] );
// Replace the macros with temporary placeholders.
// This is to prevent the markdown chars in macros from being replaced.
// For example, if the macro is {post_title}, "_" will get replaced with "<i>". This is not desired.
for ( $i = 0; $i < $total; $i++ ) {
$macro_map[ "{:MACRO{$i}:}" ] = $matches[0][ $i ];
}
}

// Replace the macros with temporary placeholders.
$template = str_replace( array_values( $macro_map ), array_keys( $macro_map ), $template );

// Convert links to html.
$template = preg_replace( '/\[([^\]]+?)\]\(([^\)]+?)\)/ui', '<a href="${2}">${1}</a>', $template );

foreach ( $markdown_v1_to_html_map as $char => $tag ) {
if ( false === strpos( $template, $char ) ) {
continue;
}
$placeholder = '{:' . $tag . ':}';
// Replace the escaped chars with temporary placeholders.
$template = str_replace( '\\' . $char, $placeholder, $template );

$regex_char = preg_quote( $char, '/' );

// Create a regex pattern to match the chars.
// The pattern is like: /_([^_]+?)_/ius and replaces it with <i>${1}</i>.
$pattern = sprintf( '/%1$s([^%1$s]+?)%1$s/ius', $regex_char );
// Replace the markdown v1 chars with html.
$replace = sprintf( '<%1$s>${1}</%1$s>', $tag );

$template = preg_replace( $pattern, $replace, $template );
// Replace the temporary placeholders with the chars.
$template = str_replace( $placeholder, $char, $template );
}

// Replace the macros with the original values.
$template = str_replace( array_keys( $macro_map ), array_values( $macro_map ), $template );

$template = stripslashes( $template );

// Update the message template.
$options['message_template'] = $template;
// Set the parse mode to HTML.
$options['parse_mode'] = 'HTML';

// Update the options.
WPTG()->options()->set( $section, $options );
}
}
}
}
Loading

0 comments on commit 106f90a

Please sign in to comment.