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