| 1 |
<?php |
| 2 |
/** |
| 3 |
* Generate Hash |
| 4 |
* |
| 5 |
* @package AutomatorWP\Integrations\Generator\Actions\Generate_Hash |
| 6 |
* @author AutomatorWP <contact@automatorwp.com>, Ruben Garcia <rubengcdev@gmail.com> |
| 7 |
* @since 1.0.0 |
| 8 |
*/ |
| 9 |
// Exit if accessed directly |
| 10 |
|
| 11 |
if( !defined( 'ABSPATH' ) ) exit; |
| 12 |
|
| 13 |
class AutomatorWP_Generator_Generate_Hash extends AutomatorWP_Integration_Action { |
| 14 |
|
| 15 |
public $integration = 'generator'; |
| 16 |
public $action = 'generator_generate_hash'; |
| 17 |
|
| 18 |
/** |
| 19 |
* Register the trigger |
| 20 |
* |
| 21 |
* @since 1.0.0 |
| 22 |
*/ |
| 23 |
public function register() { |
| 24 |
|
| 25 |
automatorwp_register_action( $this->action, array( |
| 26 |
'integration' => $this->integration, |
| 27 |
'label' => __( 'Generate a hash', 'automatorwp' ), |
| 28 |
'select_option' => __( 'Generate a <strong>hash</strong>', 'automatorwp' ), |
| 29 |
/* translators: %1$s: Hash algorithm. */ |
| 30 |
'edit_label' => sprintf( __( 'Generate a hash with %1$s', 'automatorwp' ), '{random_hash}' ), |
| 31 |
/* translators: %1$s: Hash algorithm. */ |
| 32 |
'log_label' => sprintf( __( 'Generate a hash with %1$s', 'automatorwp' ), '{random_hash}' ), |
| 33 |
'options' => array( |
| 34 |
'random_hash' => array( |
| 35 |
'from' => 'algorithm', |
| 36 |
'default' => __( 'algorithm', 'automatorwp' ), |
| 37 |
'fields' => array( |
| 38 |
'data' => array( |
| 39 |
'name' => __( 'Data:', 'automatorwp' ), |
| 40 |
'desc' => __( 'Data to be hashed.', 'automatorwp' ), |
| 41 |
'type' => 'text', |
| 42 |
'default' => '' |
| 43 |
), |
| 44 |
'algorithm' => array( |
| 45 |
'name' => __( 'Algorithm:', 'automatorwp' ), |
| 46 |
'type' => 'select', |
| 47 |
'classes' => 'automatorwp-selector', |
| 48 |
'options_cb' => 'automatorwp_generator_get_algorithms', |
| 49 |
'attributes' => array( |
| 50 |
'data-placeholder' => __( 'Select an algorithm', 'automatorwp' ), |
| 51 |
), |
| 52 |
'default' => '', |
| 53 |
), |
| 54 |
) ) |
| 55 |
), |
| 56 |
'tags' => function_exists( 'automatorwp_generator_get_actions_random_hash_tags' ) ? automatorwp_generator_get_actions_random_hash_tags() : array(), |
| 57 |
) ); |
| 58 |
|
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Action execution function |
| 63 |
* |
| 64 |
* @since 1.0.0 |
| 65 |
* |
| 66 |
* @param stdClass $action The action object |
| 67 |
* @param int $user_id The user ID |
| 68 |
* @param array $action_options The action's stored options (with tags already passed) |
| 69 |
* @param stdClass $automation The action's automation object |
| 70 |
*/ |
| 71 |
public function execute( $action, $user_id, $action_options, $automation ) { |
| 72 |
|
| 73 |
$this->result = ''; |
| 74 |
|
| 75 |
// Shorthand |
| 76 |
$data = $action_options['data']; |
| 77 |
$algorithm = $action_options['algorithm']; |
| 78 |
|
| 79 |
// Bail if not data |
| 80 |
if ( empty( $data ) ) { |
| 81 |
$this->result = __( 'Please, insert data to be hashed', 'automatorwp' ); |
| 82 |
return; |
| 83 |
} |
| 84 |
|
| 85 |
// Bail if not algorithm |
| 86 |
if ( empty( $algorithm ) ) { |
| 87 |
$this->result = __( 'Please, select an algorithm', 'automatorwp' ); |
| 88 |
return; |
| 89 |
} |
| 90 |
|
| 91 |
// Get the algorithms |
| 92 |
$all_algorithms = hash_algos(); |
| 93 |
|
| 94 |
$this->random_hash = hash( $all_algorithms[$algorithm], $data ); |
| 95 |
|
| 96 |
$this->result = sprintf( __( '%s', 'automatorwp' ), $this->random_hash ); |
| 97 |
|
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Register required hooks |
| 102 |
* |
| 103 |
* @since 1.0.0 |
| 104 |
*/ |
| 105 |
public function hooks() { |
| 106 |
|
| 107 |
// Configuration notice |
| 108 |
add_filter( 'automatorwp_automation_ui_after_item_label', array( $this, 'configuration_notice' ), 10, 2 ); |
| 109 |
|
| 110 |
// Admin notice |
| 111 |
add_action( 'admin_notices', array( $this, 'admin_notice' ) ); |
| 112 |
|
| 113 |
// Log meta data |
| 114 |
add_filter( 'automatorwp_user_completed_action_log_meta', array( $this, 'log_meta' ), 10, 5 ); |
| 115 |
|
| 116 |
// Log fields |
| 117 |
add_filter( 'automatorwp_log_fields', array( $this, 'log_fields' ), 10, 5 ); |
| 118 |
|
| 119 |
parent::hooks(); |
| 120 |
|
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Configuration notice |
| 125 |
* |
| 126 |
* @since 1.0.0 |
| 127 |
* |
| 128 |
* @param stdClass $object The trigger/action object |
| 129 |
* @param string $item_type The object type (trigger|action) |
| 130 |
*/ |
| 131 |
public function configuration_notice( $object, $item_type ) { |
| 132 |
|
| 133 |
// Bail if action type don't match this action |
| 134 |
if( $item_type !== 'action' ) { |
| 135 |
return; |
| 136 |
} |
| 137 |
|
| 138 |
if( $object->type !== $this->action ) { |
| 139 |
return; |
| 140 |
} |
| 141 |
|
| 142 |
// Warn user if file was deleted |
| 143 |
if( ! function_exists( 'automatorwp_generator_get_actions_random_hash_tags' ) ) : ?> |
| 144 |
<div class="automatorwp-notice-warning" style="margin-top: 10px; margin-bottom: 0;"> |
| 145 |
<?php echo |
| 146 |
__( 'The generate a hash function was not found and may have been accidentally removed by a security plugin on your site.<br>', 'automatorwp' ); ?> |
| 147 |
<?php echo |
| 148 |
__( 'Please report this to the security plugin to add an exception to AutomatorWP and reinstall AutomatorWP to restore the deleted code.', 'automatorwp' ); ?> |
| 149 |
</div> |
| 150 |
<?php endif; |
| 151 |
|
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Configuration notice |
| 156 |
* |
| 157 |
* @since 1.0.0 |
| 158 |
* |
| 159 |
*/ |
| 160 |
public function admin_notice( ) { |
| 161 |
|
| 162 |
// Warn user if file was deleted |
| 163 |
if( ! function_exists( 'automatorwp_generator_get_actions_random_hash_tags' ) ) : ?> |
| 164 |
<div id="message-error" class="notice notice-error is-dismissible"> |
| 165 |
<?php echo |
| 166 |
__( '<strong>AutomatorWP found an issue:</strong><br>', 'automatorwp' ); ?> |
| 167 |
<?php echo |
| 168 |
__( 'The generate a hash function was not found and may have been accidentally removed by a security plugin on your site.<br>', 'automatorwp' ); ?> |
| 169 |
<?php echo |
| 170 |
__( 'Please report this to the security plugin to add an exception to AutomatorWP and reinstall AutomatorWP to restore the deleted code.', 'automatorwp' ); ?> |
| 171 |
</div> |
| 172 |
<?php endif; |
| 173 |
|
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Action custom log meta |
| 178 |
* |
| 179 |
* @since 1.0.0 |
| 180 |
* |
| 181 |
* @param array $log_meta Log meta data |
| 182 |
* @param stdClass $action The action object |
| 183 |
* @param int $user_id The user ID |
| 184 |
* @param array $action_options The action's stored options (with tags already passed) |
| 185 |
* @param stdClass $automation The action's automation object |
| 186 |
* |
| 187 |
* @return array |
| 188 |
*/ |
| 189 |
public function log_meta( $log_meta, $action, $user_id, $action_options, $automation ) { |
| 190 |
|
| 191 |
// Bail if action type don't match this action |
| 192 |
if( $action->type !== $this->action ) { |
| 193 |
return $log_meta; |
| 194 |
} |
| 195 |
|
| 196 |
// Store the action's result |
| 197 |
$log_meta['result'] = $this->result; |
| 198 |
$log_meta['random_hash'] = ( isset( $this->random_hash ) ? $this->random_hash : '' ); |
| 199 |
|
| 200 |
return $log_meta; |
| 201 |
} |
| 202 |
|
| 203 |
/** |
| 204 |
* Action custom log fields |
| 205 |
* |
| 206 |
* @since 1.0.0 |
| 207 |
* |
| 208 |
* @param array $log_fields The log fields |
| 209 |
* @param stdClass $log The log object |
| 210 |
* @param stdClass $object The trigger/action/automation object attached to the log |
| 211 |
* |
| 212 |
* @return array |
| 213 |
*/ |
| 214 |
public function log_fields( $log_fields, $log, $object ) { |
| 215 |
|
| 216 |
// Bail if log is not assigned to an action |
| 217 |
if( $log->type !== 'action' ) { |
| 218 |
return $log_fields; |
| 219 |
} |
| 220 |
|
| 221 |
// Bail if action type don't match this action |
| 222 |
if( $object->type !== $this->action ) { |
| 223 |
return $log_fields; |
| 224 |
} |
| 225 |
|
| 226 |
$log_fields['result'] = array( |
| 227 |
'name' => __( 'Result:', 'automatorwp' ), |
| 228 |
'type' => 'text', |
| 229 |
); |
| 230 |
|
| 231 |
return $log_fields; |
| 232 |
} |
| 233 |
|
| 234 |
} |
| 235 |
|
| 236 |
new AutomatorWP_Generator_Generate_Hash(); |