← All changes
|
integrations/automatorwp/actions/run-all-posts-automation.php
+201
-0
5.7.6
→
6.0.4
View file →
| @@ -1,0 +1,201 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Run All Posts Automation | |
| 4 | + * | |
| 5 | + * @package AutomatorWP\Integrations\WordPress\Actions\Run_All_Posts_Automation | |
| 6 | + * @author AutomatorWP <[email protected]>, Ruben Garcia <[email protected]> | |
| 7 | + * @since 1.0.0 | |
| 8 | + */ | |
| 9 | +// Exit if accessed directly | |
| 10 | +if( !defined( 'ABSPATH' ) ) exit; | |
| 11 | + | |
| 12 | +class AutomatorWP_WordPress_Run_All_Posts_Automation extends AutomatorWP_Integration_Action { | |
| 13 | + | |
| 14 | + /** | |
| 15 | + * Initialize the action | |
| 16 | + * | |
| 17 | + * @since 1.0.0 | |
| 18 | + */ | |
| 19 | + public function __construct( $integration ) { | |
| 20 | + | |
| 21 | + $this->integration = $integration; | |
| 22 | + $this->action = $integration . '_run_all_posts_automation'; | |
| 23 | + | |
| 24 | + parent::__construct(); | |
| 25 | + | |
| 26 | + } | |
| 27 | + | |
| 28 | + /** | |
| 29 | + * The action result | |
| 30 | + * | |
| 31 | + * @since 1.0.0 | |
| 32 | + * | |
| 33 | + * @var string $result | |
| 34 | + */ | |
| 35 | + public $result = ''; | |
| 36 | + | |
| 37 | + /** | |
| 38 | + * Register the trigger | |
| 39 | + * | |
| 40 | + * @since 1.0.0 | |
| 41 | + */ | |
| 42 | + public function register() { | |
| 43 | + | |
| 44 | + automatorwp_register_action( $this->action, array( | |
| 45 | + 'integration' => $this->integration, | |
| 46 | + 'label' => __( 'Run all posts automation', 'automatorwp' ), | |
| 47 | + 'select_option' => __( 'Run <strong>all posts</strong> automation', 'automatorwp' ), | |
| 48 | + /* translators: %1$s: Hook name. */ | |
| 49 | + 'edit_label' => sprintf( __( 'Run %1$s automation', 'automatorwp' ), '{automation}' ), | |
| 50 | + /* translators: %1$s: Hook name */ | |
| 51 | + 'log_label' => sprintf( __( 'Run %1$s automation', 'automatorwp' ), '{automation}' ), | |
| 52 | + 'options' => array( | |
| 53 | + 'automation' => automatorwp_utilities_automation_option( array( | |
| 54 | + 'option_none_label' => __( 'any', 'automatorwp' ), | |
| 55 | + 'option_none_value' => 'all-posts', | |
| 56 | + 'default' => 'all-posts', | |
| 57 | + ) ), | |
| 58 | + ), | |
| 59 | + ) ); | |
| 60 | + | |
| 61 | + } | |
| 62 | + | |
| 63 | + /** | |
| 64 | + * Action execution function | |
| 65 | + * | |
| 66 | + * @since 1.0.0 | |
| 67 | + * | |
| 68 | + * @param stdClass $action The action object | |
| 69 | + * @param int $user_id The user ID | |
| 70 | + * @param array $action_options The action's stored options (with tags already passed) | |
| 71 | + * @param stdClass $automation The action's automation object | |
| 72 | + */ | |
| 73 | + public function execute( $action, $user_id, $action_options, $automation ) { | |
| 74 | + | |
| 75 | + // Shorthand | |
| 76 | + $automation_id = absint( $action_options['automation'] ); | |
| 77 | + | |
| 78 | + // Bail if empty automation ID | |
| 79 | + if( $automation_id === 0 || $action_options['automation'] === 'all-posts' ) { | |
| 80 | + $this->result = __( 'No automation selected.', 'automatorwp' ); | |
| 81 | + return; | |
| 82 | + } | |
| 83 | + | |
| 84 | + $automation = automatorwp_get_automation_object( $automation_id ); | |
| 85 | + | |
| 86 | + // Bail if not automation found | |
| 87 | + if( ! $automation ) { | |
| 88 | + $this->result = __( 'Automation not found.', 'automatorwp' ); | |
| 89 | + return; | |
| 90 | + } | |
| 91 | + | |
| 92 | + // Bail if automation is not an all-posts automation | |
| 93 | + if( $automation->type !== 'all-posts' ) { | |
| 94 | + $this->result = __( 'Automation selected is not an all posts automation.', 'automatorwp' ); | |
| 95 | + return; | |
| 96 | + } | |
| 97 | + | |
| 98 | + // Run the automation (automation will get called itself every 60 seconds) | |
| 99 | + $result = automatorwp_run_automation( $automation->id ); | |
| 100 | + | |
| 101 | + if( $result ) { | |
| 102 | + $this->result = __( 'Automation executed successfully.', 'automatorwp' ); | |
| 103 | + } else { | |
| 104 | + $this->result = automatorwp_get_run_automation_error(); | |
| 105 | + } | |
| 106 | + | |
| 107 | + } | |
| 108 | + | |
| 109 | + /** | |
| 110 | + * Register required hooks | |
| 111 | + * | |
| 112 | + * @since 1.0.0 | |
| 113 | + */ | |
| 114 | + public function hooks() { | |
| 115 | + | |
| 116 | + // Get objects query args | |
| 117 | + add_filter( 'automatorwp_ajax_get_objects_query_args', array( $this, 'get_objects_query_args' ), 10, 2 ); | |
| 118 | + | |
| 119 | + // Log meta data | |
| 120 | + add_filter( 'automatorwp_user_completed_action_log_meta', array( $this, 'log_meta' ), 10, 5 ); | |
| 121 | + | |
| 122 | + // Log fields | |
| 123 | + add_filter( 'automatorwp_log_fields', array( $this, 'log_fields' ), 10, 5 ); | |
| 124 | + | |
| 125 | + parent::hooks(); | |
| 126 | + } | |
| 127 | + | |
| 128 | + public function get_objects_query_args( $ct_query_args, $ct_table ) { | |
| 129 | + | |
| 130 | + if( $ct_table->name !== 'automatorwp_automations' ) { | |
| 131 | + return $ct_query_args; | |
| 132 | + } | |
| 133 | + | |
| 134 | + if( isset( $_REQUEST['option_none_value'] ) && $_REQUEST['option_none_value'] === 'all-posts' ) { | |
| 135 | + $ct_query_args['type'] = 'all-posts'; | |
| 136 | + } | |
| 137 | + | |
| 138 | + return $ct_query_args; | |
| 139 | + | |
| 140 | + } | |
| 141 | + | |
| 142 | + /** | |
| 143 | + * Action custom log meta | |
| 144 | + * | |
| 145 | + * @since 1.0.0 | |
| 146 | + * | |
| 147 | + * @param array $log_meta Log meta data | |
| 148 | + * @param stdClass $action The action object | |
| 149 | + * @param int $user_id The user ID | |
| 150 | + * @param array $action_options The action's stored options (with tags already passed) | |
| 151 | + * @param stdClass $automation The action's automation object | |
| 152 | + * | |
| 153 | + * @return array | |
| 154 | + */ | |
| 155 | + public function log_meta( $log_meta, $action, $user_id, $action_options, $automation ) { | |
| 156 | + | |
| 157 | + // Bail if action type don't match this action | |
| 158 | + if( $action->type !== $this->action ) { | |
| 159 | + return $log_meta; | |
| 160 | + } | |
| 161 | + | |
| 162 | + $log_meta['result'] = $this->result; | |
| 163 | + | |
| 164 | + return $log_meta; | |
| 165 | + } | |
| 166 | + | |
| 167 | + /** | |
| 168 | + * Action custom log fields | |
| 169 | + * | |
| 170 | + * @since 1.0.0 | |
| 171 | + * | |
| 172 | + * @param array $log_fields The log fields | |
| 173 | + * @param stdClass $log The log object | |
| 174 | + * @param stdClass $object The trigger/action/automation object attached to the log | |
| 175 | + * | |
| 176 | + * @return array | |
| 177 | + */ | |
| 178 | + public function log_fields( $log_fields, $log, $object ) { | |
| 179 | + | |
| 180 | + // Bail if log is not assigned to an action | |
| 181 | + if( $log->type !== 'action' ) { | |
| 182 | + return $log_fields; | |
| 183 | + } | |
| 184 | + | |
| 185 | + // Bail if action type don't match this action | |
| 186 | + if( $object->type !== $this->action ) { | |
| 187 | + return $log_fields; | |
| 188 | + } | |
| 189 | + | |
| 190 | + $log_fields['result'] = array( | |
| 191 | + 'name' => __( 'Result:', 'automatorwp' ), | |
| 192 | + 'type' => 'text', | |
| 193 | + ); | |
| 194 | + | |
| 195 | + return $log_fields; | |
| 196 | + } | |
| 197 | + | |
| 198 | +} | |
| 199 | + | |
| 200 | +new AutomatorWP_WordPress_Run_All_Posts_Automation( 'automatorwp' ); | |
| 201 | +new AutomatorWP_WordPress_Run_All_Posts_Automation( 'run_now' ); | |