← All changes
|
includes/blocks/class-convertkit-block-content.php
+74
-28
2.2.8
→
3.4.4
View file →
| @@ -37,8 +37,9 @@ | ||
| 37 | 37 | |
| 38 | 38 | /** |
| 39 | 39 | * This will register as: |
| 40 | 40 | * - a shortcode, with the name [convertkit_content] |
| 41 | + * - a shortcode, with the name [kit_content] | |
| 41 | 42 | * - a Gutenberg block, with the name convertkit/content |
| 42 | 43 | */ |
| 43 | 44 | return 'content'; |
| 44 | 45 | |
| @@ -44,8 +45,30 @@ | ||
| 44 | 45 | |
| 45 | 46 | } |
| 46 | 47 | |
| 47 | 48 | /** |
| 49 | + * Returns this block's title. | |
| 50 | + * | |
| 51 | + * @since 3.1.1 | |
| 52 | + */ | |
| 53 | + public function get_title() { | |
| 54 | + | |
| 55 | + return __( 'Kit Custom Content', 'convertkit' ); | |
| 56 | + | |
| 57 | + } | |
| 58 | + | |
| 59 | + /** | |
| 60 | + * Returns this block's icon. | |
| 61 | + * | |
| 62 | + * @since 3.1.1 | |
| 63 | + */ | |
| 64 | + public function get_icon() { | |
| 65 | + | |
| 66 | + return 'resources/backend/images/block-icon-content.svg'; | |
| 67 | + | |
| 68 | + } | |
| 69 | + | |
| 70 | + /** | |
| 48 | 71 | * Returns this block's Title, Icon, Categories, Keywords and properties. |
| 49 | 72 | * |
| 50 | 73 | * @since 1.9.6 |
| 51 | 74 | * |
| @@ -53,14 +76,15 @@ | ||
| 53 | 76 | */ |
| 54 | 77 | public function get_overview() { |
| 55 | 78 | |
| 56 | 79 | return array( |
| 57 | - 'title' => __( 'ConvertKit Custom Content', 'convertkit' ), | |
| 58 | - 'description' => __( 'Displays ConvertKit Custom Content for a subscriber if their tag matches the Page\'s tag.', 'convertkit' ), | |
| 59 | - 'icon' => 'resources/backend/images/block-icon-content.svg', | |
| 80 | + 'title' => $this->get_title(), | |
| 81 | + 'description' => __( 'Displays Kit Custom Content for a subscriber if their tag matches the Page\'s tag.', 'convertkit' ), | |
| 82 | + 'icon' => $this->get_icon(), | |
| 60 | 83 | 'category' => 'convertkit', |
| 61 | 84 | 'keywords' => array( |
| 62 | 85 | __( 'ConvertKit', 'convertkit' ), |
| 86 | + __( 'Kit', 'convertkit' ), | |
| 63 | 87 | __( 'Content', 'convertkit' ), |
| 64 | 88 | ), |
| 65 | 89 | |
| 66 | 90 | // TinyMCE / QuickTags Modal Width and Height. |
| @@ -102,13 +126,8 @@ | ||
| 102 | 126 | * @return bool|array |
| 103 | 127 | */ |
| 104 | 128 | public function get_fields() { |
| 105 | 129 | |
| 106 | - // Bail if the request is not for the WordPress Administration or frontend editor. | |
| 107 | - if ( ! WP_ConvertKit()->is_admin_or_frontend_editor() ) { | |
| 108 | - return false; | |
| 109 | - } | |
| 110 | - | |
| 111 | 130 | // Get ConvertKit Tags. |
| 112 | 131 | $tags = array(); |
| 113 | 132 | $convertkit_tags = new ConvertKit_Resource_Tags(); |
| 114 | 133 | if ( $convertkit_tags->exist() ) { |
| @@ -135,13 +154,8 @@ | ||
| 135 | 154 | * @return bool|array |
| 136 | 155 | */ |
| 137 | 156 | public function get_panels() { |
| 138 | 157 | |
| 139 | - // Bail if the request is not for the WordPress Administration or frontend editor. | |
| 140 | - if ( ! WP_ConvertKit()->is_admin_or_frontend_editor() ) { | |
| 141 | - return false; | |
| 142 | - } | |
| 143 | - | |
| 144 | 158 | return array( |
| 145 | 159 | 'general' => array( |
| 146 | 160 | 'label' => __( 'General', 'convertkit' ), |
| 147 | 161 | 'fields' => array( |
| @@ -195,9 +209,9 @@ | ||
| 195 | 209 | |
| 196 | 210 | // Bail if the tag isn't specified. |
| 197 | 211 | if ( ! $atts['tag'] ) { |
| 198 | 212 | if ( $settings->debug_enabled() ) { |
| 199 | - return '<!-- ConvertKit Custom Content: No Tag Specified -->'; | |
| 213 | + return '<!-- Kit Custom Content: No Tag Specified -->'; | |
| 200 | 214 | } |
| 201 | 215 | |
| 202 | 216 | return ''; |
| 203 | 217 | } |
| @@ -206,9 +220,9 @@ | ||
| 206 | 220 | $subscriber = new ConvertKit_Subscriber(); |
| 207 | 221 | $subscriber_id = $subscriber->get_subscriber_id(); |
| 208 | 222 | if ( is_wp_error( $subscriber_id ) ) { |
| 209 | 223 | if ( $settings->debug_enabled() ) { |
| 210 | - return '<!-- ConvertKit Custom Content: Subscriber ID Error: ' . $subscriber_id->get_error_message() . ' -->'; | |
| 224 | + return '<!-- Kit Custom Content: Subscriber ID Error: ' . $subscriber_id->get_error_message() . ' -->'; | |
| 211 | 225 | } |
| 212 | 226 | |
| 213 | 227 | return ''; |
| 214 | 228 | } |
| @@ -213,9 +227,9 @@ | ||
| 213 | 227 | return ''; |
| 214 | 228 | } |
| 215 | 229 | if ( ! $subscriber_id ) { |
| 216 | 230 | if ( $settings->debug_enabled() ) { |
| 217 | - return '<!-- ConvertKit Custom Content: Subscriber ID does not exist -->'; | |
| 231 | + return '<!-- Kit Custom Content: Subscriber ID does not exist -->'; | |
| 218 | 232 | } |
| 219 | 233 | |
| 220 | 234 | return ''; |
| 221 | 235 | } |
| @@ -221,11 +235,11 @@ | ||
| 221 | 235 | } |
| 222 | 236 | |
| 223 | 237 | // Bail if the API hasn't been configured. |
| 224 | 238 | $settings = new ConvertKit_Settings(); |
| 225 | - if ( ! $settings->has_api_key_and_secret() ) { | |
| 239 | + if ( ! $settings->has_access_and_refresh_token() ) { | |
| 226 | 240 | if ( $settings->debug_enabled() ) { |
| 227 | - return '<!-- ConvertKit Custom Content: No API Key and Secret -->'; | |
| 241 | + return '<!-- Kit Custom Content: No Access Token -->'; | |
| 228 | 242 | } |
| 229 | 243 | |
| 230 | 244 | return ''; |
| 231 | 245 | } |
| @@ -230,26 +244,58 @@ | ||
| 230 | 244 | return ''; |
| 231 | 245 | } |
| 232 | 246 | |
| 233 | 247 | // Initialize the API. |
| 234 | - $api = new ConvertKit_API( $settings->get_api_key(), $settings->get_api_secret(), $settings->debug_enabled(), 'output_content' ); | |
| 248 | + $api = new ConvertKit_API_V4( | |
| 249 | + CONVERTKIT_OAUTH_CLIENT_ID, | |
| 250 | + CONVERTKIT_OAUTH_CLIENT_REDIRECT_URI, | |
| 251 | + $settings->get_access_token(), | |
| 252 | + $settings->get_refresh_token(), | |
| 253 | + $settings->debug_enabled(), | |
| 254 | + 'output_content' | |
| 255 | + ); | |
| 235 | 256 | |
| 236 | 257 | // Get the subscriber's tags, to see if they subscribed to this tag. |
| 237 | - $tags = $api->get_subscriber_tags( $subscriber_id ); | |
| 258 | + if ( is_numeric( $subscriber_id ) ) { | |
| 259 | + $tags = $api->get_subscriber_tags( $subscriber_id ); | |
| 238 | 260 | |
| 239 | - // Bail if an error occured. | |
| 240 | - if ( is_wp_error( $tags ) ) { | |
| 241 | - if ( $settings->debug_enabled() ) { | |
| 242 | - return '<!-- ConvertKit Custom Content: ' . $tags->get_error_message() . ' -->'; | |
| 261 | + // Bail if an error occurred. | |
| 262 | + if ( is_wp_error( $tags ) ) { | |
| 263 | + if ( $settings->debug_enabled() ) { | |
| 264 | + return '<!-- Kit Custom Content: ' . $tags->get_error_message() . ' -->'; | |
| 265 | + } | |
| 266 | + | |
| 267 | + return ''; | |
| 243 | 268 | } |
| 269 | + } else { | |
| 270 | + // Subscriber ID is a signed subscriber ID. | |
| 271 | + // Get tags that the subscriber has access to via the profile() method. | |
| 272 | + $result = $api->profile( $subscriber_id ); | |
| 244 | 273 | |
| 245 | - return ''; | |
| 274 | + // If an error occurred, the subscriber ID is invalid. | |
| 275 | + if ( is_wp_error( $result ) ) { | |
| 276 | + if ( $settings->debug_enabled() ) { | |
| 277 | + return '<!-- Kit Custom Content: ' . $result->get_error_message() . ' -->'; | |
| 278 | + } | |
| 279 | + | |
| 280 | + return ''; | |
| 281 | + } | |
| 282 | + | |
| 283 | + // Build tags array. | |
| 284 | + $tags = array( | |
| 285 | + 'tags' => array(), | |
| 286 | + ); | |
| 287 | + foreach ( $result['tags'] as $tag_id ) { | |
| 288 | + $tags['tags'][] = array( | |
| 289 | + 'id' => $tag_id, | |
| 290 | + ); | |
| 291 | + } | |
| 246 | 292 | } |
| 247 | 293 | |
| 248 | 294 | // Bail if the subscriber has no tags. |
| 249 | 295 | if ( ! count( $tags ) ) { |
| 250 | 296 | if ( $settings->debug_enabled() ) { |
| 251 | - return '<!-- ConvertKit Custom Content: Subscriber has no tags -->'; | |
| 297 | + return '<!-- Kit Custom Content: Subscriber has no tags -->'; | |
| 252 | 298 | } |
| 253 | 299 | |
| 254 | 300 | return ''; |
| 255 | 301 | } |
| @@ -254,9 +300,9 @@ | ||
| 254 | 300 | return ''; |
| 255 | 301 | } |
| 256 | 302 | |
| 257 | 303 | // Iterate through ConvertKit Tags to find a match. |
| 258 | - foreach ( $tags as $tag ) { | |
| 304 | + foreach ( $tags['tags'] as $tag ) { | |
| 259 | 305 | // Skip if this ConvertKit Tag isn't the Tag specified in the block. |
| 260 | 306 | if ( absint( $tag['id'] ) !== absint( $atts['tag'] ) ) { |
| 261 | 307 | continue; |
| 262 | 308 | } |
| @@ -290,9 +336,9 @@ | ||
| 290 | 336 | } |
| 291 | 337 | |
| 292 | 338 | // If here, the subscriber does not have the block's tag. |
| 293 | 339 | if ( $settings->debug_enabled() ) { |
| 294 | - return '<!-- ConvertKit Custom Content: Subscriber does not have tag -->'; | |
| 340 | + return '<!-- Kit Custom Content: Subscriber does not have tag -->'; | |
| 295 | 341 | } |
| 296 | 342 | |
| 297 | 343 | return ''; |
| 298 | 344 | |