| @@ -7,10 +7,10 @@ | ||
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | 9 | /** |
| 10 | 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. | |
| 11 | + * that do not use the block editor, saving settings when the Post is saved | |
| 12 | + * in the WordPress Administration interface. | |
| 13 | 13 | * |
| 14 | 14 | * @package ConvertKit |
| 15 | 15 | * @author ConvertKit |
| 16 | 16 | */ |
| @@ -22,14 +22,84 @@ | ||
| 22 | 22 | * @since 1.9.6 |
| 23 | 23 | */ |
| 24 | 24 | public function __construct() { |
| 25 | 25 | |
| 26 | - add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) ); | |
| 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' ), 10, 2 ); | |
| 27 | 31 | add_action( 'save_post', array( $this, 'save_post_meta' ) ); |
| 28 | 32 | |
| 29 | 33 | } |
| 30 | 34 | |
| 31 | 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 | + /** | |
| 32 | 102 | * Enqueue JavaScript when editing a Page, Post or Custom Post Type that outputs |
| 33 | 103 | * ConvertKit Plugin settings. |
| 34 | 104 | * |
| 35 | 105 | * @since 1.9.6.4 |
| @@ -62,10 +132,13 @@ | ||
| 62 | 132 | |
| 63 | 133 | // Enqueue Post CSS. |
| 64 | 134 | wp_enqueue_style( 'convertkit-post', CONVERTKIT_PLUGIN_URL . 'resources/backend/css/post.css', array(), CONVERTKIT_PLUGIN_VERSION ); |
| 65 | 135 | |
| 136 | + // Enqueue Refresh Resources CSS. | |
| 137 | + wp_enqueue_style( 'convertkit-admin-refresh-resources', CONVERTKIT_PLUGIN_URL . 'resources/backend/css/refresh-resources.css', array(), CONVERTKIT_PLUGIN_VERSION ); | |
| 138 | + | |
| 66 | 139 | /** |
| 67 | - * Enqueue CSS for the Settings Screen at Settings > ConvertKit | |
| 140 | + * Enqueue CSS for the Settings Screen at Settings > Kit | |
| 68 | 141 | * |
| 69 | 142 | * @since 1.9.6.4 |
| 70 | 143 | */ |
| 71 | 144 | do_action( 'convertkit_admin_post_enqueue_styles' ); |
| @@ -72,15 +145,47 @@ | ||
| 72 | 145 | |
| 73 | 146 | } |
| 74 | 147 | |
| 75 | 148 | /** |
| 149 | + * Registers actions in the pre-publish actions section of the Publish metabox | |
| 150 | + * in the Classic Editor. | |
| 151 | + * | |
| 152 | + * @since 2.4.0 | |
| 153 | + * | |
| 154 | + * @param WP_Post $post WordPress Post. | |
| 155 | + */ | |
| 156 | + public function output_pre_publish_actions( $post ) { | |
| 157 | + | |
| 158 | + // Bail if no actions registered. | |
| 159 | + $pre_publish_actions = convertkit_get_pre_publish_actions(); | |
| 160 | + if ( ! count( $pre_publish_actions ) ) { | |
| 161 | + return; | |
| 162 | + } | |
| 163 | + | |
| 164 | + // Bail if Post is not a supported Post Type. | |
| 165 | + if ( get_post_type( $post ) !== 'post' ) { | |
| 166 | + return; | |
| 167 | + } | |
| 168 | + | |
| 169 | + // Bail if Post is not a draft. | |
| 170 | + if ( ! in_array( $post->post_status, array( 'draft', 'auto-draft' ), true ) ) { | |
| 171 | + return; | |
| 172 | + } | |
| 173 | + | |
| 174 | + // Load pre-publish actions view. | |
| 175 | + include CONVERTKIT_PLUGIN_PATH . '/views/backend/post/pre-publish-actions.php'; | |
| 176 | + | |
| 177 | + } | |
| 178 | + | |
| 179 | + /** | |
| 76 | 180 | * Adds a meta box for the given Post Type. |
| 77 | 181 | * |
| 78 | 182 | * @since 1.9.6 |
| 79 | 183 | * |
| 80 | - * @param string $post_type Post Type. | |
| 184 | + * @param string $post_type Post Type. | |
| 185 | + * @param WP_Post $post Post. | |
| 81 | 186 | */ |
| 82 | - public function add_meta_boxes( $post_type ) { | |
| 187 | + public function add_meta_boxes( $post_type, $post ) { | |
| 83 | 188 | |
| 84 | 189 | // Don't register the meta box if this Post Type isn't supported. |
| 85 | 190 | $supported_post_types = convertkit_get_supported_post_types(); |
| 86 | 191 | if ( ! in_array( $post_type, $supported_post_types, true ) ) { |
| @@ -86,10 +191,15 @@ | ||
| 86 | 191 | if ( ! in_array( $post_type, $supported_post_types, true ) ) { |
| 87 | 192 | return; |
| 88 | 193 | } |
| 89 | 194 | |
| 195 | + // Don't register the meta box if the block editor is being used, as register_post_meta() handles saving post meta. | |
| 196 | + if ( function_exists( 'use_block_editor_for_post' ) && use_block_editor_for_post( $post ) ) { | |
| 197 | + return; | |
| 198 | + } | |
| 199 | + | |
| 90 | 200 | // Register Meta Box. |
| 91 | - add_meta_box( 'wp-convertkit-meta-box', __( 'ConvertKit', 'convertkit' ), array( $this, 'display_meta_box' ), $post_type, 'normal' ); | |
| 201 | + add_meta_box( 'wp-convertkit-meta-box', __( 'Kit', 'convertkit' ), array( $this, 'display_meta_box' ), $post_type, 'normal' ); | |
| 92 | 202 | |
| 93 | 203 | // Enqueue JS and CSS. |
| 94 | 204 | add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); |
| 95 | 205 | add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_styles' ) ); |
| @@ -111,10 +221,11 @@ | ||
| 111 | 221 | } |
| 112 | 222 | |
| 113 | 223 | // Show a warning if the API credentials haven't been set. |
| 114 | 224 | $settings = new ConvertKit_Settings(); |
| 115 | - if ( ! $settings->has_api_key_and_secret() ) { | |
| 225 | + if ( ! $settings->has_access_and_refresh_token() ) { | |
| 116 | 226 | $post_type = get_post_type_object( $post->post_type ); |
| 227 | + $api = new ConvertKit_API_V4( CONVERTKIT_OAUTH_CLIENT_ID, CONVERTKIT_OAUTH_CLIENT_REDIRECT_URI ); | |
| 117 | 228 | include CONVERTKIT_PLUGIN_PATH . '/views/backend/post/no-api-key.php'; |
| 118 | 229 | return; |
| 119 | 230 | } |
| 120 | 231 | |
| @@ -121,10 +232,11 @@ | ||
| 121 | 232 | // Initialize Restrict Content Settings class. |
| 122 | 233 | $restrict_content_settings = new ConvertKit_Settings_Restrict_Content(); |
| 123 | 234 | |
| 124 | 235 | // Fetch Post Settings, Forms, Landing Pages and Tags. |
| 125 | - $convertkit_post = new ConvertKit_Post( $post->ID ); | |
| 126 | - $convertkit_forms = new ConvertKit_Resource_Forms(); | |
| 236 | + $convertkit_post = new ConvertKit_Post( $post->ID ); | |
| 237 | + $convertkit_forms = new ConvertKit_Resource_Forms(); | |
| 238 | + | |
| 127 | 239 | $convertkit_landing_pages = new ConvertKit_Resource_Landing_Pages(); |
| 128 | 240 | $convertkit_products = new ConvertKit_Resource_Products(); |
| 129 | 241 | $convertkit_tags = new ConvertKit_Resource_Tags(); |
| 130 | 242 | |
| @@ -170,9 +282,9 @@ | ||
| 170 | 282 | return; |
| 171 | 283 | } |
| 172 | 284 | |
| 173 | 285 | // Save Post's settings. |
| 174 | - $this->save_post_settings( $post_id, $_POST['wp-convertkit'] ); | |
| 286 | + $this->save_post_settings( $post_id, wp_unslash( $_POST['wp-convertkit'] ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | |
| 175 | 287 | |
| 176 | 288 | } |
| 177 | 289 | |
| 178 | 290 | /** |
| @@ -200,13 +312,13 @@ | ||
| 200 | 312 | continue; |
| 201 | 313 | } |
| 202 | 314 | |
| 203 | 315 | // Sanitize value. |
| 204 | - $new_value = sanitize_text_field( wp_unslash( $settings[ $key ] ) ); | |
| 316 | + $new_value = sanitize_text_field( $settings[ $key ] ); | |
| 205 | 317 | |
| 206 | 318 | // Skip if the setting value is -2, as this means it's a Bulk Edit request and this setting |
| 207 | 319 | // is set as 'No Change'. |
| 208 | - if ( $new_value == '-2' ) { // phpcs:ignore WordPress.PHP.StrictComparisons | |
| 320 | + if ( $new_value == '-2' ) { // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual | |
| 209 | 321 | continue; |
| 210 | 322 | } |
| 211 | 323 | |
| 212 | 324 | // Update setting using posted value. |
| @@ -222,8 +334,21 @@ | ||
| 222 | 334 | // it is never displayed again. |
| 223 | 335 | if ( $meta['form'] || $meta['landing_page'] ) { |
| 224 | 336 | WP_ConvertKit()->get_class( 'review_request' )->request_review(); |
| 225 | 337 | } |
| 338 | + | |
| 339 | + } | |
| 340 | + | |
| 341 | + /** | |
| 342 | + * Get the current post type based on the screen that is viewed. | |
| 343 | + * | |
| 344 | + * @since 2.5.5 | |
| 345 | + * | |
| 346 | + * @return bool|string | |
| 347 | + */ | |
| 348 | + private function get_current_post_type() { | |
| 349 | + | |
| 350 | + return convertkit_get_current_screen( 'post_type' ); | |
| 226 | 351 | |
| 227 | 352 | } |
| 228 | 353 | |
| 229 | 354 | } |