| 1 |
<?php |
| 2 |
/** |
| 3 |
* ConvertKit Post Types class. |
| 4 |
* |
| 5 |
* @package ConvertKit |
| 6 |
* @author ConvertKit |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Class to register the Product Custom Post Type, and store |
| 11 |
* Products in the Custom Post Type, permitting both Gutenberg's |
| 12 |
* LinkControl and Classic Editor link functionality to display |
| 13 |
* ConvertKit Products as results when searching for items to link text to. |
| 14 |
* |
| 15 |
* Also adds data-commerce attributes to any links pointing to a ConvertKit |
| 16 |
* Product URL. |
| 17 |
* |
| 18 |
* @since 2.0.0 |
| 19 |
*/ |
| 20 |
class ConvertKit_Post_Type_Product { |
| 21 |
|
| 22 |
/** |
| 23 |
* Holds the Post Type name. |
| 24 |
* |
| 25 |
* @since 2.0.0 |
| 26 |
* |
| 27 |
* @var string |
| 28 |
*/ |
| 29 |
private $post_type_name = 'convertkit_product'; |
| 30 |
|
| 31 |
/** |
| 32 |
* Constructor. |
| 33 |
* |
| 34 |
* @since 2.0.0 |
| 35 |
*/ |
| 36 |
public function __construct() { |
| 37 |
|
| 38 |
// Register Custom Post Type. |
| 39 |
add_action( 'init', array( $this, 'register' ) ); |
| 40 |
|
| 41 |
// Register commerce.js. |
| 42 |
add_action( 'init', array( $this, 'register_commerce_script' ) ); |
| 43 |
|
| 44 |
// Update Products stored in Custom Post Type when Products Resource is refreshed. |
| 45 |
add_action( 'convertkit_resource_refreshed_products', array( $this, 'refresh' ) ); |
| 46 |
|
| 47 |
// Change Product Custom Post link to the ConvertKit Product's URL. |
| 48 |
add_filter( 'post_type_link', array( $this, 'filter_permalink' ), 10, 2 ); |
| 49 |
|
| 50 |
// Adds the data-commerce attribute to HTML links that link to a ConvertKit Product. |
| 51 |
add_filter( 'the_content', array( $this, 'add_data_commerce_to_permalink' ) ); |
| 52 |
|
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Registers the ConvertKit Product Custom Post Type. |
| 57 |
* |
| 58 |
* @since 2.0.0 |
| 59 |
*/ |
| 60 |
public function register() { |
| 61 |
|
| 62 |
// Define Post Type arguments. |
| 63 |
$args = array( |
| 64 |
'labels' => array( |
| 65 |
'name' => __( 'ConvertKit Products', 'convertkit' ), |
| 66 |
'singular_name' => __( 'ConvertKit Product', 'convertkit' ), |
| 67 |
'menu_name' => __( 'ConvertKit Products', 'convertkit' ), |
| 68 |
'add_new' => __( 'Add New', 'convertkit' ), |
| 69 |
'add_new_item' => __( 'Add New ConvertKit Product', 'convertkit' ), |
| 70 |
'edit_item' => __( 'Edit ConvertKit Product', 'convertkit' ), |
| 71 |
'new_item' => __( 'New ConvertKit Product', 'convertkit' ), |
| 72 |
'view_item' => __( 'View ConvertKit Product', 'convertkit' ), |
| 73 |
'search_items' => __( 'Search ConvertKit Products', 'convertkit' ), |
| 74 |
'not_found' => __( 'No ConvertKit Products found', 'convertkit' ), |
| 75 |
'not_found_in_trash' => __( 'No ConvertKit Products found in Trash', 'convertkit' ), |
| 76 |
'parent_item_colon' => '', |
| 77 |
), |
| 78 |
'description' => __( 'ConvertKit Products', 'convertkit' ), |
| 79 |
'public' => true, |
| 80 |
'publicly_queryable' => false, |
| 81 |
'exclude_from_search' => true, |
| 82 |
'show_ui' => false, |
| 83 |
'show_in_menu' => false, |
| 84 |
'capability_type' => 'page', |
| 85 |
'hierarchical' => false, |
| 86 |
'supports' => array( 'title' ), |
| 87 |
'has_archive' => false, |
| 88 |
'show_in_nav_menus' => false, |
| 89 |
'show_in_rest' => true, |
| 90 |
); |
| 91 |
|
| 92 |
/** |
| 93 |
* Filter the arguments for registering the Products Custom Post Type |
| 94 |
* |
| 95 |
* @since 2.0.0 |
| 96 |
* |
| 97 |
* @param array $args register_post_type() compatible arguments. |
| 98 |
*/ |
| 99 |
$args = apply_filters( 'convertkit_post_type_product_register', $args ); |
| 100 |
|
| 101 |
// Register Post Type. |
| 102 |
register_post_type( $this->post_type_name, $args ); |
| 103 |
|
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Registers the commerce.js script. |
| 108 |
* |
| 109 |
* @since 2.0.0 |
| 110 |
*/ |
| 111 |
public function register_commerce_script() { |
| 112 |
|
| 113 |
// Get Products Resource class. |
| 114 |
$products = new ConvertKit_Resource_Products(); |
| 115 |
|
| 116 |
// Get commerce.js URL. |
| 117 |
$url = $products->get_commerce_js_url(); |
| 118 |
|
| 119 |
// If no URL exists, bail. |
| 120 |
if ( ! $url ) { |
| 121 |
return; |
| 122 |
} |
| 123 |
|
| 124 |
// Enqueue JS. |
| 125 |
wp_register_script( 'convertkit-commerce', $url, array(), CONVERTKIT_PLUGIN_VERSION, true ); |
| 126 |
|
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Update the Custom Post Type's Products based on the supplied array |
| 131 |
* of ConvertKit Products, when the Resource class performs a refresh. |
| 132 |
* |
| 133 |
* @since 2.0.0 |
| 134 |
* |
| 135 |
* @param array $products Products. |
| 136 |
*/ |
| 137 |
public function refresh( $products ) { |
| 138 |
|
| 139 |
// Delete all Products in the Custom Post Type. |
| 140 |
$this->delete_all(); |
| 141 |
|
| 142 |
// If no Products exist, bail. |
| 143 |
if ( ! $products ) { |
| 144 |
return; |
| 145 |
} |
| 146 |
|
| 147 |
// Create/update Products in the Custom Post Type. |
| 148 |
foreach ( $products as $product ) { |
| 149 |
$this->store( $product ); |
| 150 |
} |
| 151 |
|
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Returns the ConvertKit Product URL instead of the Product Custom Post URL |
| 156 |
* when a call to e.g. get_permalink() is made. |
| 157 |
* |
| 158 |
* @since 2.0.0 |
| 159 |
* |
| 160 |
* @param string $url URL. |
| 161 |
* @param WP_Post $post Product Custom Post. |
| 162 |
* @return string URL |
| 163 |
*/ |
| 164 |
public function filter_permalink( $url, $post ) { |
| 165 |
|
| 166 |
// Don't filter if the Post's type isn't a ConvertKit Product. |
| 167 |
if ( $post->post_type !== $this->post_type_name ) { |
| 168 |
return $url; |
| 169 |
} |
| 170 |
|
| 171 |
// Return ConvertKit Product URL. |
| 172 |
return get_post_meta( $post->ID, 'url', true ); |
| 173 |
|
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Adds the data-commerce attribute to HTML links that link to a ConvertKit Product. |
| 178 |
* |
| 179 |
* @since 2.0.0 |
| 180 |
* |
| 181 |
* @param string $content Page/Post Content. |
| 182 |
* @return string Page/Post Content |
| 183 |
*/ |
| 184 |
public function add_data_commerce_to_permalink( $content ) { |
| 185 |
|
| 186 |
// Get Products. |
| 187 |
$products = new ConvertKit_Resource_Products(); |
| 188 |
|
| 189 |
// Return content, unedited, if no Products exist. |
| 190 |
if ( ! $products->exist() ) { |
| 191 |
return $content; |
| 192 |
} |
| 193 |
|
| 194 |
// For each Product, add the data-commerce tag to any href attributes that link to a ConvertKit Product. |
| 195 |
foreach ( $products->get() as $product ) { |
| 196 |
// Skip if this Product's URL does not exist in the content. |
| 197 |
if ( strpos( $content, $product['url'] ) === false ) { |
| 198 |
continue; |
| 199 |
} |
| 200 |
|
| 201 |
// Enqueue commerce.js. |
| 202 |
if ( ! wp_script_is( 'convertkit-commerce', 'enqueued' ) ) { |
| 203 |
wp_enqueue_script( 'convertkit-commerce' ); |
| 204 |
} |
| 205 |
|
| 206 |
// Add data-commerce attribute. |
| 207 |
$content = str_replace( 'href="' . esc_attr( $product['url'] ) . '"', 'href="' . esc_attr( $product['url'] ) . '" data-commerce', $content ); |
| 208 |
} |
| 209 |
|
| 210 |
// Return content. |
| 211 |
return $content; |
| 212 |
|
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Stores the Product in the Custom Post Type, either creating or updating it |
| 217 |
* depending on whether the Product already exists in the Custom Post Type. |
| 218 |
* |
| 219 |
* @since 2.0.0 |
| 220 |
* |
| 221 |
* @param array $product ConvertKit Product. |
| 222 |
* @return WP_Error|int Error or Post ID. |
| 223 |
*/ |
| 224 |
private function store( $product ) { |
| 225 |
|
| 226 |
// Check if the Product already exists in the Custom Post Type. |
| 227 |
$existing_product = new WP_Query( |
| 228 |
array( |
| 229 |
'post_type' => $this->post_type_name, |
| 230 |
'post_status' => 'publish', |
| 231 |
'meta_query' => array( |
| 232 |
array( |
| 233 |
'key' => 'id', |
| 234 |
'value' => $product['id'], |
| 235 |
), |
| 236 |
), |
| 237 |
'fields' => 'ids', |
| 238 |
) |
| 239 |
); |
| 240 |
|
| 241 |
// If the Product does not exist in the Custom Post Type, create it now. |
| 242 |
if ( ! count( $existing_product->posts ) ) { |
| 243 |
return wp_insert_post( |
| 244 |
array( |
| 245 |
'post_type' => $this->post_type_name, |
| 246 |
'post_title' => $product['name'], |
| 247 |
'post_status' => 'publish', |
| 248 |
'meta_input' => array( |
| 249 |
'id' => $product['id'], |
| 250 |
'url' => $product['url'], |
| 251 |
), |
| 252 |
), |
| 253 |
true |
| 254 |
); |
| 255 |
} |
| 256 |
|
| 257 |
// The Product exists in the Custom Post Type; update it now. |
| 258 |
return wp_update_post( |
| 259 |
array( |
| 260 |
'ID' => $existing_product->posts[0], |
| 261 |
'post_title' => $product['name'], |
| 262 |
'meta_input' => array( |
| 263 |
'id' => $product['id'], |
| 264 |
'url' => $product['url'], |
| 265 |
), |
| 266 |
), |
| 267 |
true |
| 268 |
); |
| 269 |
|
| 270 |
} |
| 271 |
|
| 272 |
/** |
| 273 |
* Deletes all Products from the Custom Post Type. |
| 274 |
* |
| 275 |
* @since 2.0.0 |
| 276 |
*/ |
| 277 |
private function delete_all() { |
| 278 |
|
| 279 |
global $wpdb; |
| 280 |
|
| 281 |
$wpdb->query( |
| 282 |
$wpdb->prepare( |
| 283 |
'DELETE FROM wp_posts WHERE post_type=%s', |
| 284 |
$this->post_type_name |
| 285 |
) |
| 286 |
); |
| 287 |
|
| 288 |
} |
| 289 |
|
| 290 |
} |
| 291 |
|