| 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 |
add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) ); |
| 27 |
add_action( 'save_post', array( $this, 'save_post_meta' ) ); |
| 28 |
|
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Enqueue JavaScript when editing a Page, Post or Custom Post Type that outputs |
| 33 |
* ConvertKit Plugin settings. |
| 34 |
* |
| 35 |
* @since 1.9.6.4 |
| 36 |
*/ |
| 37 |
public function enqueue_scripts() { |
| 38 |
|
| 39 |
// Enqueue Select2 JS. |
| 40 |
convertkit_select2_enqueue_scripts(); |
| 41 |
|
| 42 |
/** |
| 43 |
* Enqueue JavaScript when editing a Page, Post or Custom Post Type that outputs |
| 44 |
* ConvertKit Plugin settings. |
| 45 |
* |
| 46 |
* @since 1.9.6.4 |
| 47 |
*/ |
| 48 |
do_action( 'convertkit_admin_post_enqueue_scripts' ); |
| 49 |
|
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Enqueue CSS when editing a Page, Post or Custom Post Type that outputs |
| 54 |
* ConvertKit Plugin settings. |
| 55 |
* |
| 56 |
* @since 1.9.6.4 |
| 57 |
*/ |
| 58 |
public function enqueue_styles() { |
| 59 |
|
| 60 |
// Enqueue Select2 CSS. |
| 61 |
convertkit_select2_enqueue_styles(); |
| 62 |
|
| 63 |
/** |
| 64 |
* Enqueue CSS for the Settings Screen at Settings > ConvertKit |
| 65 |
* |
| 66 |
* @since 1.9.6.4 |
| 67 |
*/ |
| 68 |
do_action( 'convertkit_admin_post_enqueue_styles' ); |
| 69 |
|
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Adds a meta box for the given Post Type. |
| 74 |
* |
| 75 |
* @since 1.9.6 |
| 76 |
* |
| 77 |
* @param string $post_type Post Type. |
| 78 |
*/ |
| 79 |
public function add_meta_boxes( $post_type ) { |
| 80 |
|
| 81 |
// Don't register the meta box if this Post Type isn't supported. |
| 82 |
$supported_post_types = convertkit_get_supported_post_types(); |
| 83 |
if ( ! in_array( $post_type, $supported_post_types, true ) ) { |
| 84 |
return; |
| 85 |
} |
| 86 |
|
| 87 |
// Register Meta Box. |
| 88 |
add_meta_box( 'wp-convertkit-meta-box', __( 'ConvertKit', 'convertkit' ), array( $this, 'display_meta_box' ), $post_type, 'normal' ); |
| 89 |
|
| 90 |
// Enqueue JS and CSS. |
| 91 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); |
| 92 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_styles' ) ); |
| 93 |
|
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Outputs the meta box. |
| 98 |
* |
| 99 |
* @since 1.9.6 |
| 100 |
* |
| 101 |
* @param WP_Post $post The Post being edited. |
| 102 |
*/ |
| 103 |
public function display_meta_box( $post ) { |
| 104 |
|
| 105 |
// Don't register the meta box if this Post is the blog archive page. |
| 106 |
if ( $post->ID === get_option( 'page_for_posts' ) ) { |
| 107 |
return; |
| 108 |
} |
| 109 |
|
| 110 |
// Show a warning if the API credentials haven't been set. |
| 111 |
$settings = new ConvertKit_Settings(); |
| 112 |
if ( ! $settings->has_api_key_and_secret() ) { |
| 113 |
$post_type = get_post_type_object( $post->post_type ); |
| 114 |
include CONVERTKIT_PLUGIN_PATH . '/views/backend/post/no-api-key.php'; |
| 115 |
return; |
| 116 |
} |
| 117 |
|
| 118 |
// Fetch Post Settings, Forms, Landing Pages and Tags. |
| 119 |
$convertkit_post = new ConvertKit_Post( $post->ID ); |
| 120 |
$convertkit_forms = new ConvertKit_Resource_Forms(); |
| 121 |
$convertkit_landing_pages = new ConvertKit_Resource_Landing_Pages(); |
| 122 |
$convertkit_tags = new ConvertKit_Resource_Tags(); |
| 123 |
|
| 124 |
// Get settings page link. |
| 125 |
$settings_link = convertkit_get_settings_link(); |
| 126 |
|
| 127 |
// Load metabox view. |
| 128 |
include CONVERTKIT_PLUGIN_PATH . '/views/backend/post/meta-box.php'; |
| 129 |
|
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Save Post Settings. |
| 134 |
* |
| 135 |
* @since 1.9.6 |
| 136 |
* |
| 137 |
* @param int $post_id Post ID. |
| 138 |
*/ |
| 139 |
public function save_post_meta( $post_id ) { |
| 140 |
|
| 141 |
// Bail if this is an autosave. |
| 142 |
if ( wp_is_post_autosave( $post_id ) ) { |
| 143 |
return; |
| 144 |
} |
| 145 |
|
| 146 |
// Bail if this is a post revision. |
| 147 |
if ( wp_is_post_revision( $post_id ) ) { |
| 148 |
return; |
| 149 |
} |
| 150 |
|
| 151 |
// Bail if no nonce field exists. |
| 152 |
if ( ! isset( $_POST['wp-convertkit-save-meta-nonce'] ) ) { |
| 153 |
return; |
| 154 |
} |
| 155 |
|
| 156 |
// Bail if the nonce verification fails. |
| 157 |
if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['wp-convertkit-save-meta-nonce'] ) ), 'wp-convertkit-save-meta' ) ) { |
| 158 |
return; |
| 159 |
} |
| 160 |
|
| 161 |
// Bail if no ConvertKit settings were posted. |
| 162 |
if ( ! isset( $_POST['wp-convertkit'] ) ) { |
| 163 |
return; |
| 164 |
} |
| 165 |
|
| 166 |
// Build metadata. |
| 167 |
$meta = array( |
| 168 |
'form' => ( isset( $_POST['wp-convertkit']['form'] ) ? sanitize_text_field( wp_unslash( $_POST['wp-convertkit']['form'] ) ) : '-1' ), |
| 169 |
'landing_page' => ( isset( $_POST['wp-convertkit']['landing_page'] ) ? sanitize_text_field( wp_unslash( $_POST['wp-convertkit']['landing_page'] ) ) : '' ), |
| 170 |
'tag' => ( isset( $_POST['wp-convertkit']['tag'] ) ? sanitize_text_field( wp_unslash( $_POST['wp-convertkit']['tag'] ) ) : '' ), |
| 171 |
); |
| 172 |
|
| 173 |
// Save metadata. |
| 174 |
$convertkit_post = new ConvertKit_Post( $post_id ); |
| 175 |
$convertkit_post->save( $meta ); |
| 176 |
|
| 177 |
// If a Form or Landing Page was specified, request a review. |
| 178 |
// This can safely be called multiple times, as the review request |
| 179 |
// class will ensure once a review request is dismissed by the user, |
| 180 |
// it is never displayed again. |
| 181 |
if ( $meta['form'] || $meta['landing_page'] ) { |
| 182 |
WP_ConvertKit()->get_class( 'review_request' )->request_review(); |
| 183 |
} |
| 184 |
|
| 185 |
} |
| 186 |
|
| 187 |
} |
| 188 |
|