← All changes
|
integrations/wpcode/includes/actions/activate-snippet.php
+156
-0
5.7.3
→
6.0.3
View file →
| @@ -1,0 +1,156 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Active Snippet | |
| 4 | + * | |
| 5 | + * @package AutomatorWP\Integrations\WPCode\Actions\Active_Snippet | |
| 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_WPCode_Active_Snippet extends AutomatorWP_Integration_Action { | |
| 13 | + | |
| 14 | + public $integration = 'wpcode'; | |
| 15 | + public $action = 'wpcode_active_snippet'; | |
| 16 | + | |
| 17 | + /** | |
| 18 | + * Register the trigger | |
| 19 | + * | |
| 20 | + * @since 1.0.0 | |
| 21 | + */ | |
| 22 | + public function register() { | |
| 23 | + | |
| 24 | + automatorwp_register_action( $this->action, array( | |
| 25 | + 'integration' => $this->integration, | |
| 26 | + 'label' => __( 'Activate a snippet', 'automatorwp' ), | |
| 27 | + 'select_option' => __( '<strong>Activate</strong> a snippet', 'automatorwp' ), | |
| 28 | + /* translators: %1$s: Title snippet. */ | |
| 29 | + 'edit_label' => sprintf( __( 'Activate %1$s', 'automatorwp' ), '{snippet}' ), | |
| 30 | + /* translators: %1$s: Title snippet. */ | |
| 31 | + 'log_label' => sprintf( __( 'Activate %1$s', 'automatorwp' ), '{snippet}' ), | |
| 32 | + 'options' => array( | |
| 33 | + 'snippet' => automatorwp_utilities_ajax_selector_option( array( | |
| 34 | + 'field' => 'snippet', | |
| 35 | + 'option_default' => __( 'Snippet', 'automatorwp' ), | |
| 36 | + 'name' => __( 'Snippet:', 'automatorwp' ), | |
| 37 | + 'action_cb' => 'automatorwp_wpcode_get_snippets', | |
| 38 | + 'options_cb' => 'automatorwp_wpcode_options_cb_snippet', | |
| 39 | + 'placeholder' => 'Select a snippet', | |
| 40 | + 'default' => '' | |
| 41 | + ) ), | |
| 42 | + ), | |
| 43 | + ) ); | |
| 44 | + | |
| 45 | + } | |
| 46 | + | |
| 47 | + /** | |
| 48 | + * Action execution function | |
| 49 | + * | |
| 50 | + * @since 1.0.0 | |
| 51 | + * | |
| 52 | + * @param stdClass $action The action object | |
| 53 | + * @param int $user_id The user ID | |
| 54 | + * @param array $action_options The action's stored options (with tags already passed) | |
| 55 | + * @param stdClass $automation The action's automation object | |
| 56 | + */ | |
| 57 | + public function execute( $action, $user_id, $action_options, $automation ) { | |
| 58 | + | |
| 59 | + // Shorthand | |
| 60 | + $this->result = ''; | |
| 61 | + $snippet_id = $action_options['snippet']; | |
| 62 | + | |
| 63 | + if ( empty ( $snippet_id ) ) { | |
| 64 | + $this->result = __( 'Please, select a snippet to activate', 'automatorwp' ); | |
| 65 | + return; | |
| 66 | + } | |
| 67 | + | |
| 68 | + $snippet = new \WPCode_Snippet( absint( $snippet_id ) ); | |
| 69 | + | |
| 70 | + if ( $snippet->is_active() ) { | |
| 71 | + $this->result = sprintf( __( 'The snippet %s is already active', 'automatorwp' ), get_the_title( $snippet_id ) ); | |
| 72 | + return; | |
| 73 | + } | |
| 74 | + | |
| 75 | + $snippet->activate(); | |
| 76 | + $this->result = sprintf( __( 'The snippet %s has been activated', 'automatorwp' ), get_the_title( $snippet_id ) ); | |
| 77 | + | |
| 78 | + } | |
| 79 | + | |
| 80 | + /** | |
| 81 | + * Register required hooks | |
| 82 | + * | |
| 83 | + * @since 1.0.0 | |
| 84 | + */ | |
| 85 | + public function hooks() { | |
| 86 | + | |
| 87 | + // Log meta data | |
| 88 | + add_filter( 'automatorwp_user_completed_action_log_meta', array( $this, 'log_meta' ), 10, 5 ); | |
| 89 | + | |
| 90 | + // Log fields | |
| 91 | + add_filter( 'automatorwp_log_fields', array( $this, 'log_fields' ), 10, 5 ); | |
| 92 | + | |
| 93 | + parent::hooks(); | |
| 94 | + | |
| 95 | + } | |
| 96 | + | |
| 97 | + /** | |
| 98 | + * Action custom log meta | |
| 99 | + * | |
| 100 | + * @since 1.0.0 | |
| 101 | + * | |
| 102 | + * @param array $log_meta Log meta data | |
| 103 | + * @param stdClass $action The action object | |
| 104 | + * @param int $user_id The user ID | |
| 105 | + * @param array $action_options The action's stored options (with tags already passed) | |
| 106 | + * @param stdClass $automation The action's automation object | |
| 107 | + * | |
| 108 | + * @return array | |
| 109 | + */ | |
| 110 | + public function log_meta( $log_meta, $action, $user_id, $action_options, $automation ) { | |
| 111 | + | |
| 112 | + // Bail if action type don't match this action | |
| 113 | + if( $action->type !== $this->action ) { | |
| 114 | + return $log_meta; | |
| 115 | + } | |
| 116 | + | |
| 117 | + // Store the action's result | |
| 118 | + $log_meta['result'] = $this->result; | |
| 119 | + | |
| 120 | + return $log_meta; | |
| 121 | + } | |
| 122 | + | |
| 123 | + /** | |
| 124 | + * Action custom log fields | |
| 125 | + * | |
| 126 | + * @since 1.0.0 | |
| 127 | + * | |
| 128 | + * @param array $log_fields The log fields | |
| 129 | + * @param stdClass $log The log object | |
| 130 | + * @param stdClass $object The trigger/action/automation object attached to the log | |
| 131 | + * | |
| 132 | + * @return array | |
| 133 | + */ | |
| 134 | + public function log_fields( $log_fields, $log, $object ) { | |
| 135 | + | |
| 136 | + // Bail if log is not assigned to an action | |
| 137 | + if( $log->type !== 'action' ) { | |
| 138 | + return $log_fields; | |
| 139 | + } | |
| 140 | + | |
| 141 | + // Bail if action type don't match this action | |
| 142 | + if( $object->type !== $this->action ) { | |
| 143 | + return $log_fields; | |
| 144 | + } | |
| 145 | + | |
| 146 | + $log_fields['result'] = array( | |
| 147 | + 'name' => __( 'Result:', 'automatorwp' ), | |
| 148 | + 'type' => 'text', | |
| 149 | + ); | |
| 150 | + | |
| 151 | + return $log_fields; | |
| 152 | + } | |
| 153 | + | |
| 154 | +} | |
| 155 | + | |
| 156 | +new AutomatorWP_WPCode_Active_Snippet(); | |