| 1 |
<?php |
| 2 |
/** |
| 3 |
* ConvertKit Admin Post class. |
| 4 |
* |
| 5 |
* @package ConvertKit |
| 6 |
* @author ConvertKit |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Registers a metabox on Posts, Pages and public facing Custom Post Types |
| 11 |
* and saves its settings when the Post is saved in the WordPress Administration |
| 12 |
* interface. |
| 13 |
* |
| 14 |
* @package ConvertKit |
| 15 |
* @author ConvertKit |
| 16 |
*/ |
| 17 |
class ConvertKit_Admin_Post { |
| 18 |
|
| 19 |
/** |
| 20 |
* Registers action and filter hooks. |
| 21 |
* |
| 22 |
* @since 1.9.6 |
| 23 |
*/ |
| 24 |
public function __construct() { |
| 25 |
|
| 26 |
// Register "Add New" Kit button on Pages. |
| 27 |
add_filter( 'views_edit-page', array( $this, 'output_wp_list_table_buttons' ) ); |
| 28 |
|
| 29 |
add_action( 'post_submitbox_misc_actions', array( $this, 'output_pre_publish_actions' ) ); |
| 30 |
add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) ); |
| 31 |
add_action( 'save_post', array( $this, 'save_post_meta' ) ); |
| 32 |
|
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Registers 'Add New' buttons for the given Post Type's admin screen. |
| 37 |
* |
| 38 |
* If no options are registered, no button is displayed. |
| 39 |
* |
| 40 |
* JS will move this button to be displayed next to the "Add New" button when viewing the table of Pages or Posts, |
| 41 |
* as there is not a native WordPress action/filter for registering buttons next to the "Add New" button. |
| 42 |
* |
| 43 |
* @since 2.5.5 |
| 44 |
* |
| 45 |
* @param array $views Views. |
| 46 |
* @return array Views |
| 47 |
*/ |
| 48 |
public function output_wp_list_table_buttons( $views ) { |
| 49 |
|
| 50 |
// Get current post type that we're viewing. |
| 51 |
$post_type = $this->get_current_post_type(); |
| 52 |
|
| 53 |
// Don't output any buttons if we couldn't determine the current post type. |
| 54 |
if ( ! $post_type ) { |
| 55 |
return $views; |
| 56 |
} |
| 57 |
|
| 58 |
// Define a blank array of buttons to be filtered. |
| 59 |
$buttons = array(); |
| 60 |
|
| 61 |
/** |
| 62 |
* Registers 'Add New' buttons for the given Post Type's admin screen. |
| 63 |
* |
| 64 |
* @since 2.5.5 |
| 65 |
* |
| 66 |
* @param array $buttons Buttons. |
| 67 |
* @param string $post_type Post Type. |
| 68 |
*/ |
| 69 |
$buttons = apply_filters( 'convertkit_admin_post_register_add_new_buttons', $buttons, $post_type ); |
| 70 |
|
| 71 |
// Bail if no buttons are registered for display. |
| 72 |
if ( ! count( $buttons ) ) { |
| 73 |
return $views; |
| 74 |
} |
| 75 |
|
| 76 |
// Enqueue JS and CSS. |
| 77 |
wp_enqueue_script( 'convertkit-admin-wp-list-table-buttons', CONVERTKIT_PLUGIN_URL . 'resources/backend/js/wp-list-table-buttons.js', array( 'jquery' ), CONVERTKIT_PLUGIN_VERSION, true ); |
| 78 |
wp_enqueue_style( 'convertkit-admin-wp-list-table-buttons', CONVERTKIT_PLUGIN_URL . 'resources/backend/css/wp-list-table-buttons.css', array(), CONVERTKIT_PLUGIN_VERSION ); |
| 79 |
|
| 80 |
// Build buttons HTML. |
| 81 |
$html = ''; |
| 82 |
foreach ( $buttons as $button_name => $button ) { |
| 83 |
$html .= sprintf( |
| 84 |
'<a href="%s">%s</a>', |
| 85 |
esc_attr( $button['url'] ), |
| 86 |
esc_html( $button['label'] ) |
| 87 |
); |
| 88 |
} |
| 89 |
|
| 90 |
// Register an 'Add New' dropdown button, with buttons HTML. |
| 91 |
$views['convertkit'] = sprintf( |
| 92 |
'<span class="convertkit-action page-title-action hidden">%s<span class="convertkit-actions hidden">%s</span></span>', |
| 93 |
__( 'Add New', 'convertkit' ), |
| 94 |
$html |
| 95 |
); |
| 96 |
|
| 97 |
return $views; |
| 98 |
|
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Enqueue JavaScript when editing a Page, Post or Custom Post Type that outputs |
| 103 |
* ConvertKit Plugin settings. |
| 104 |
* |
| 105 |
* @since 1.9.6.4 |
| 106 |
*/ |
| 107 |
public function enqueue_scripts() { |
| 108 |
|
| 109 |
// Enqueue Select2 JS. |
| 110 |
convertkit_select2_enqueue_scripts(); |
| 111 |
|
| 112 |
/** |
| 113 |
* Enqueue JavaScript when editing a Page, Post or Custom Post Type that outputs |
| 114 |
* ConvertKit Plugin settings. |
| 115 |
* |
| 116 |
* @since 1.9.6.4 |
| 117 |
*/ |
| 118 |
do_action( 'convertkit_admin_post_enqueue_scripts' ); |
| 119 |
|
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Enqueue CSS when editing a Page, Post or Custom Post Type that outputs |
| 124 |
* ConvertKit Plugin settings. |
| 125 |
* |
| 126 |
* @since 1.9.6.4 |
| 127 |
*/ |
| 128 |
public function enqueue_styles() { |
| 129 |
|
| 130 |
// Enqueue Select2 CSS. |
| 131 |
convertkit_select2_enqueue_styles(); |
| 132 |
|
| 133 |
// Enqueue Post CSS. |
| 134 |
wp_enqueue_style( 'convertkit-post', CONVERTKIT_PLUGIN_URL . 'resources/backend/css/post.css', array(), CONVERTKIT_PLUGIN_VERSION ); |
| 135 |
|
| 136 |
/** |
| 137 |
* Enqueue CSS for the Settings Screen at Settings > Kit |
| 138 |
* |
| 139 |
* @since 1.9.6.4 |
| 140 |
*/ |
| 141 |
do_action( 'convertkit_admin_post_enqueue_styles' ); |
| 142 |
|
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Registers actions in the pre-publish actions section of the Publish metabox |
| 147 |
* in the Classic Editor. |
| 148 |
* |
| 149 |
* @since 2.4.0 |
| 150 |
* |
| 151 |
* @param WP_Post $post WordPress Post. |
| 152 |
*/ |
| 153 |
public function output_pre_publish_actions( $post ) { |
| 154 |
|
| 155 |
// Bail if no actions registered. |
| 156 |
$pre_publish_actions = convertkit_get_pre_publish_actions(); |
| 157 |
if ( ! count( $pre_publish_actions ) ) { |
| 158 |
return; |
| 159 |
} |
| 160 |
|
| 161 |
// Bail if Post is not a supported Post Type. |
| 162 |
if ( get_post_type( $post ) !== 'post' ) { |
| 163 |
return; |
| 164 |
} |
| 165 |
|
| 166 |
// Bail if Post is not a draft. |
| 167 |
if ( ! in_array( $post->post_status, array( 'draft', 'auto-draft' ), true ) ) { |
| 168 |
return; |
| 169 |
} |
| 170 |
|
| 171 |
// Load pre-publish actions view. |
| 172 |
include CONVERTKIT_PLUGIN_PATH . '/views/backend/post/pre-publish-actions.php'; |
| 173 |
|
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Adds a meta box for the given Post Type. |
| 178 |
* |
| 179 |
* @since 1.9.6 |
| 180 |
* |
| 181 |
* @param string $post_type Post Type. |
| 182 |
*/ |
| 183 |
public function add_meta_boxes( $post_type ) { |
| 184 |
|
| 185 |
// Don't register the meta box if this Post Type isn't supported. |
| 186 |
$supported_post_types = convertkit_get_supported_post_types(); |
| 187 |
if ( ! in_array( $post_type, $supported_post_types, true ) ) { |
| 188 |
return; |
| 189 |
} |
| 190 |
|
| 191 |
// Register Meta Box. |
| 192 |
add_meta_box( 'wp-convertkit-meta-box', __( 'Kit', 'convertkit' ), array( $this, 'display_meta_box' ), $post_type, 'normal' ); |
| 193 |
|
| 194 |
// Enqueue JS and CSS. |
| 195 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); |
| 196 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_styles' ) ); |
| 197 |
|
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* Outputs the meta box. |
| 202 |
* |
| 203 |
* @since 1.9.6 |
| 204 |
* |
| 205 |
* @param WP_Post $post The Post being edited. |
| 206 |
*/ |
| 207 |
public function display_meta_box( $post ) { |
| 208 |
|
| 209 |
// Don't register the meta box if this Post is the blog archive page. |
| 210 |
if ( $post->ID === get_option( 'page_for_posts' ) ) { |
| 211 |
return; |
| 212 |
} |
| 213 |
|
| 214 |
// Show a warning if the API credentials haven't been set. |
| 215 |
$settings = new ConvertKit_Settings(); |
| 216 |
if ( ! $settings->has_access_and_refresh_token() ) { |
| 217 |
$post_type = get_post_type_object( $post->post_type ); |
| 218 |
$api = new ConvertKit_API_V4( CONVERTKIT_OAUTH_CLIENT_ID, CONVERTKIT_OAUTH_CLIENT_REDIRECT_URI ); |
| 219 |
include CONVERTKIT_PLUGIN_PATH . '/views/backend/post/no-api-key.php'; |
| 220 |
return; |
| 221 |
} |
| 222 |
|
| 223 |
// Initialize Restrict Content Settings class. |
| 224 |
$restrict_content_settings = new ConvertKit_Settings_Restrict_Content(); |
| 225 |
|
| 226 |
// Fetch Post Settings, Forms, Landing Pages and Tags. |
| 227 |
$convertkit_post = new ConvertKit_Post( $post->ID ); |
| 228 |
$convertkit_forms = new ConvertKit_Resource_Forms(); |
| 229 |
|
| 230 |
$convertkit_landing_pages = new ConvertKit_Resource_Landing_Pages(); |
| 231 |
$convertkit_products = new ConvertKit_Resource_Products(); |
| 232 |
$convertkit_tags = new ConvertKit_Resource_Tags(); |
| 233 |
|
| 234 |
// Get settings page link. |
| 235 |
$settings_link = convertkit_get_settings_link(); |
| 236 |
|
| 237 |
// Load metabox view. |
| 238 |
include CONVERTKIT_PLUGIN_PATH . '/views/backend/post/meta-box.php'; |
| 239 |
|
| 240 |
} |
| 241 |
|
| 242 |
/** |
| 243 |
* Saves Post Settings when either editing a Post/Page or using the Quick Edit functionality. |
| 244 |
* |
| 245 |
* @since 1.9.6 |
| 246 |
* |
| 247 |
* @param int $post_id Post ID. |
| 248 |
*/ |
| 249 |
public function save_post_meta( $post_id ) { |
| 250 |
|
| 251 |
// Bail if this is an autosave. |
| 252 |
if ( wp_is_post_autosave( $post_id ) ) { |
| 253 |
return; |
| 254 |
} |
| 255 |
|
| 256 |
// Bail if this is a post revision. |
| 257 |
if ( wp_is_post_revision( $post_id ) ) { |
| 258 |
return; |
| 259 |
} |
| 260 |
|
| 261 |
// Bail if no nonce field exists. |
| 262 |
if ( ! isset( $_POST['wp-convertkit-save-meta-nonce'] ) ) { |
| 263 |
return; |
| 264 |
} |
| 265 |
|
| 266 |
// Bail if the nonce verification fails. |
| 267 |
if ( ! wp_verify_nonce( sanitize_key( wp_unslash( $_POST['wp-convertkit-save-meta-nonce'] ) ), 'wp-convertkit-save-meta' ) ) { |
| 268 |
return; |
| 269 |
} |
| 270 |
|
| 271 |
// Bail if no ConvertKit settings were posted. |
| 272 |
if ( ! isset( $_POST['wp-convertkit'] ) ) { |
| 273 |
return; |
| 274 |
} |
| 275 |
|
| 276 |
// Save Post's settings. |
| 277 |
$this->save_post_settings( $post_id, wp_unslash( $_POST['wp-convertkit'] ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 278 |
|
| 279 |
} |
| 280 |
|
| 281 |
/** |
| 282 |
* Saves the Post's settings submitted via $_POST or $_REQUEST. |
| 283 |
* Can be used across Edit, Quick Edit and Bulk Edit. |
| 284 |
* |
| 285 |
* @since 1.9.8.0 |
| 286 |
* |
| 287 |
* @param int $post_id Post ID. |
| 288 |
* @param array $settings Settings. |
| 289 |
*/ |
| 290 |
public function save_post_settings( $post_id, $settings ) { |
| 291 |
|
| 292 |
// Get Post's settings. |
| 293 |
$convertkit_post = new ConvertKit_Post( $post_id ); |
| 294 |
$meta = $convertkit_post->get(); |
| 295 |
|
| 296 |
// Update Post's setting values if they were included in the $_POST data. |
| 297 |
// Some values may not be included in the $_POST data e.g. if Quick Edit is used and no Landing Page was specified, |
| 298 |
// in which case the existing Post's value will be used. |
| 299 |
// This ensures settings are not deleted by accident. |
| 300 |
foreach ( $meta as $key => $existing_value ) { |
| 301 |
// Skip if this setting isn't included in the $_POST data. |
| 302 |
if ( ! isset( $settings[ $key ] ) ) { |
| 303 |
continue; |
| 304 |
} |
| 305 |
|
| 306 |
// Sanitize value. |
| 307 |
$new_value = sanitize_text_field( $settings[ $key ] ); |
| 308 |
|
| 309 |
// Skip if the setting value is -2, as this means it's a Bulk Edit request and this setting |
| 310 |
// is set as 'No Change'. |
| 311 |
if ( $new_value == '-2' ) { // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual |
| 312 |
continue; |
| 313 |
} |
| 314 |
|
| 315 |
// Update setting using posted value. |
| 316 |
$meta[ $key ] = $new_value; |
| 317 |
} |
| 318 |
|
| 319 |
// Save settings. |
| 320 |
$convertkit_post->save( $meta ); |
| 321 |
|
| 322 |
// If a Form or Landing Page was specified, request a review. |
| 323 |
// This can safely be called multiple times, as the review request |
| 324 |
// class will ensure once a review request is dismissed by the user, |
| 325 |
// it is never displayed again. |
| 326 |
if ( $meta['form'] || $meta['landing_page'] ) { |
| 327 |
WP_ConvertKit()->get_class( 'review_request' )->request_review(); |
| 328 |
} |
| 329 |
|
| 330 |
} |
| 331 |
|
| 332 |
/** |
| 333 |
* Get the current post type based on the screen that is viewed. |
| 334 |
* |
| 335 |
* @since 2.5.5 |
| 336 |
* |
| 337 |
* @return bool|string |
| 338 |
*/ |
| 339 |
private function get_current_post_type() { |
| 340 |
|
| 341 |
return convertkit_get_current_screen( 'post_type' ); |
| 342 |
|
| 343 |
} |
| 344 |
|
| 345 |
} |
| 346 |
|