Changeset 2976439
- Timestamp:
- 10/09/2023 02:20:17 PM (2 years ago)
- Location:
- ringier-bus/trunk
- Files:
-
- 2 added
- 12 edited
-
CHANGELOG.md (modified) (1 diff)
-
README.md (modified) (2 diffs)
-
readme.txt (modified) (3 diffs)
-
ringier-bus.php (modified) (2 diffs)
-
src/Core/AdminSettingsPage.php (modified) (3 diffs)
-
src/Core/Bus/ArticleEvent.php (modified) (6 diffs)
-
src/Core/Bus/BusHelper.php (modified) (22 diffs)
-
src/Core/BusPluginClass.php (modified) (1 diff)
-
src/Core/Enum.php (modified) (1 diff)
-
src/Core/Utils.php (modified) (6 diffs)
-
src/ringier_bus_plugin_helper.php (modified) (4 diffs)
-
views/admin/field_alternate_primary_category_selectbox.twig (added)
-
views/admin/field_alternate_primary_category_textbox.twig (added)
-
views/admin/field_slack_channel_name.twig (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
ringier-bus/trunk/CHANGELOG.md
r2939921 r2976439 1 1 # Changelog Details 2 3 ### 2.2.0 (Oct 9, 2023) ### 4 5 * [NEW] Introduction of the possibility to add a custom Top level primary category - can ENABLE/DISABLED when needed 6 * Addition of two new fields on the Settings page for the below 7 * use-case: when you have several wordpress instance on the same root domain 8 * by default, it will use the full domain as the primary category when enabled, with the flexibility for you to change it on the *settings page* 9 10 * [UPDATE] Refactored the logic for saving custom fields (on gutenberg) to work as soon as the plugin is active, irrespective if the BUS sync is OFF 11 * [FIX] There was a bug that could prevent the primary category of an article from being fetched from the fallback method if the one from Yoast fails 2 12 3 13 ### 2.1.0 (Jul 18, 2023) ### -
ringier-bus/trunk/README.md
r2939921 r2976439 6 6 **Tags:** ringier, bus, api, cde 7 7 **Requires at least:** 6.0 8 **Tested up to:** 6. 2.29 **Stable tag:** 2. 1.08 **Tested up to:** 6.3.1 9 **Stable tag:** 2.2.0 10 10 **Requires PHP:** 8.0.2 11 11 **License:** GPLv2 or later … … 33 33 Event names: ArticleCreated, ArticleUpdated and ArticleDeleted. 34 34 35 The events are scheduled to be sent to the Bus with a 2-minute delay. This is to allow WordPress to process the changes and update custom fields in the database, which is done asynchronously. You can view scheduled events by making use of the plugin "Advanced Cron Manager".35 The **events are scheduled** to be sent to the Bus **within a 1-minute delay**. This is to allow WordPress to process the changes and update custom fields in the database, which is done asynchronously. You can view scheduled events by making use of the plugin "Advanced Cron Manager". 36 36 37 37 The plugin also creates two custom fields, available on the article edition page under "Event Bus". -
ringier-bus/trunk/readme.txt
r2939921 r2976439 3 3 Tags: ringier, bus, api, cde 4 4 Requires at least: 6.0 5 Tested up to: 6. 2.26 Stable tag: 2. 1.05 Tested up to: 6.3.1 6 Stable tag: 2.2.0 7 7 Requires PHP: 8.0.2 8 8 License: GPLv2 or later … … 29 29 Event names: ArticleCreated, ArticleUpdated and ArticleDeleted. 30 30 31 The events are scheduled to be sent to the Bus with a 2-minute delay. This is to allow WordPress to process the changes and update custom fields in the database, which is done asynchronously. You can view scheduled events by making use of the plugin "Advanced Cron Manager".31 The events are scheduled to be sent to the Bus with a 1-minute delay. This is to allow WordPress to process the changes and update custom fields in the database, which is done asynchronously. You can view scheduled events by making use of the plugin "Advanced Cron Manager". 32 32 33 33 The plugin also creates two custom fields, available on the article edition page under "Event Bus". … … 73 73 74 74 == Changelog == 75 76 ### 2.2.0 (Oct 9, 2023) ### 77 * [NEW] Introduction of the possibility to add a custom Top level primary category - can ENABLE/DISABLED when needed | See Changelog.md 78 * [UPDATE] Refactored the logic for saving custom fields (on gutenberg) to work as soon as the plugin is active, irrespective if the BUS sync is OFF 79 * [FIX] There was a bug that could prevent the primary category of an article from being fetched from the fallback method if the one from Yoast fails 75 80 76 81 ### 2.1.0 (Jul 18, 2023) ### -
ringier-bus/trunk/ringier-bus.php
r2939921 r2976439 11 11 * Plugin URI: https://github.com/RingierIMU/mkt-plugin-wordpress-bus 12 12 * Description: A plugin to push events to Ringier CDE via the BUS API whenever an article is created, updated or deleted 13 * Version: 2. 1.013 * Version: 2.2.0 14 14 * Requires at least: 6.0 15 15 * Author: Ringier SA, Wasseem Khayrattee … … 50 50 */ 51 51 define('RINGIER_BUS_DS', DIRECTORY_SEPARATOR); 52 define('RINGIER_BUS_PLUGIN_VERSION', '2. 1.0');52 define('RINGIER_BUS_PLUGIN_VERSION', '2.2.0'); 53 53 define('RINGIER_BUS_PLUGIN_MINIMUM_WP_VERSION', '6.0'); 54 54 define('RINGIER_BUS_PLUGIN_DIR_URL', plugin_dir_url(__FILE__)); //has trailing slash at end -
ringier-bus/trunk/src/Core/AdminSettingsPage.php
r2939921 r2976439 100 100 $this->add_field_validation_publication_reason(); 101 101 $this->add_field_validation_article_lifetime(); 102 $this->add_alternate_primary_category_selectbox(); 103 $this->add_alternate_primary_category_textbox(); 102 104 } 103 105 … … 346 348 'wp_bus_' . Enum::FIELD_SLACK_CHANNEL_NAME, 347 349 // Use $args' label_for to populate the id inside the callback. 348 'Slack Channel Name ',350 'Slack Channel Name (or ID)', 349 351 [self::class, 'field_slack_channel_name_callback'], 350 352 Enum::ADMIN_SETTINGS_MENU_SLUG, … … 554 556 unset($timber); 555 557 } 558 559 /** 560 * FIELD - field_status_alt_primary_category 561 */ 562 public function add_alternate_primary_category_selectbox(): void 563 { 564 add_settings_field( 565 'wp_bus_' . Enum::FIELD_STATUS_ALTERNATE_PRIMARY_CATEGORY, 566 // Use $args' label_for to populate the id inside the callback. 567 'Enable custom Top level Primary category', 568 [self::class, 'field_alt_primary_category_selectbox_callback'], 569 Enum::ADMIN_SETTINGS_MENU_SLUG, 570 Enum::ADMIN_SETTINGS_SECTION_1, 571 [ 572 'label_for' => Enum::FIELD_STATUS_ALTERNATE_PRIMARY_CATEGORY, 573 'class' => 'ringier-bus-row alt-category-field first', 574 'field_custom_data' => Enum::FIELD_STATUS_ALTERNATE_PRIMARY_CATEGORY, 575 ] 576 ); 577 } 578 579 /** 580 * field bus status callback function. 581 * 582 * @param $args 583 */ 584 public static function field_alt_primary_category_selectbox_callback($args) 585 { 586 // Get the value of the setting we've registered with register_setting() 587 $options = get_option(Enum::SETTINGS_PAGE_OPTION_NAME); 588 589 $timber = new Timber(); 590 $field_status_alt_category_tpl = RINGIER_BUS_PLUGIN_VIEWS . 'admin' . RINGIER_BUS_DS . 'field_alternate_primary_category_selectbox.twig'; 591 592 $bus_status_selected_on = $bus_status_selected_off = ''; 593 if (isset($options[$args['label_for']])) { 594 $bus_status_selected_on = selected($options[ $args['label_for'] ], 'on', false); 595 $bus_status_selected_off = selected($options[ $args['label_for'] ], 'off', false); 596 } 597 598 if (file_exists($field_status_alt_category_tpl)) { 599 //handle select box 600 $context['field_bus_status_name'] = Enum::SETTINGS_PAGE_OPTION_NAME . '[' . esc_attr($args['label_for']) . ']'; 601 $context['label_for'] = esc_attr($args['label_for']); 602 $context['field_custom_data'] = esc_attr($args['field_custom_data']); 603 $context['field_custom_data_selected_on'] = esc_attr($bus_status_selected_on); 604 $context['field_custom_data_selected_off'] = esc_attr($bus_status_selected_off); 605 606 $timber::render($field_status_alt_category_tpl, $context); 607 } 608 unset($timber); 609 } 610 611 /** 612 * FIELD - field_text_alt_primary_category 613 */ 614 public function add_alternate_primary_category_textbox(): void 615 { 616 add_settings_field( 617 'wp_bus_' . Enum::FIELD_TEXT_ALTERNATE_PRIMARY_CATEGORY, 618 // Use $args' label_for to populate the id inside the callback. 619 'Primary category', 620 [self::class, 'field_alt_primary_category_textbox_callback'], 621 Enum::ADMIN_SETTINGS_MENU_SLUG, 622 Enum::ADMIN_SETTINGS_SECTION_1, 623 [ 624 'label_for' => Enum::FIELD_TEXT_ALTERNATE_PRIMARY_CATEGORY, 625 'class' => 'ringier-bus-row alt-category-field', 626 'field_custom_data' => Enum::FIELD_TEXT_ALTERNATE_PRIMARY_CATEGORY, 627 ] 628 ); 629 } 630 631 /** 632 * field field_text_alt_primary_category status callback function. 633 * 634 * @param $args 635 */ 636 public static function field_alt_primary_category_textbox_callback($args): void 637 { 638 // Get the value of the setting we've registered with register_setting() 639 $options = get_option(Enum::SETTINGS_PAGE_OPTION_NAME); 640 641 $timber = new Timber(); 642 $field_tpl = RINGIER_BUS_PLUGIN_VIEWS . 'admin' . RINGIER_BUS_DS . 'field_alternate_primary_category_textbox.twig'; 643 644 $field_value = parse_url(get_site_url(), PHP_URL_HOST); 645 if (isset($options[$args['label_for']])) { 646 $field_value = $options[$args['label_for']]; 647 } 648 649 if (file_exists($field_tpl)) { 650 $context['field_name'] = Enum::SETTINGS_PAGE_OPTION_NAME . '[' . esc_attr($args['label_for']) . ']'; 651 $context['label_for'] = esc_attr($args['label_for']); 652 $context['field_custom_data'] = esc_attr($args['field_custom_data']); 653 $context['field_value'] = esc_attr($field_value); 654 655 $timber::render($field_tpl, $context); 656 } 657 unset($timber); 658 } 556 659 } -
ringier-bus/trunk/src/Core/Bus/ArticleEvent.php
r2939921 r2976439 192 192 public function buildArticlePayloadData(int $post_ID, \WP_Post $post): array 193 193 { 194 return[194 $payload_array = [ 195 195 'reference' => "$post_ID", 196 196 'status' => $this->getFieldStatus(), … … 234 234 'culture' => ringier_getLocale(), 235 235 'value' => Utils::truncate(get_the_excerpt($post_ID), 300), 236 ],237 ],238 'body' => [239 [240 'culture' => ringier_getLocale(),241 'value' => Utils::getRawContent($this->fetchArticleContent($post_ID)),242 236 ], 243 237 ], … … 255 249 * (wasseem | 9th Dec 2022) 256 250 */ 257 'categories' => [ 258 $this->getParentCategoryArray($post_ID), 259 ], 251 'categories' => $this->getAllCategoryListArray($post_ID), 260 252 'sailthru_tags' => $this->getSailthruTags($post_ID), 261 253 'sailthru_vars' => $this->getSailthruVars($post_ID), … … 263 255 'publication_reason' => Utils::getPublicationReason($post_ID), 264 256 'primary_media_type' => $this->getPrimaryMediaType($post), 257 'body' => [ 258 [ 259 'culture' => ringier_getLocale(), 260 'value' => Utils::getRawContent($this->fetchArticleContent($post_ID)), 261 ], 262 ], 265 263 ]; 264 265 if ($this->isCustomTopLevelCategoryEnabled() === true) { 266 $payload_array['child_category'] = $this->getBlogParentCategory($post_ID); 267 } 268 269 return $payload_array; 266 270 } 267 271 … … 568 572 } 569 573 570 private function getParentCategoryArray(int $post_ID) 574 /** 575 * Will return the primary category array depending on whether any user defined Top level category was ENABLEBD 576 * 577 * @param int $post_ID 578 * 579 * @throws \Monolog\Handler\MissingExtensionException 580 * 581 * @return array 582 */ 583 private function getParentCategoryArray(int $post_ID): array 584 { 585 if ($this->isCustomTopLevelCategoryEnabled() === true) { 586 return $this->getCustomTopLevelCategory(); 587 } else { 588 return $this->getBlogParentCategory($post_ID); 589 } 590 } 591 592 /** 593 * To check if the custom Top Level category is enabled 594 * This is done on the Settings page on the admin UI 595 * 596 * @return bool 597 */ 598 private function isCustomTopLevelCategoryEnabled(): bool 599 { 600 $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; 604 } 605 606 return false; 607 } 608 609 /** 610 * This is the array for the custom top level category 611 * 612 * @return array 613 */ 614 private function getCustomTopLevelCategory(): array 615 { 616 $options = get_option(Enum::SETTINGS_PAGE_OPTION_NAME); 617 $field_alt_category = $options[Enum::FIELD_TEXT_ALTERNATE_PRIMARY_CATEGORY]; 618 619 return [ 620 'id' => 0, 621 'title' => [ 622 [ 623 'culture' => ringier_getLocale(), 624 'value' => Utils::returnEmptyOnNullorFalse($field_alt_category), 625 ], 626 ], 627 'slug' => [ 628 [ 629 'culture' => ringier_getLocale(), 630 'value' => sanitize_title($field_alt_category), 631 ], 632 ], 633 ]; 634 } 635 636 /** 637 * This is the actual primary category set within WordPress for this blog instance 638 * 639 * @param int $post_ID 640 * 641 * @throws \Monolog\Handler\MissingExtensionException 642 * 643 * @return array 644 */ 645 private function getBlogParentCategory(int $post_ID): array 571 646 { 572 647 return [ … … 588 663 589 664 /** 665 * To get list of all categories 666 * 667 * 668 * 669 * @param int $post_ID 670 * 671 * @throws \Monolog\Handler\MissingExtensionException 672 * 673 * @return array 674 */ 675 private function getAllCategoryListArray(int $post_ID): array 676 { 677 if ($this->isCustomTopLevelCategoryEnabled() === true) { 678 $data_array[] = $this->getCustomTopLevelCategory(); 679 } 680 $data_array[] = $this->getBlogParentCategory($post_ID); 681 682 return $data_array; 683 } 684 685 /** 590 686 * Get Modified Date for post 591 687 * in the format RFC3339 (ISO8601) -
ringier-bus/trunk/src/Core/Bus/BusHelper.php
r2939921 r2976439 8 8 namespace RingierBusPlugin\Bus; 9 9 10 use Exception; 11 use GuzzleHttp\Exception\GuzzleException; 12 use Monolog\Handler\MissingExtensionException; 13 use Psr\Cache\InvalidArgumentException; 10 14 use RingierBusPlugin\Enum; 11 15 use RingierBusPlugin\Utils; 16 use WP_Post; 12 17 13 18 class BusHelper … … 22 27 * @return array 23 28 */ 24 public static function getImageArrayForApi(int $post_ID, string $image_size_name = 'large_rectangle', string $isHero = 'false') 29 public static function getImageArrayForApi(int $post_ID, string $image_size_name = 'large_rectangle', string $isHero = 'false'): array 25 30 { 26 31 $image_id = get_post_thumbnail_id($post_ID); … … 47 52 public static function registerBusApiActions(): void 48 53 { 54 /* 55 * the logic of saving custom fields like publication_reason or lifetime, etc 56 * should work as soon as the plugin is active, irrespective if the BUS sync is OFF 57 */ 58 add_action('save_post', [self::class, 'save_custom_fields'], 10, 3); 59 49 60 $fieldsObject = new Fields(); 50 61 //Register Bus Events ONLY IF it is enabled 51 62 if ($fieldsObject->is_bus_enabled === true) { 52 add_action('save_post', [self::class, 'save_custom_fields'], 10, 3);53 63 add_action('transition_post_status', [self::class, 'cater_for_custom_post'], 10, 3); 54 64 add_action('rest_after_insert_post', [self::class, 'triggerArticleEvent'], 10, 1); 65 add_action('future_to_publish', [self::class, 'cater_for_manually_scheduled_post'], 10, 1); 55 66 add_action('publish_to_trash', [self::class, 'triggerArticleDeletedEvent'], 10, 3); 56 67 add_action(Enum::HOOK_NAME_SCHEDULED_EVENTS, [self::class, 'cronSendToBusScheduled'], 10, 3); … … 62 73 * 63 74 * @param int $post_id 64 * @param \WP_Post $post75 * @param WP_Post $post 65 76 * @param bool $update 66 77 * 67 * @throws \Exception 68 */ 69 public static function save_custom_fields(int $post_id, \WP_Post $post, bool $update) 70 { 71 //bail if a page 78 * @throws Exception 79 */ 80 public static function save_custom_fields(int $post_id, WP_Post $post, bool $update): void 81 { 72 82 if (strcmp($post->post_type, 'page') == 0) { 73 83 return; … … 79 89 return; 80 90 } 81 // Bail if we're working on a draft or trashed item 91 82 92 $wordpress_post_status = $post->post_status; 83 if (in_array($wordpress_post_status, ['auto-draft', ' draft', 'inherit', 'trash'])) {93 if (in_array($wordpress_post_status, ['auto-draft', 'inherit', 'trash'])) { 84 94 return; 85 95 } … … 91 101 } 92 102 93 if (strcmp($wordpress_post_status, 'publish') == 0) { //save only when in publish state, no for drafts..etc 103 //MKTC-1750 - should now allow saving when 'saving draft' and 'scheduled' posting 104 if (in_array($wordpress_post_status, ['publish', 'draft', 'future'])) { 94 105 $post_id = Utils::getParentPostId($post_id); 95 106 … … 100 111 update_post_meta($post_id, Enum::ACF_ARTICLE_LIFETIME_KEY, $article_lifetime_value); 101 112 } else { 102 ringier_errorlogthis('[ error] BUS: article_lifetime field value not in whitelist');113 ringier_errorlogthis('[warning] BUS: article_lifetime field value not in whitelist or was empty'); 103 114 } 104 115 } … … 110 121 update_post_meta($post_id, Enum::FIELD_PUBLICATION_REASON_KEY, $publication_reason_value); 111 122 } else { 112 ringier_errorlogthis('[ error] BUS: publication_reason field value not in whitelist');123 ringier_errorlogthis('[warning] BUS: publication_reason field value not in whitelist or was empty'); 113 124 } 114 125 } … … 123 134 /** 124 135 * To cater for custom post_type only 136 * Triggered by hook: transition_post_status 125 137 * 126 138 * @param string $new_status 127 139 * @param string $old_status 128 * @param \WP_Post $post129 */ 130 public static function cater_for_custom_post(string $new_status, string $old_status, \WP_Post $post)140 * @param WP_Post $post 141 */ 142 public static function cater_for_custom_post(string $new_status, string $old_status, WP_Post $post): void 131 143 { 132 144 //bail if a page … … 151 163 152 164 /** 165 * Triggered by Hook: rest_after_insert_post 166 * 153 167 * This action will be invoked ONLY when a post in being Created/Updated 154 168 * (Codex for save_post: https://developer.wordpress.org/reference/hooks/save_post/) … … 159 173 * hence previously hook `save_post` was of a not flexible way for us. 160 174 * 161 * @param int $post_ID 162 * @param \WP_Post $post 163 * @param bool $update 164 * 165 * @throws \GuzzleHttp\Exception\GuzzleException 175 * @param WP_Post $post 176 * 177 * @throws MissingExtensionException 178 * @throws Exception 166 179 * 167 180 * @author Wasseem Khayrattee <wasseemk@ringier.co.za> … … 169 182 * @github wkhayrattee 170 183 */ 171 public static function triggerArticleEvent( \WP_Post $post)184 public static function triggerArticleEvent(WP_Post $post): void 172 185 { 173 186 $wordpress_post_status = $post->post_status; 187 174 188 //we don't want to trigger the event if it is a draft, only when in public 175 189 if (strcmp($wordpress_post_status, 'publish') == 0) { … … 183 197 * Hope in the future WordPress exposes a better way for us to get this context 184 198 */ 185 if (is_object($post)) { 186 $blogKey = $_ENV[Enum::ENV_BUS_APP_KEY]; 187 if (Utils::isPostNew($post_ID) === true) { 188 $articleTriggerMode = Enum::EVENT_ARTICLE_CREATED; 189 } else { 190 $articleTriggerMode = Enum::EVENT_ARTICLE_EDITED; 191 } 199 $blogKey = $_ENV[Enum::ENV_BUS_APP_KEY]; 200 if (Utils::isPostNew($post_ID) === true) { 201 $articleTriggerMode = Enum::EVENT_ARTICLE_CREATED; 202 } else { 203 $articleTriggerMode = Enum::EVENT_ARTICLE_EDITED; 192 204 } 193 205 /* … … 200 212 201 213 //push to SLACK 202 $message = <<<EOF 203 $blogKey: $articleTriggerMode queued for article (ID: $post_ID) 204 Scheduled to run in the next minute(s) 205 EOF; 206 Utils::l($message); 207 } 208 } 209 210 /** 214 self::pushToSLACK($blogKey, $articleTriggerMode, $post_ID); 215 } 216 } 217 218 /** 219 * Triggered by hook: future_to_publish 220 * 221 * @param WP_Post $post 222 * 223 * @throws MissingExtensionException 224 */ 225 public static function cater_for_manually_scheduled_post(WP_Post $post): void 226 { 227 $wordpress_post_status = $post->post_status; 228 if (strcmp($wordpress_post_status, 'publish') == 0) { 229 $blogKey = $_ENV[Enum::ENV_BUS_APP_KEY]; 230 $post_ID = $post->ID; 231 $post_ID = Utils::getParentPostId($post_ID); 232 $articleTriggerMode = 'ArticleCreated'; 233 self::scheduleSendToBus($articleTriggerMode, $post_ID, 0, 1); 234 self::pushToSLACK($blogKey, $articleTriggerMode, $post_ID); 235 } 236 } 237 238 /** 239 * Triggered by hook: publish_to_trash 240 * 211 241 * This action will be invoked ONLY when a post in being Created/Updated 212 242 * I made use of Transitions 213 243 * (Codex for Status Transitions: https://codex.wordpress.org/Post_Status_Transitions) 214 244 * 215 * @param \WP_Post $post216 * 217 * @throws \GuzzleHttp\Exception\GuzzleException245 * @param WP_Post $post 246 * 247 * @throws GuzzleException|MissingExtensionException|InvalidArgumentException 218 248 * 219 249 * @author Wasseem<wasseemk@ringier.co.za> 220 250 */ 221 public static function triggerArticleDeletedEvent( \WP_Post $post)251 public static function triggerArticleDeletedEvent(WP_Post $post): void 222 252 { 223 253 $post_ID = Utils::getParentPostId($post->ID); … … 236 266 * @param int $countCalled 237 267 * 238 * @throws \GuzzleHttp\Exception\GuzzleException 239 * @throws \Monolog\Handler\MissingExtensionException 268 * @throws GuzzleException 269 * @throws MissingExtensionException 270 * @throws InvalidArgumentException 240 271 * 241 272 * @author Wasseem<wasseemk@ringier.co.za> 242 273 */ 243 public static function cronSendToBusScheduled(string $articleTriggerMode, int $post_ID, int $countCalled) 274 public static function cronSendToBusScheduled(string $articleTriggerMode, int $post_ID, int $countCalled): void 244 275 { 245 276 $blogKey = $_ENV[Enum::ENV_BUS_APP_KEY]; 246 277 $message = <<<EOF 247 $blogKey: Now attempting to execute Queued "Push-to-BUS" for article (ID: $post_ID)..278 $blogKey: Now attempting to execute "Push-to-BUS" for article (ID: $post_ID). 248 279 249 280 NOTE: 250 If no error follows, means push-to-BUS was successful281 If no error follows, means successful push 251 282 (else task will be re-queued) 252 283 EOF; … … 262 293 * @param string $articleTriggerMode 263 294 * @param int $post_ID 264 * @param \WP_Post $post295 * @param WP_Post $post 265 296 * @param int $countCalled to keep track of how many times this function was called by the cron 266 297 * 267 * @throws \GuzzleHttp\Exception\GuzzleException|\Monolog\Handler\MissingExtensionException268 */ 269 public static function sendToBus(string $articleTriggerMode, int $post_ID, \WP_Post $post, int $countCalled = 1): void298 * @throws GuzzleException|MissingExtensionException|InvalidArgumentException 299 */ 300 public static function sendToBus(string $articleTriggerMode, int $post_ID, WP_Post $post, int $countCalled = 1): void 270 301 { 271 302 try { … … 277 308 $articleEvent = new ArticleEvent($authClient); 278 309 310 // Internal to some of the ventures, so not all will have this object, hence the check 279 311 if (class_exists('Brand_settings')) { 280 312 $articleEvent->brandSettings = new \Brand_settings(); … … 286 318 ringier_errorlogthis('[error] A problem with Auth Token'); 287 319 288 throw new \Exception('A problem with Auth Token');320 throw new Exception('A problem with Auth Token'); 289 321 } 290 } catch ( \Exception $exception) {322 } catch (Exception $exception) { 291 323 self::scheduleSendToBus($articleTriggerMode, $post_ID, $countCalled); 292 324 } … … 302 334 * @param mixed $run_after_minutes 303 335 * 304 * @throws \Monolog\Handler\MissingExtensionException305 */ 306 public static function scheduleSendToBus(string $articleTriggerMode, int $post_ID, int $countCalled = 1, mixed $run_after_minutes = false) 336 * @throws MissingExtensionException 337 */ 338 public static function scheduleSendToBus(string $articleTriggerMode, int $post_ID, int $countCalled = 1, mixed $run_after_minutes = false): void 307 339 { 308 340 if ($run_after_minutes === false) { … … 321 353 322 354 /* 323 * timest map of any already scheduled event with SAME args355 * timestamp of any already scheduled event with SAME args 324 356 * - needs to be uniquely identified will return false if not scheduled 325 357 */ 326 358 $alreadyScheduledTimestamp = wp_next_scheduled($hookSendToBus, $args); 327 if ($alreadyScheduledTimestamp === false) { //means first time this cron is being scheduled 328 wp_schedule_single_event($currentTimestampForAction, $hookSendToBus, $args, true); 329 } else { //is not on first time 359 if ($alreadyScheduledTimestamp !== false) { //is not on first time 330 360 //unschedule current 331 361 wp_unschedule_event($alreadyScheduledTimestamp, $hookSendToBus, $args); //we want to remove any pre existing ones … … 333 363 //re-schedule same for another time 334 364 $args = [$articleTriggerMode, $post_ID, ++$countCalled]; //2nd time called, need to increment count 335 wp_schedule_single_event($currentTimestampForAction, $hookSendToBus, $args, true);336 }365 } 366 wp_schedule_single_event($currentTimestampForAction, $hookSendToBus, $args, true); 337 367 338 368 $blogKey = $_ENV[Enum::ENV_BUS_APP_KEY]; 339 369 $message = <<<EOF 340 370 $blogKey: [Queuing] Push-to-BUS for article (ID: $post_ID) has just been queued. 341 And will run in the next ($minutesToRun)mins. .371 And will run in the next ($minutesToRun)mins. 342 372 343 Args: 373 Passed Params (mode, article_id, count_for_time_invoked): 374 344 375 EOF; 345 376 Utils::l($message . print_r($args, true)); //push to SLACK … … 351 382 * @return string 352 383 */ 353 public static function scheduledHookName() 384 public static function scheduledHookName(): string 354 385 { 355 386 return Enum::HOOK_NAME_SCHEDULED_EVENTS; 356 387 } 388 389 /** 390 * @param mixed $blogKey 391 * @param string $articleTriggerMode 392 * @param int $post_ID 393 * 394 * @throws MissingExtensionException 395 */ 396 private static function pushToSLACK(mixed $blogKey, string $articleTriggerMode, int $post_ID): void 397 { 398 $message = <<<EOF 399 $blogKey: [Confirming] $articleTriggerMode queued for article (ID: $post_ID) 400 Scheduled to run in the next minute(s) 401 EOF; 402 Utils::l($message); 403 } 357 404 } -
ringier-bus/trunk/src/Core/BusPluginClass.php
r2939921 r2976439 75 75 76 76 /* 77 * Hide the "Quick Edit" button on Post List screen (wp-admin/edit.php) 78 * Because with Quick Edit button, we can change an article from draft to publish, 79 * bypassing the checklist - which we do not want 80 */ 81 add_action('post_row_actions', [self::class, 'hide_quick_edit_button'], PHP_INT_MAX); 82 83 /* 77 84 * Register Bus API Mechanism 78 85 * Note: commented out because we are now fetching values from the UI (dashboard) itself 79 86 */ 80 87 // BusHelper::load_vars_into_env(); 88 } 89 90 /** 91 * Hook reference: https://developer.wordpress.org/reference/hooks/post_row_actions/ 92 * 93 * @param array $actions 94 * 95 * @return array 96 */ 97 public static function hide_quick_edit_button(array $actions): array 98 { 99 unset($actions['inline hide-if-no-js']); 100 101 return $actions; 81 102 } 82 103 -
ringier-bus/trunk/src/Core/Enum.php
r2939921 r2976439 45 45 const FIELD_VALIDATION_PUBLICATION_REASON = 'field_validation_publication_reason'; 46 46 const FIELD_VALIDATION_ARTICLE_LIFETIME = 'field_validation_article_lifetime'; 47 const FIELD_STATUS_ALTERNATE_PRIMARY_CATEGORY = 'field_status_alt_primary_category'; 48 const FIELD_TEXT_ALTERNATE_PRIMARY_CATEGORY = 'field_text_alt_primary_category'; 47 49 48 50 //ADMIN LOG PAGE -
ringier-bus/trunk/src/Core/Utils.php
r2939921 r2976439 8 8 namespace RingierBusPlugin; 9 9 10 use Monolog\Handler\MissingExtensionException; 10 11 use Ramsey\Uuid\Uuid; 11 12 use Ramsey\Uuid\UuidInterface; … … 77 78 78 79 /** 79 * Fetch the property of a category80 80 * Property is any of the following: 81 81 * WP_Term Object … … 96 96 * @param $property 97 97 * 98 * @return false|mixed 99 */ 100 public static function getPrimaryCategoryProperty($post_id, $property) 98 * @throws MissingExtensionException 99 * 100 * @return mixed 101 */ 102 public static function getPrimaryCategoryProperty($post_id, $property): mixed 101 103 { 102 104 //some post_ids are simply revisions, so make sure we are actually using the parent id … … 114 116 115 117 $categories = get_the_terms($post_id, 'category'); 116 if ((!is_wp_error($ term)) && is_array($categories)) {118 if ((!is_wp_error($categories)) && is_array($categories)) { 117 119 $primaryCategory = $categories[0]; 118 120 119 121 return $primaryCategory->{$property}; 120 122 } 123 124 ringier_errorlogthis('Warning: Could not find a category for article with ID: ' . $post_id); 125 Utils::l('Warning: Could not find a category for article with ID: ' . $post_id); 121 126 122 127 return false; … … 232 237 * @param array $context 233 238 * 234 * @throws \Monolog\Handler\MissingExtensionException235 */ 236 public static function l($message, $logLevel = 'alert', array $context = []): void239 * @throws MissingExtensionException 240 */ 241 public static function l($message, string $logLevel = 'alert', array $context = []): void 237 242 { 238 243 //Enable logging to Slack ONLY IF it was enabled … … 240 245 LoggingHandler::getInstance()->log($logLevel, $message, $context); 241 246 } else { 242 ringier_errorlogthis('[info] - did not to Slack, it is probably OFF');247 ringier_errorlogthis('[info] - did not push to Slack, it is probably OFF'); 243 248 } 244 249 } -
ringier-bus/trunk/src/ringier_bus_plugin_helper.php
r2632675 r2976439 4 4 * 5 5 * @author Wasseem Khayrattee <wasseemk@ringier.co.za> 6 * 6 7 * @github wkhayrattee 7 8 */ … … 19 20 * @throws \Exception 20 21 */ 21 function ringier_infologthis($message) 22 function ringier_infologthis($message): void 22 23 { 23 24 if (isset($_ENV['APP_ENV']) && ($_ENV['APP_ENV'] != 'prod')) { … … 38 39 * @throws \Exception 39 40 */ 40 function ringier_errorlogthis($message) 41 function ringier_errorlogthis($message): void 41 42 { 42 43 $log = new Logger('ringier_bus_plugin_error_log'); … … 54 55 * @return mixed|string 55 56 */ 56 function ringier_getLocale() 57 function ringier_getLocale(): mixed 57 58 { 58 59 if (isset($_ENV[Enum::ENV_BUS_API_LOCALE])) { -
ringier-bus/trunk/views/admin/field_slack_channel_name.twig
r2632675 r2976439 5 5 style="width: 22rem;"> 6 6 <p>You can normally create a Channel on your slack, if not ask help with your Slack Admin</p> 7 <p>Consider using the <strong>channel ID</strong>, so that if you change your channel's name, nothing breaks!</p>
Note: See TracChangeset
for help on using the changeset viewer.