| 1 |
<?php |
| 2 |
/** |
| 3 |
* All Posts |
| 4 |
* |
| 5 |
* @package AutomatorWP\Integrations\AutomatorWP\Triggers\All_Posts |
| 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_AutomatorWP_All_Posts extends AutomatorWP_Integration_Trigger { |
| 13 |
|
| 14 |
public $integration = 'automatorwp'; |
| 15 |
public $trigger = 'automatorwp_all_posts'; |
| 16 |
|
| 17 |
/** |
| 18 |
* The field conditions |
| 19 |
* |
| 20 |
* @since 1.0.0 |
| 21 |
* |
| 22 |
* @var array $field_conditions |
| 23 |
*/ |
| 24 |
public $field_conditions = array(); |
| 25 |
|
| 26 |
/** |
| 27 |
* The meta conditions |
| 28 |
* |
| 29 |
* @since 1.0.0 |
| 30 |
* |
| 31 |
* @var array $meta_conditions |
| 32 |
*/ |
| 33 |
public $meta_conditions = array(); |
| 34 |
|
| 35 |
/** |
| 36 |
* Register the trigger |
| 37 |
* |
| 38 |
* @since 1.0.0 |
| 39 |
*/ |
| 40 |
public function register() { |
| 41 |
|
| 42 |
automatorwp_register_trigger( $this->trigger, array( |
| 43 |
'integration' => $this->integration, |
| 44 |
'label' => __( 'Run automation on all posts', 'automatorwp' ), |
| 45 |
'select_option' => '', |
| 46 |
'show_in_selector' => false, |
| 47 |
/* translators: %1$s: Automation title. %2$s: Number of times. */ |
| 48 |
'edit_label' => sprintf( __( 'Run automation on %1$s', 'automatorwp' ), '{all_posts_conditions}' ), |
| 49 |
/* translators: %1$s: Automation title. */ |
| 50 |
'log_label' => sprintf( __( 'Run automation on %1$s', 'automatorwp' ), '{all_posts_conditions}' ), |
| 51 |
'options' => array( |
| 52 |
'all_posts_conditions' => array( |
| 53 |
'default' => __( 'all posts', 'automatorwp' ), |
| 54 |
'fields' => array( |
| 55 |
'field_conditions' => array( |
| 56 |
'name' => __( 'Field Conditions:', 'automatorwp' ), |
| 57 |
'desc' => __( 'Set conditions to filter posts by their fields.', 'automatorwp' ), |
| 58 |
'type' => 'group', |
| 59 |
'classes' => 'automatorwp-fields-table', |
| 60 |
'options' => array( |
| 61 |
'add_button' => __( 'Add condition', 'automatorwp' ), |
| 62 |
'remove_button' => '<span class="dashicons dashicons-no-alt"></span>', |
| 63 |
), |
| 64 |
'fields' => array( |
| 65 |
'operator' => automatorwp_utilities_operator_field(), |
| 66 |
'field' => array( |
| 67 |
'name' => __( 'Field:', 'automatorwp' ), |
| 68 |
'type' => 'select', |
| 69 |
'options_cb' => 'automatorwp_options_cb_post_fields', |
| 70 |
'option_none' => true, |
| 71 |
'option_none_value' => '', |
| 72 |
'option_none_label' => __( 'Choose a field', 'automatorwp' ), |
| 73 |
'default' => '' |
| 74 |
), |
| 75 |
'condition' => automatorwp_utilities_condition_field(), |
| 76 |
'value' => array( |
| 77 |
'name' => __( 'Value:', 'automatorwp' ), |
| 78 |
'type' => 'text', |
| 79 |
'default' => '' |
| 80 |
), |
| 81 |
), |
| 82 |
), |
| 83 |
'meta_conditions' => array( |
| 84 |
'name' => __( 'Meta Conditions:', 'automatorwp' ), |
| 85 |
'desc' => __( 'Set conditions to filter posts by their metas.', 'automatorwp' ), |
| 86 |
'type' => 'group', |
| 87 |
'classes' => 'automatorwp-fields-table', |
| 88 |
'options' => array( |
| 89 |
'add_button' => __( 'Add condition', 'automatorwp' ), |
| 90 |
'remove_button' => '<span class="dashicons dashicons-no-alt"></span>', |
| 91 |
), |
| 92 |
'fields' => array( |
| 93 |
'operator' => automatorwp_utilities_operator_field(), |
| 94 |
'meta_key' => array( |
| 95 |
'name' => __( 'Meta Key:', 'automatorwp' ), |
| 96 |
'type' => 'text', |
| 97 |
'default' => '' |
| 98 |
), |
| 99 |
'condition' => automatorwp_utilities_condition_field(), |
| 100 |
'meta_value' => array( |
| 101 |
'name' => __( 'Meta Value:', 'automatorwp' ), |
| 102 |
'type' => 'text', |
| 103 |
'default' => '' |
| 104 |
), |
| 105 |
), |
| 106 |
), |
| 107 |
) |
| 108 |
), |
| 109 |
), |
| 110 |
'tags' => array_merge( |
| 111 |
automatorwp_utilities_post_tags(), |
| 112 |
automatorwp_utilities_times_tag() |
| 113 |
) |
| 114 |
) ); |
| 115 |
|
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Register the required hooks |
| 120 |
* |
| 121 |
* @since 1.0.0 |
| 122 |
*/ |
| 123 |
public function hooks() { |
| 124 |
|
| 125 |
parent::hooks(); |
| 126 |
|
| 127 |
add_filter( 'automatorwp_get_automation_item_option_replacement', array( $this, 'dynamic_item_option_replacement' ), 10, 5 ); |
| 128 |
add_filter( 'automatorwp_get_all-posts_automation_sql', array( $this, 'get_sql' ), 10, 7 ); |
| 129 |
|
| 130 |
// Override edit item HTML |
| 131 |
add_filter( 'automatorwp_automation_item_edit_html_integration', array( $this, 'override_item_edit_html_integration' ), 10, 4 ); |
| 132 |
add_filter( 'automatorwp_automations_column_triggers_integration', array( $this, 'override_item_edit_html_integration' ), 10, 4 ); |
| 133 |
add_filter( 'automatorwp_get_log_integration_icon_integration', array( $this, 'override_item_edit_html_integration' ), 10, 4 ); |
| 134 |
add_filter( 'automatorwp_automation_item_edit_html_classes', array( $this, 'override_item_edit_html_classes' ), 10, 4 ); |
| 135 |
add_filter( 'automatorwp_automation_item_edit_html_actions', array( $this, 'override_item_edit_html_actions' ), 10, 4 ); |
| 136 |
|
| 137 |
// Times tag, register as "no user trigger" |
| 138 |
add_filter( 'automatorwp_get_trigger_tag_replacement_times_no_user_triggers', array( $this, 'add_type_to_array' ) ); |
| 139 |
add_filter( 'automatorwp_get_trigger_last_completion_log_no_user_types', array( $this, 'add_type_to_array' ) ); |
| 140 |
add_filter( 'automatorwp_get_action_last_completion_log_no_user_types', array( $this, 'add_type_to_array' ) ); |
| 141 |
|
| 142 |
// Extend log object on last completion log |
| 143 |
add_filter( 'automatorwp_get_trigger_last_completion_log_extend_log', array( $this, 'extend_last_completion_log' ), 10, 2 ); |
| 144 |
add_filter( 'automatorwp_get_action_last_completion_log_extend_log', array( $this, 'extend_last_completion_log' ), 10, 2 ); |
| 145 |
|
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Filters the option value for replacement on labels |
| 150 |
* |
| 151 |
* @since 1.0.0 |
| 152 |
* |
| 153 |
* @param string $value The option value |
| 154 |
* @param stdClass $object The trigger/action object |
| 155 |
* @param string $item_type The item type (trigger|action) |
| 156 |
* @param string $option The option name |
| 157 |
* @param string $context The context this function is executed |
| 158 |
* |
| 159 |
* @return string |
| 160 |
*/ |
| 161 |
public function dynamic_item_option_replacement( $value, $object, $item_type, $option, $context ) { |
| 162 |
|
| 163 |
// Bail if not is our option |
| 164 |
if( $option !== 'all_posts_conditions' ) { |
| 165 |
return $value; |
| 166 |
} |
| 167 |
|
| 168 |
ct_setup_table( "automatorwp_{$item_type}s" ); |
| 169 |
|
| 170 |
// Setup vars |
| 171 |
$field_conditions = ct_get_object_meta( $object->id, 'field_conditions', true ); |
| 172 |
$meta_conditions = ct_get_object_meta( $object->id, 'meta_conditions', true ); |
| 173 |
|
| 174 |
$someting_in_fields = false; |
| 175 |
$someting_in_metas = false; |
| 176 |
|
| 177 |
// Check if there is any field condition |
| 178 |
if( is_array( $field_conditions ) ) { |
| 179 |
foreach( $field_conditions as $condition ) { |
| 180 |
if( ! empty( $condition['field'] ) && ! empty( $condition['condition'] ) && ! empty( $condition['value'] ) ) { |
| 181 |
$someting_in_fields = true; |
| 182 |
break; |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
// Check if there is any meta condition |
| 188 |
if( is_array( $meta_conditions ) ) { |
| 189 |
foreach( $meta_conditions as $condition ) { |
| 190 |
if( ! empty( $condition['meta_key'] ) && ! empty( $condition['condition'] ) && ! empty( $condition['meta_value'] ) ) { |
| 191 |
$someting_in_fields = true; |
| 192 |
break; |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
if( $someting_in_fields || $someting_in_metas ) { |
| 198 |
$value = __( 'some posts', 'automatorwp' ); |
| 199 |
} |
| 200 |
|
| 201 |
ct_reset_setup_table(); |
| 202 |
|
| 203 |
return $value; |
| 204 |
|
| 205 |
} |
| 206 |
|
| 207 |
/** |
| 208 |
* Available filter to override the all posts automation SQL |
| 209 |
* |
| 210 |
* @since 1.0.0 |
| 211 |
* |
| 212 |
* @param string $sql The SQL query |
| 213 |
* @param stdClass $automation The automation object |
| 214 |
* @param stdClass $trigger The trigger object |
| 215 |
* @param bool $count True if is looking for the SQL to count the number of posts |
| 216 |
* @param array $trigger_options The trigger's stored options |
| 217 |
* @param int $items_per_loop The automation items per loop option |
| 218 |
* @param int $loop The current loop |
| 219 |
*/ |
| 220 |
public function get_sql( $sql, $automation, $trigger, $count, $trigger_options, $items_per_loop, $loop ) { |
| 221 |
|
| 222 |
global $wpdb; |
| 223 |
|
| 224 |
// Setup vars |
| 225 |
$field_conditions = isset( $trigger_options['field_conditions'] ) ? $trigger_options['field_conditions'] : array(); |
| 226 |
$meta_conditions = isset( $trigger_options['meta_conditions'] ) ? $trigger_options['meta_conditions'] : array(); |
| 227 |
$allowed_fields = automatorwp_utilities_get_post_fields( ); |
| 228 |
|
| 229 |
$joins = array(); |
| 230 |
$where = array(); |
| 231 |
|
| 232 |
// Set up the post field conditions |
| 233 |
if( is_array( $field_conditions ) ) { |
| 234 |
|
| 235 |
$where = array( "( 1=1" ); |
| 236 |
|
| 237 |
foreach( $field_conditions as $condition ) { |
| 238 |
|
| 239 |
if( ! isset( $condition['field'] ) ) { |
| 240 |
continue; |
| 241 |
} |
| 242 |
|
| 243 |
// Skip not allowed fields |
| 244 |
if( ! isset( $allowed_fields[$condition['field']] ) ) { |
| 245 |
continue; |
| 246 |
} |
| 247 |
|
| 248 |
// Initialize operator for backward compatibility |
| 249 |
if( ! isset( $condition['operator'] ) ) { |
| 250 |
$condition['operator'] = "AND"; |
| 251 |
} |
| 252 |
|
| 253 |
// Ensure that operator is AND or OR, other values are not allowed |
| 254 |
if( ! in_array( $condition['operator'], array( "AND", "OR" ) ) ) { |
| 255 |
$condition['operator'] = "AND"; |
| 256 |
} |
| 257 |
|
| 258 |
// Replace all tags by their replacements |
| 259 |
$condition = automatorwp_parse_automation_tags( $automation->id, 0, $condition ); |
| 260 |
|
| 261 |
// Sanitize |
| 262 |
$field = sanitize_text_field( $condition['field'] ); |
| 263 |
$value = sanitize_text_field( $condition['value'] ); |
| 264 |
|
| 265 |
if( ! empty( $field ) ) { |
| 266 |
$where[] = $condition['operator'] . " (" . automatorwp_utilities_parse_condition_to_sql( 'p.' . $field, $condition['condition'], $value ) . ")"; |
| 267 |
} |
| 268 |
} |
| 269 |
$where[] = ')'; |
| 270 |
} |
| 271 |
|
| 272 |
// Set up the post meta conditions |
| 273 |
if( is_array( $meta_conditions ) ) { |
| 274 |
foreach( $meta_conditions as $condition ) { |
| 275 |
|
| 276 |
if( ! isset( $condition['meta_key'] ) ) { |
| 277 |
continue; |
| 278 |
} |
| 279 |
|
| 280 |
// Initialize operator for backward compatibility |
| 281 |
if( ! isset( $condition['operator'] ) ) { |
| 282 |
$condition['operator'] = "AND"; |
| 283 |
} |
| 284 |
|
| 285 |
// Ensure that operator is AND or OR, other values are not allowed |
| 286 |
if( ! in_array( $condition['operator'], array( "AND", "OR" ) ) ) { |
| 287 |
$condition['operator'] = "AND"; |
| 288 |
} |
| 289 |
|
| 290 |
// Replace all tags by their replacements |
| 291 |
$condition = automatorwp_parse_automation_tags( $automation->id, 0, $condition ); |
| 292 |
|
| 293 |
// Sanitize |
| 294 |
$meta_key = sanitize_key( $condition['meta_key'] ); |
| 295 |
$meta_key = esc_sql( $meta_key ); |
| 296 |
$meta_value = sanitize_text_field( $condition['meta_value'] ); |
| 297 |
|
| 298 |
if( ! empty( $meta_key ) ) { |
| 299 |
$index = count( $joins ); |
| 300 |
|
| 301 |
$joins[] = "INNER JOIN {$wpdb->postmeta} AS pm{$index} ON ( pm{$index}.post_id = p.ID AND pm{$index}.meta_key = '{$meta_key}' )"; |
| 302 |
|
| 303 |
$where[] = $condition['operator'] . " (" .automatorwp_utilities_parse_condition_to_sql( "pm{$index}.meta_value", $condition['condition'], $meta_value, false ) . ")"; |
| 304 |
} |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
// Turn arrays into strings |
| 309 |
$joins = implode( ' ', $joins ); |
| 310 |
$where = ( ! empty( $where ) ? 'WHERE ' . implode( ' ', $where ) : '' ); |
| 311 |
|
| 312 |
if( $count ) { |
| 313 |
// The count SQL query |
| 314 |
return "SELECT COUNT(*) FROM {$wpdb->posts} AS p {$joins} {$where}"; |
| 315 |
} else { |
| 316 |
$offset = $loop * $items_per_loop; |
| 317 |
|
| 318 |
// The normal SQL query |
| 319 |
return "SELECT p.ID FROM {$wpdb->posts} AS p {$joins} {$where} LIMIT $offset, {$items_per_loop}"; |
| 320 |
} |
| 321 |
|
| 322 |
} |
| 323 |
|
| 324 |
public function override_item_edit_html_integration( $integration, $object, $item_type, $automation ) { |
| 325 |
|
| 326 |
if( $item_type === 'trigger' && $object->type === $this->trigger ) { |
| 327 |
$integration['icon'] = AUTOMATORWP_URL . 'assets/img/automatorwp-all-posts.svg'; |
| 328 |
} |
| 329 |
|
| 330 |
return $integration; |
| 331 |
|
| 332 |
} |
| 333 |
|
| 334 |
public function override_item_edit_html_classes( $classes, $object, $item_type, $automation ) { |
| 335 |
|
| 336 |
if( $automation->type === 'all-posts' && $object->type === $this->trigger ) { |
| 337 |
$classes[] = 'automatorwp-no-grab'; |
| 338 |
} |
| 339 |
|
| 340 |
return $classes; |
| 341 |
|
| 342 |
} |
| 343 |
|
| 344 |
public function override_item_edit_html_actions( $actions, $object, $item_type, $automation ) { |
| 345 |
|
| 346 |
if( $automation->type === 'all-posts' && $object->type === $this->trigger ) { |
| 347 |
unset( $actions['move-up'] ); |
| 348 |
unset( $actions['move-down'] ); |
| 349 |
unset( $actions['delete'] ); |
| 350 |
} |
| 351 |
|
| 352 |
return $actions; |
| 353 |
|
| 354 |
} |
| 355 |
|
| 356 |
public function add_type_to_array( $no_user_triggers ) { |
| 357 |
|
| 358 |
$no_user_triggers[] = $this->trigger; |
| 359 |
|
| 360 |
return $no_user_triggers; |
| 361 |
|
| 362 |
} |
| 363 |
|
| 364 |
public function extend_last_completion_log( $log, $trigger ) { |
| 365 |
|
| 366 |
global $automatorwp_event; |
| 367 |
|
| 368 |
if( $trigger->type === $this->trigger ) { |
| 369 |
|
| 370 |
// Update the post ID for tag parsing |
| 371 |
if( is_array( $automatorwp_event ) && isset( $automatorwp_event['post_id'] ) ) { |
| 372 |
$log->post_id = absint( $automatorwp_event['post_id'] ); |
| 373 |
} |
| 374 |
} |
| 375 |
|
| 376 |
return $log; |
| 377 |
} |
| 378 |
|
| 379 |
} |
| 380 |
|
| 381 |
new AutomatorWP_AutomatorWP_All_Posts(); |