| 1 |
<?php |
| 2 |
/** |
| 3 |
* ConvertKit Pre-publish Action class. |
| 4 |
* |
| 5 |
* @package ConvertKit |
| 6 |
* @author ConvertKit |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* ConvertKit Pre-publish Action definition for Gutenberg and Shortcode. |
| 11 |
* |
| 12 |
* @package ConvertKit |
| 13 |
* @author ConvertKit |
| 14 |
*/ |
| 15 |
class ConvertKit_Pre_Publish_Action { |
| 16 |
|
| 17 |
/** |
| 18 |
* Holds the Post Meta Key prefix that stores the ConvertKit pre-publish action setting on a per-Post basis. |
| 19 |
* |
| 20 |
* @var string |
| 21 |
* |
| 22 |
* @since 2.4.0 |
| 23 |
*/ |
| 24 |
const POST_META_KEY_PREFIX = '_convertkit_action_'; |
| 25 |
|
| 26 |
/** |
| 27 |
* Holds the Post Meta Key that stores whether to run a given ConvertKit pre-publish action on a per-Post basis. |
| 28 |
* |
| 29 |
* @var string |
| 30 |
* |
| 31 |
* @since 2.4.0 |
| 32 |
*/ |
| 33 |
private $meta_key = ''; |
| 34 |
|
| 35 |
/** |
| 36 |
* Constructor |
| 37 |
* |
| 38 |
* @since 2.4.0 |
| 39 |
*/ |
| 40 |
public function __construct() { |
| 41 |
|
| 42 |
// Define the meta key. |
| 43 |
$this->meta_key = self::POST_META_KEY_PREFIX . $this->get_name(); |
| 44 |
|
| 45 |
// Register this as a pre-publish action in the ConvertKit Plugin. |
| 46 |
add_filter( 'convertkit_get_pre_publish_actions', array( $this, 'register' ) ); |
| 47 |
|
| 48 |
// Register meta key for Gutenberg. |
| 49 |
add_action( 'init', array( $this, 'register_meta_key' ) ); |
| 50 |
|
| 51 |
// Perform pre-publish action. |
| 52 |
add_action( 'transition_post_status', array( $this, 'run' ), 10, 3 ); |
| 53 |
|
| 54 |
// Save whether to run the pre-publish action when the Post is saved. |
| 55 |
add_action( 'save_post', array( $this, 'save_post_meta' ) ); |
| 56 |
|
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Registers this pre-publish action with the ConvertKit Plugin. |
| 61 |
* |
| 62 |
* @since 2.4.0 |
| 63 |
* |
| 64 |
* @param array $pre_publish_actions Pre-publish actions to register. |
| 65 |
* @return array Pre-publish actions to register. |
| 66 |
*/ |
| 67 |
public function register( $pre_publish_actions ) { |
| 68 |
|
| 69 |
$pre_publish_actions[ $this->get_name() ] = array( |
| 70 |
'name' => $this->get_name(), |
| 71 |
'label' => $this->get_label(), |
| 72 |
'description' => $this->get_description(), |
| 73 |
); |
| 74 |
|
| 75 |
return $pre_publish_actions; |
| 76 |
|
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Registers the action's meta key in WordPress. |
| 81 |
* This is required for Gutenberg to save the '_convertkit_action_{$name}' |
| 82 |
* meta key/value pair when a Post is saved. |
| 83 |
* |
| 84 |
* @since 2.4.0 |
| 85 |
*/ |
| 86 |
public function register_meta_key() { |
| 87 |
|
| 88 |
// Register action as a meta key. |
| 89 |
register_post_meta( |
| 90 |
'post', |
| 91 |
$this->meta_key, |
| 92 |
array( |
| 93 |
'show_in_rest' => true, |
| 94 |
'single' => true, |
| 95 |
'type' => 'boolean', |
| 96 |
'auth_callback' => '__return_true', |
| 97 |
) |
| 98 |
); |
| 99 |
|
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Performs the pre-publish action, if the Post has been transitioned to published. |
| 104 |
* |
| 105 |
* @since 2.4.0 |
| 106 |
* |
| 107 |
* @param string $new_status New Status. |
| 108 |
* @param string $old_status Old Status. |
| 109 |
* @param WP_Post $post Post. |
| 110 |
*/ |
| 111 |
public function run( $new_status, $old_status, $post ) { |
| 112 |
|
| 113 |
// Remove actions registered by this Plugin. |
| 114 |
// This ensures that when Page Builders call trigger actions via AJAX, we don't run this multiple times. |
| 115 |
remove_action( 'wp_insert_post', array( $this, 'classic_editor_post_published' ), 999 ); |
| 116 |
remove_action( 'rest_after_insert_' . $post->post_type, array( $this, 'rest_api_post_published' ), 10 ); |
| 117 |
|
| 118 |
// Ignore if the Post is not transitioning to published. |
| 119 |
if ( $new_status !== 'publish' ) { |
| 120 |
return; |
| 121 |
} |
| 122 |
|
| 123 |
// Ignore if the statuses match i.e. it's already a published Post that is being updated. |
| 124 |
if ( $new_status === $old_status ) { |
| 125 |
return; |
| 126 |
} |
| 127 |
|
| 128 |
// REST API and Gutenberg / Block Editor. |
| 129 |
if ( $this->is_rest_api_request() ) { |
| 130 |
add_action( 'rest_after_insert_' . $post->post_type, array( $this, 'rest_api_post_publish' ), 10 ); |
| 131 |
return; |
| 132 |
} |
| 133 |
|
| 134 |
// Classic Editor. |
| 135 |
// transition_post_status hooks are always called before save_post hooks. Therefore, if a Post |
| 136 |
// is created and immediately published (it's not saved as a draft first), we need to |
| 137 |
// manually call the save_post_meta() function now, before checking whether the action is enabled. |
| 138 |
// Otherwise, is_enabled() will return false because save_post_meta() has not yet been triggered. |
| 139 |
$this->save_post_meta( $post->ID ); |
| 140 |
|
| 141 |
// Check the action was enabled on this Post by the user. |
| 142 |
if ( ! $this->is_enabled( $post->ID ) ) { |
| 143 |
return; |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Run this pre-publish action, as the WordPress Post has just transitioned to publish |
| 148 |
* from another state. |
| 149 |
* |
| 150 |
* @since 2.4.0 |
| 151 |
* |
| 152 |
* @param WP_Post $post Post. |
| 153 |
*/ |
| 154 |
do_action( 'convertkit_pre_publish_action_run_' . $this->get_name(), $post ); |
| 155 |
|
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Called when a Post is created or updated via the REST API, including Gutenberg. |
| 160 |
* |
| 161 |
* @since 2.4.0 |
| 162 |
* |
| 163 |
* @param WP_Post $post Post. |
| 164 |
*/ |
| 165 |
public function rest_api_post_publish( $post ) { |
| 166 |
|
| 167 |
// Check the action was enabled on this Post by the user. |
| 168 |
if ( ! $this->is_enabled( $post->ID ) ) { |
| 169 |
return; |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Run this pre-publish action, as the WordPress Post has just transitioned to publish |
| 174 |
* from another state. |
| 175 |
* |
| 176 |
* @since 2.4.0 |
| 177 |
* |
| 178 |
* @param WP_Post $post Post. |
| 179 |
*/ |
| 180 |
do_action( 'convertkit_pre_publish_action_run_' . $this->get_name(), $post ); |
| 181 |
|
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Saves a meta key/value pair against the Post in the Classic Editor, based on whether the user has permitted |
| 186 |
* that the pre-publish action should run when the Post is published. |
| 187 |
* |
| 188 |
* @since 2.4.0 |
| 189 |
* |
| 190 |
* @param int $post_id Post ID. |
| 191 |
*/ |
| 192 |
public function save_post_meta( $post_id ) { |
| 193 |
|
| 194 |
// Bail if this is an autosave. |
| 195 |
if ( wp_is_post_autosave( $post_id ) ) { |
| 196 |
return; |
| 197 |
} |
| 198 |
|
| 199 |
// Bail if this is a post revision. |
| 200 |
if ( wp_is_post_revision( $post_id ) ) { |
| 201 |
return; |
| 202 |
} |
| 203 |
|
| 204 |
// Bail if no nonce field exists. |
| 205 |
if ( ! isset( $_POST['wp-convertkit-pre-publish-actions-nonce'] ) ) { |
| 206 |
return; |
| 207 |
} |
| 208 |
|
| 209 |
// Bail if the nonce verification fails. |
| 210 |
if ( ! wp_verify_nonce( sanitize_key( wp_unslash( $_POST['wp-convertkit-pre-publish-actions-nonce'] ) ), 'wp-convertkit-pre-publish-actions' ) ) { |
| 211 |
return; |
| 212 |
} |
| 213 |
|
| 214 |
// Bail if the current user cannot edit this Post. |
| 215 |
if ( ! current_user_can( 'edit_post', $post_id ) ) { |
| 216 |
return; |
| 217 |
} |
| 218 |
|
| 219 |
// Delete meta key if this action's checkbox has not been checked. |
| 220 |
if ( ! array_key_exists( $this->meta_key, $_POST ) ) { |
| 221 |
return delete_post_meta( $post_id, $this->meta_key ); |
| 222 |
} |
| 223 |
|
| 224 |
// Save setting. |
| 225 |
update_post_meta( $post_id, $this->meta_key, true ); |
| 226 |
|
| 227 |
} |
| 228 |
|
| 229 |
/** |
| 230 |
* Deletes the action's meta key and value from the given Post. |
| 231 |
* |
| 232 |
* @since 2.4.0 |
| 233 |
* |
| 234 |
* @param int $post_id Post ID. |
| 235 |
*/ |
| 236 |
public function delete_post_meta( $post_id ) { |
| 237 |
|
| 238 |
delete_post_meta( $post_id, $this->meta_key ); |
| 239 |
|
| 240 |
} |
| 241 |
|
| 242 |
/** |
| 243 |
* Returns this action's programmatic name, excluding the convertkit- prefix. |
| 244 |
* |
| 245 |
* @since 2.4.0 |
| 246 |
* |
| 247 |
* @return string |
| 248 |
*/ |
| 249 |
public function get_name() { |
| 250 |
|
| 251 |
/** |
| 252 |
* This will register as: |
| 253 |
* - a Classic Editor pre-publish action, displayed as a checkbox on a draft Post. |
| 254 |
* - a Gutenberg pre-publish action, with the name convertkit-action-{$name}. |
| 255 |
*/ |
| 256 |
return ''; |
| 257 |
|
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
* Returns this action's label. |
| 262 |
* |
| 263 |
* @since 2.4.0 |
| 264 |
* |
| 265 |
* @return string |
| 266 |
*/ |
| 267 |
public function get_label() { |
| 268 |
|
| 269 |
return ''; |
| 270 |
|
| 271 |
} |
| 272 |
|
| 273 |
/** |
| 274 |
* Returns this action's description. |
| 275 |
* |
| 276 |
* @since 2.4.0 |
| 277 |
* |
| 278 |
* @return string |
| 279 |
*/ |
| 280 |
public function get_description() { |
| 281 |
|
| 282 |
return ''; |
| 283 |
|
| 284 |
} |
| 285 |
|
| 286 |
/** |
| 287 |
* Returns whether this action has been enabled by the user to be run |
| 288 |
* when the Post is published. |
| 289 |
* |
| 290 |
* @since 2.4.0 |
| 291 |
* |
| 292 |
* @param int $post_id Post ID. |
| 293 |
* @return bool |
| 294 |
*/ |
| 295 |
public function is_enabled( $post_id ) { |
| 296 |
|
| 297 |
return (bool) get_post_meta( $post_id, $this->meta_key, true ); |
| 298 |
|
| 299 |
} |
| 300 |
|
| 301 |
/** |
| 302 |
* Helper function to determine if the request is a REST API request. |
| 303 |
* |
| 304 |
* @since 2.4.0 |
| 305 |
* |
| 306 |
* @return bool Is REST API Request |
| 307 |
*/ |
| 308 |
public function is_rest_api_request() { |
| 309 |
|
| 310 |
if ( ! defined( 'REST_REQUEST' ) ) { |
| 311 |
return false; |
| 312 |
} |
| 313 |
|
| 314 |
if ( ! REST_REQUEST ) { |
| 315 |
return false; |
| 316 |
} |
| 317 |
|
| 318 |
return true; |
| 319 |
|
| 320 |
} |
| 321 |
|
| 322 |
} |
| 323 |
|