← All changes
|
integrations/shortlinkspro/includes/actions/create-link.php
+212
-0
5.6.1
→
6.0.3
View file →
| @@ -1,0 +1,212 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Add new link | |
| 4 | + * | |
| 5 | + * @package AutomatorWP\Integrations\ShortLinksPro\Actions\Create_Link | |
| 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_ShortLinksPro_Create_Link extends AutomatorWP_Integration_Action { | |
| 14 | + | |
| 15 | + public $integration = 'shortlinkspro'; | |
| 16 | + public $action = 'shortlinkspro_create_link'; | |
| 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' => __( 'Create a link', 'automatorwp' ), | |
| 28 | + 'select_option' => __( 'Create a <strong>link</strong>', 'automatorwp' ), | |
| 29 | + /* translators: %1$s: Link title. */ | |
| 30 | + 'edit_label' => sprintf( __( 'Create a %1$s', 'automatorwp' ), '{link}' ), | |
| 31 | + /* translators: %1$s: Link title. */ | |
| 32 | + 'log_label' => sprintf( __( 'Create a %1$s', 'automatorwp' ), '{link}' ), | |
| 33 | + 'options' => array( | |
| 34 | + 'link' => array( | |
| 35 | + 'from' => 'link', | |
| 36 | + 'default' => __('link', 'automatorwp'), | |
| 37 | + 'fields' => array( | |
| 38 | + 'name_link' => array( | |
| 39 | + 'name' => __( 'Name:', 'automatorwp' ), | |
| 40 | + 'type' => 'text', | |
| 41 | + 'default' => '', | |
| 42 | + 'required' => true | |
| 43 | + ), | |
| 44 | + 'redirection' => array( | |
| 45 | + 'name' => __( 'Redirect type:', 'automatorwp' ), | |
| 46 | + 'type' => 'select', | |
| 47 | + 'options' => shortlinkspro_redirect_types(), | |
| 48 | + 'required' => true | |
| 49 | + ), | |
| 50 | + 'target_url' => array( | |
| 51 | + 'name' => __( 'Target URL:', 'automatorwp' ), | |
| 52 | + 'type' => 'text', | |
| 53 | + 'default' => '', | |
| 54 | + 'required' => true | |
| 55 | + ), | |
| 56 | + 'shortlink_slug' => array( | |
| 57 | + 'name' => __( 'ShortLink slug:', 'automatorwp' ), | |
| 58 | + 'type' => 'text', | |
| 59 | + 'default' => '', | |
| 60 | + 'desc' => __('A random slug will be generated if this field is empty.', 'automatorwp') | |
| 61 | + ), | |
| 62 | + 'notes' => array( | |
| 63 | + 'name' => __( 'Notes:', 'automatorwp' ), | |
| 64 | + 'type' => 'textarea', | |
| 65 | + 'default' => '', | |
| 66 | + 'desc' => __('Internal notes to your link for your own needs.', 'automatorwp') | |
| 67 | + ), | |
| 68 | + 'link_options' => array( | |
| 69 | + 'name' => __( 'Link options:', 'automatorwp' ), | |
| 70 | + 'type' => 'multicheck_inline', | |
| 71 | + 'classes' => 'cmb2-switch', | |
| 72 | + 'options' => array( | |
| 73 | + 'nofollow' => __( 'No Follow', 'automatorwp' ) . cmb_tooltip_get_html( __( 'This will add the nofollow and noindex parameters in the HTTP response headers when enabled.', 'automatorwp' ) ), | |
| 74 | + 'sponsored' => __( 'Sponsored', 'automatorwp' ) . cmb_tooltip_get_html( __( 'This will add the sponsored parameter in the HTTP response headers when enabled.', 'automatorwp' ) ), | |
| 75 | + 'parameter_forwarding' => __( 'Parameter Forwarding', 'automatorwp' ) . cmb_tooltip_get_html( __( 'This will forward parameters passed to links when enabled.', 'automatorwp' ) ), | |
| 76 | + 'tracking' => __( 'Tracking', 'automatorwp' ) . cmb_tooltip_get_html( __( 'This will enable clicks tracking when enabled.', 'automatorwp' ) ), | |
| 77 | + ), | |
| 78 | + 'select_all_button' => false, | |
| 79 | + 'default' => array( 'nofollow', 'tracking' ), | |
| 80 | + 'label_cb' => 'cmb_tooltip_label_cb', | |
| 81 | + ), | |
| 82 | + ) | |
| 83 | + ), | |
| 84 | + ), | |
| 85 | + ) ); | |
| 86 | + | |
| 87 | + } | |
| 88 | + | |
| 89 | + /** | |
| 90 | + * Action execution function | |
| 91 | + * | |
| 92 | + * @since 1.0.0 | |
| 93 | + * | |
| 94 | + * @param stdClass $action The action object | |
| 95 | + * @param int $user_id The user ID | |
| 96 | + * @param array $action_options The action's stored options (with tags already passed) | |
| 97 | + * @param stdClass $automation The action's automation object | |
| 98 | + */ | |
| 99 | + public function execute( $action, $user_id, $action_options, $automation ) { | |
| 100 | + | |
| 101 | + $this->result = ''; | |
| 102 | + | |
| 103 | + // Bail if no target URL | |
| 104 | + if ( empty( $action_options['target_url'] ) ) { | |
| 105 | + $this->result = __('Target url cannot be empty.', 'automatorwp'); | |
| 106 | + return; | |
| 107 | + } | |
| 108 | + | |
| 109 | + // If empty generate random slug | |
| 110 | + if ( empty( $action_options['slug'] ) ) { | |
| 111 | + $slug = shortlinkspro_generate_link_slug(); | |
| 112 | + } | |
| 113 | + | |
| 114 | + // Link options | |
| 115 | + $link_options = $action_options['link_options']; | |
| 116 | + | |
| 117 | + $link_data = array( | |
| 118 | + 'title' => $action_options['name_link'], | |
| 119 | + 'url' => $action_options['target_url'], | |
| 120 | + 'slug' => $slug, | |
| 121 | + 'redirect_type' => $action_options['redirection'], | |
| 122 | + 'nofollow' => absint( in_array( 'nofollow', $link_options) ), | |
| 123 | + 'sponsored' => absint( in_array( 'sponsored', $link_options) ), | |
| 124 | + 'parameter_forwarding' => absint( in_array( 'parameter_forwarding', $link_options) ), | |
| 125 | + 'tracking' => absint( in_array('tracking', $link_options) ), | |
| 126 | + ); | |
| 127 | + | |
| 128 | + // Insert the object | |
| 129 | + $ct_table = ct_setup_table( 'shortlinkspro_links' ); | |
| 130 | + $object_id = ct_insert_object( $link_data ); | |
| 131 | + ct_reset_setup_table(); | |
| 132 | + | |
| 133 | + $this->result = __('Link created.', 'automatorwp'); | |
| 134 | + } | |
| 135 | + | |
| 136 | + /** | |
| 137 | + * Register required hooks | |
| 138 | + * | |
| 139 | + * @since 1.0.0 | |
| 140 | + */ | |
| 141 | + public function hooks() { | |
| 142 | + | |
| 143 | + // Log meta data | |
| 144 | + add_filter( 'automatorwp_user_completed_action_log_meta', array( $this, 'log_meta' ), 10, 5 ); | |
| 145 | + | |
| 146 | + // Log fields | |
| 147 | + add_filter( 'automatorwp_log_fields', array( $this, 'log_fields' ), 10, 5 ); | |
| 148 | + | |
| 149 | + parent::hooks(); | |
| 150 | + | |
| 151 | + } | |
| 152 | + | |
| 153 | + /** | |
| 154 | + * Action custom log meta | |
| 155 | + * | |
| 156 | + * @since 1.0.0 | |
| 157 | + * | |
| 158 | + * @param array $log_meta Log meta data | |
| 159 | + * @param stdClass $action The action object | |
| 160 | + * @param int $user_id The user ID | |
| 161 | + * @param array $action_options The action's stored options (with tags already passed) | |
| 162 | + * @param stdClass $automation The action's automation object | |
| 163 | + * | |
| 164 | + * @return array | |
| 165 | + */ | |
| 166 | + public function log_meta( $log_meta, $action, $user_id, $action_options, $automation ) { | |
| 167 | + | |
| 168 | + // Bail if action type don't match this action | |
| 169 | + if( $action->type !== $this->action ) { | |
| 170 | + return $log_meta; | |
| 171 | + } | |
| 172 | + | |
| 173 | + // Store the action's result | |
| 174 | + $log_meta['result'] = $this->result; | |
| 175 | + | |
| 176 | + return $log_meta; | |
| 177 | + } | |
| 178 | + | |
| 179 | + /** | |
| 180 | + * Action custom log fields | |
| 181 | + * | |
| 182 | + * @since 1.0.0 | |
| 183 | + * | |
| 184 | + * @param array $log_fields The log fields | |
| 185 | + * @param stdClass $log The log object | |
| 186 | + * @param stdClass $object The trigger/action/automation object attached to the log | |
| 187 | + * | |
| 188 | + * @return array | |
| 189 | + */ | |
| 190 | + public function log_fields( $log_fields, $log, $object ) { | |
| 191 | + | |
| 192 | + // Bail if log is not assigned to an action | |
| 193 | + if( $log->type !== 'action' ) { | |
| 194 | + return $log_fields; | |
| 195 | + } | |
| 196 | + | |
| 197 | + // Bail if action type don't match this action | |
| 198 | + if( $object->type !== $this->action ) { | |
| 199 | + return $log_fields; | |
| 200 | + } | |
| 201 | + | |
| 202 | + $log_fields['result'] = array( | |
| 203 | + 'name' => __( 'Result:', 'automatorwp' ), | |
| 204 | + 'type' => 'text', | |
| 205 | + ); | |
| 206 | + | |
| 207 | + return $log_fields; | |
| 208 | + } | |
| 209 | + | |
| 210 | +} | |
| 211 | + | |
| 212 | +new AutomatorWP_ShortLinksPro_Create_Link(); | |