Plugin Directory

Changeset 2980105


Ignore:
Timestamp:
10/17/2023 12:47:59 PM (2 years ago)
Author:
ringier
Message:

New release v2.2.0 (see CHANGELOG.md)

Location:
ringier-bus/trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • ringier-bus/trunk/CHANGELOG.md

    r2976439 r2980105  
    11# Changelog Details
     2
     3### 2.3.0 (Oct 9, 2023) ###
     4
     5* [UPDATE]: Transitioned from relying on the rest_after_insert_post hook to the more universally available transition_post_status hook.
     6
     7*Reason*: We identified that some blogs were disabling the Gutenberg editor and as a result, not utilizing the new WordPress REST API. This meant that the rest_after_insert_post hook wasn't being triggered for those instances. To ensure consistent and robust post update handling across all blogs, regardless of their editor choice, we've shifted to the transition_post_status hook.
     8
     9*Impact*: This change ensures that our logic remains consistent even in environments where Gutenberg is disabled or the REST API isn't being leveraged.
     10
     11* [UPDATE]: Improved JSON handling and compression for Slack logging
     12  * Ensured safe JSON encoding with error checks
     13  * Utilized gzcompress for payload compression when available to prevent truncation in Slack notifications channel
    214
    315### 2.2.0 (Oct 9, 2023) ###
  • ringier-bus/trunk/README.md

    r2976439 r2980105  
    66**Tags:** ringier, bus, api, cde   
    77**Requires at least:** 6.0 
    8 **Tested up to:** 6.3.1 
    9 **Stable tag:** 2.2.0 
     8**Tested up to:** 6.3.2 
     9**Stable tag:** 2.3.0 
    1010**Requires PHP:** 8.0.2 
    1111**License:** GPLv2 or later 
  • ringier-bus/trunk/readme.txt

    r2976439 r2980105  
    33Tags: ringier, bus, api, cde
    44Requires at least: 6.0
    5 Tested up to: 6.3.1
    6 Stable tag: 2.2.0
     5Tested up to: 6.3.2
     6Stable tag: 2.3.0
    77Requires PHP: 8.0.2
    88License: GPLv2 or later
     
    7474== Changelog ==
    7575
     76### 2.3.0 (Oct 9, 2023) ###
     77* [UPDATE]: Transitioned from relying on the rest_after_insert_post hook to the more universally available transition_post_status hook.
     78
     79*Reason*:
     80We identified that some blogs were disabling the Gutenberg editor and as a result, not utilizing the new WordPress REST API. This meant that the rest_after_insert_post hook wasn't being triggered for those instances. To ensure consistent and robust post update handling across all blogs, regardless of their editor choice, we've shifted to the transition_post_status hook.
     81
     82* [UPDATE]: Improved JSON handling and compression for Slack logging (see Changelog.md)
     83
    7684### 2.2.0 (Oct 9, 2023) ###
    7785* [NEW] Introduction of the possibility to add a custom Top level primary category - can ENABLE/DISABLED when needed | See Changelog.md
  • ringier-bus/trunk/ringier-bus.php

    r2976439 r2980105  
    1111 * Plugin URI: https://github.com/RingierIMU/mkt-plugin-wordpress-bus
    1212 * Description: A plugin to push events to Ringier CDE via the BUS API whenever an article is created, updated or deleted
    13  * Version: 2.2.0
     13 * Version: 2.3.0
    1414 * Requires at least: 6.0
    1515 * Author: Ringier SA, Wasseem Khayrattee
     
    5050 */
    5151define('RINGIER_BUS_DS', DIRECTORY_SEPARATOR);
    52 define('RINGIER_BUS_PLUGIN_VERSION', '2.2.0');
     52define('RINGIER_BUS_PLUGIN_VERSION', '2.3.0');
    5353define('RINGIER_BUS_PLUGIN_MINIMUM_WP_VERSION', '6.0');
    5454define('RINGIER_BUS_PLUGIN_DIR_URL', plugin_dir_url(__FILE__)); //has trailing slash at end
     
    5858define('RINGIER_BUS_PLUGIN_CACHE_DIR', WP_CONTENT_DIR . RINGIER_BUS_DS . 'cache' . RINGIER_BUS_DS);
    5959define('RINGIER_BUS_PLUGIN_ERROR_LOG_FILE', WP_CONTENT_DIR . DIRECTORY_SEPARATOR . 'ringier_bus_plugin_error_log');
     60
     61/**
     62 * define a cache nonce for asset files loaded by this plugin
     63 * on the admin facing UI (Gutenberg)
     64 * The name was chosen since we were already using this within our legacy Ringier repos.
     65 */
     66if (!defined('_S_VERSION')) {
     67    define('_S_VERSION', '2.3.0.001');
     68}
    6069
    6170/**
  • ringier-bus/trunk/src/Core/Bus/ArticleEvent.php

    r2976439 r2980105  
    9898     * @throws \Exception
    9999     */
    100     public function sendToBus(int $post_ID, \WP_Post $post)
     100    public function sendToBus(int $post_ID, \WP_Post $post): void
    101101    {
    102102        /*
     
    126126                $requestBody
    127127            );
    128             $bodyArray = json_decode((string) $response->getBody(), true);
     128            $raw_response = (string) $response->getBody();
     129            $bodyArray = json_decode($raw_response, true);
     130
     131            // To compress string so it is not truncated on Slack
     132            if (json_last_error() !== JSON_ERROR_NONE) {
     133                Utils::l('JSON encoding error: ' . json_last_error_msg());
     134
     135                return;
     136            }
     137            $raw_response = json_encode($raw_response);
     138
     139            if (extension_loaded('zlib')) {
     140                $raw_response = gzcompress($raw_response);
     141            }
     142
     143            $message2 = <<<EOF
     144            The payload seems to have been successfully delivered.
     145            And the FULL json compressed with gzcompress() (to prevent truncation) was:
     146           
     147            EOF;
     148            Utils::l($message2 . base64_encode($raw_response)); //push to SLACK
     149
    129150//            ringier_errorlogthis($bodyArray);
    130151        } catch (\Exception $exception) {
     
    599620    {
    600621        $options = get_option(Enum::SETTINGS_PAGE_OPTION_NAME);
    601         $field_status_alt_category = $options[Enum::FIELD_STATUS_ALTERNATE_PRIMARY_CATEGORY];
    602         if (strcmp($field_status_alt_category, 'on') == 0) {
    603             return true;
     622        // Check if the key exists before accessing its value.
     623        if (isset($options[Enum::FIELD_STATUS_ALTERNATE_PRIMARY_CATEGORY])) {
     624            $field_status_alt_category = $options[Enum::FIELD_STATUS_ALTERNATE_PRIMARY_CATEGORY];
     625            if (strcmp($field_status_alt_category, 'on') == 0) {
     626                return true;
     627            }
    604628        }
    605629
  • ringier-bus/trunk/src/Core/Bus/BusHelper.php

    r2976439 r2980105  
    6161        //Register Bus Events ONLY IF it is enabled
    6262        if ($fieldsObject->is_bus_enabled === true) {
    63             add_action('transition_post_status', [self::class, 'cater_for_custom_post'], 10, 3);
    64             add_action('rest_after_insert_post', [self::class, 'triggerArticleEvent'], 10, 1);
     63//            add_action('transition_post_status', [self::class, 'cater_for_custom_post'], 10, 3);
     64//            add_action('rest_after_insert_post', [self::class, 'triggerArticleEvent'], 10, 1);
     65            add_action('transition_post_status', [self::class, 'trigger_bus_event_on_post_change'], 10, 3);
    6566            add_action('future_to_publish', [self::class, 'cater_for_manually_scheduled_post'], 10, 1);
    6667            add_action('publish_to_trash', [self::class, 'triggerArticleDeletedEvent'], 10, 3);
     
    133134
    134135    /**
     136     * In essence, this method is a merge of cater_for_custom_post() and triggerArticleEvent()
     137     * because the rest_after_insert_post hook does not get triggered in all WordPress contexts,
     138     * such as when updating a post using the Classic Editor (IMO disabled Gutenberg)
     139     * or programmatically without the REST API.
     140     *
     141     * Thus we ensure that the desired actions are consistently executed regardless
     142     * of the editing method or environment.
     143     *
     144     * @param string $new_status
     145     * @param string $old_status
     146     * @param WP_Post $post
     147     *
     148     * @throws MissingExtensionException
     149     */
     150    public static function trigger_bus_event_on_post_change(string $new_status, string $old_status, WP_Post $post): void
     151    {
     152        // Check if it's a page or normal post and return
     153        if ($post->post_type === 'page' || empty($post->post_type)) {
     154            return;
     155        }
     156
     157        // Bail if we're working on a draft or trashed item
     158        if ($new_status == 'auto-draft' || $new_status == 'draft' || $new_status == 'inherit' || $new_status == 'trash') {
     159            return;
     160        }
     161
     162        if ($new_status === 'publish') {
     163            $post_ID = $post->ID;
     164            $post_ID = Utils::getParentPostId($post_ID);
     165
     166            /*
     167             * This conditioning helps us get context if the post is in mode NEW or EDIT
     168             * There is no other way around this as of this date of coding (Apr 2021)
     169             * Hope in the future WordPress exposes a better way for us to get this context
     170             */
     171            $articleTriggerMode = Utils::isPostNew($post_ID) ? Enum::EVENT_ARTICLE_CREATED : Enum::EVENT_ARTICLE_EDITED;
     172
     173            /*
     174             * we will now schedule the event after 1min instead of instantly executing it, because:
     175             * not all meta data of the article are updated correctly when:
     176             *      - article is first created,
     177             *      - when article meta are changed
     178             */
     179            self::scheduleSendToBus($articleTriggerMode, $post_ID, 0, 1);
     180
     181            //push to SLACK
     182            $blogKey = $_ENV[Enum::ENV_BUS_APP_KEY];
     183            self::pushToSLACK($blogKey, $articleTriggerMode, $post_ID);
     184        }
     185    }
     186
     187    /**
     188     * (No more used in favor of trigger_bus_event_on_post_change())
     189     *
    135190     * To cater for custom post_type only
    136191     * Triggered by hook: transition_post_status
     
    163218
    164219    /**
     220     * (No more used in favor of trigger_bus_event_on_post_change())
     221     *
    165222     * Triggered by Hook: rest_after_insert_post
    166223     *
Note: See TracChangeset for help on using the changeset viewer.