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