| 1 |
<?php |
| 2 |
/** |
| 3 |
* Delete Option |
| 4 |
* |
| 5 |
* @package AutomatorWP\Integrations\WordPress\Actions\Delete_Option |
| 6 |
* @author AutomatorWP <contact@automatorwp.com>, Ruben Garcia <rubengcdev@gmail.com> |
| 7 |
* @since 1.0.0 |
| 8 |
*/ |
| 9 |
// Exit if accessed directly |
| 10 |
if( !defined( 'ABSPATH' ) ) exit; |
| 11 |
|
| 12 |
class AutomatorWP_WordPress_Delete_Option extends AutomatorWP_Integration_Action { |
| 13 |
|
| 14 |
/** |
| 15 |
* Initialize the trigger |
| 16 |
* |
| 17 |
* @since 1.0.0 |
| 18 |
*/ |
| 19 |
public function __construct( $integration ) { |
| 20 |
|
| 21 |
$this->integration = $integration; |
| 22 |
$this->action = $integration . '_delete_option'; |
| 23 |
|
| 24 |
parent::__construct(); |
| 25 |
|
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Register the trigger |
| 30 |
* |
| 31 |
* @since 1.0.0 |
| 32 |
*/ |
| 33 |
public function register() { |
| 34 |
|
| 35 |
automatorwp_register_action( $this->action, array( |
| 36 |
'integration' => $this->integration, |
| 37 |
'label' => __( 'Delete option', 'automatorwp' ), |
| 38 |
'select_option' => __( 'Delete <strong>option</strong>', 'automatorwp' ), |
| 39 |
/* translators: %1$s: Option. */ |
| 40 |
'edit_label' => sprintf( __( 'Delete %1$s', 'automatorwp' ), '{option}' ), |
| 41 |
/* translators: %1$s: Option. */ |
| 42 |
'log_label' => sprintf( __( 'Delete %1$s', 'automatorwp' ), '{option}' ), |
| 43 |
'options' => array( |
| 44 |
'option' => array( |
| 45 |
'from' => 'option', |
| 46 |
/* translators: Refers to meta key */ |
| 47 |
'default' => __( 'option', 'automatorwp' ), |
| 48 |
'fields' => array( |
| 49 |
'option' => array( |
| 50 |
'name' => __( 'Option:', 'automatorwp' ), |
| 51 |
'type' => 'text', |
| 52 |
'default' => '' |
| 53 |
), |
| 54 |
) |
| 55 |
) |
| 56 |
), |
| 57 |
'tags' => array( |
| 58 |
|
| 59 |
) |
| 60 |
) ); |
| 61 |
|
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Register required hooks |
| 66 |
* |
| 67 |
* @since 1.0.0 |
| 68 |
*/ |
| 69 |
public function hooks() { |
| 70 |
|
| 71 |
// Log meta data |
| 72 |
add_filter( 'automatorwp_user_completed_action_log_meta', array( $this, 'log_meta' ), 10, 5 ); |
| 73 |
|
| 74 |
// Log fields |
| 75 |
add_filter( 'automatorwp_log_fields', array( $this, 'log_fields' ), 10, 5 ); |
| 76 |
|
| 77 |
parent::hooks(); |
| 78 |
|
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Action execution function |
| 83 |
* |
| 84 |
* @since 1.0.0 |
| 85 |
* |
| 86 |
* @param stdClass $action The action object |
| 87 |
* @param int $user_id The user ID |
| 88 |
* @param array $action_options The action's stored options (with tags already passed) |
| 89 |
* @param stdClass $automation The action's automation object |
| 90 |
*/ |
| 91 |
public function execute( $action, $user_id, $action_options, $automation ) { |
| 92 |
|
| 93 |
// Shorthand |
| 94 |
$option = sanitize_key( $action_options['option'] ); |
| 95 |
|
| 96 |
// Bail if empty option name |
| 97 |
if( empty( $option ) ) { |
| 98 |
$this->result = __( 'Action not processed. Option name is empty.', 'automatorwp' ); |
| 99 |
return; |
| 100 |
} |
| 101 |
|
| 102 |
// Bail if is a protected option |
| 103 |
if( automatorwp_is_protected_option( $option ) ) { |
| 104 |
$this->result = sprintf( __( 'Action not processed."%s" is a protected option. You can filter "automatorwp_protected_options" to modify this.', 'automatorwp' ), $option ); |
| 105 |
return; |
| 106 |
} |
| 107 |
|
| 108 |
// Delete the user meta value |
| 109 |
delete_option( $option ); |
| 110 |
|
| 111 |
$this->result = sprintf( __( 'Option "%s" has been deleted successfully.', 'automatorwp' ), $option ); |
| 112 |
|
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Action custom log meta |
| 117 |
* |
| 118 |
* @since 1.0.0 |
| 119 |
* |
| 120 |
* @param array $log_meta Log meta data |
| 121 |
* @param stdClass $action The action object |
| 122 |
* @param int $user_id The user ID |
| 123 |
* @param array $action_options The action's stored options (with tags already passed) |
| 124 |
* @param stdClass $automation The action's automation object |
| 125 |
* |
| 126 |
* @return array |
| 127 |
*/ |
| 128 |
public function log_meta( $log_meta, $action, $user_id, $action_options, $automation ) { |
| 129 |
|
| 130 |
// Bail if action type don't match this action |
| 131 |
if( $action->type !== $this->action ) { |
| 132 |
return $log_meta; |
| 133 |
} |
| 134 |
|
| 135 |
$log_meta['result'] = $this->result; |
| 136 |
|
| 137 |
return $log_meta; |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Action custom log fields |
| 142 |
* |
| 143 |
* @since 1.0.0 |
| 144 |
* |
| 145 |
* @param array $log_fields The log fields |
| 146 |
* @param stdClass $log The log object |
| 147 |
* @param stdClass $object The trigger/action/automation object attached to the log |
| 148 |
* |
| 149 |
* @return array |
| 150 |
*/ |
| 151 |
public function log_fields( $log_fields, $log, $object ) { |
| 152 |
|
| 153 |
// Bail if log is not assigned to an action |
| 154 |
if( $log->type !== 'action' || $object->type !== $this->action ) |
| 155 |
return $log_fields; |
| 156 |
|
| 157 |
$log_fields['result'] = array( |
| 158 |
'name' => __( 'Result:', 'automatorwp' ), |
| 159 |
'type' => 'text', |
| 160 |
); |
| 161 |
|
| 162 |
return $log_fields; |
| 163 |
} |
| 164 |
|
| 165 |
} |
| 166 |
|
| 167 |
new AutomatorWP_WordPress_Delete_Option( 'wordpress' ); |