← All changes
|
includes/class-convertkit-resource-landing-pages.php
+104
-15
2.3.0
→
3.4.3
View file →
| @@ -11,9 +11,9 @@ | ||
| 11 | 11 | * ConvertKit Landing Pages data stored locally from the API. |
| 12 | 12 | * |
| 13 | 13 | * @since 1.9.6 |
| 14 | 14 | */ |
| 15 | -class ConvertKit_Resource_Landing_Pages extends ConvertKit_Resource { | |
| 15 | +class ConvertKit_Resource_Landing_Pages extends ConvertKit_Resource_V4 { | |
| 16 | 16 | |
| 17 | 17 | /** |
| 18 | 18 | * Holds the Settings Key that stores site wide ConvertKit settings |
| 19 | 19 | * |
| @@ -32,29 +32,110 @@ | ||
| 32 | 32 | * Constructor. |
| 33 | 33 | * |
| 34 | 34 | * @since 1.9.8.4 |
| 35 | 35 | * |
| 36 | - * @param bool|string $context Context. | |
| 36 | + * @param string $context Context. | |
| 37 | 37 | */ |
| 38 | - public function __construct( $context = false ) { | |
| 38 | + public function __construct( $context = 'landing_pages' ) { | |
| 39 | 39 | |
| 40 | - // Initialize the API if the API Key and Secret have been defined in the Plugin Settings. | |
| 40 | + // Initialize the API if the Access Token has been defined in the Plugin Settings. | |
| 41 | 41 | $settings = new ConvertKit_Settings(); |
| 42 | - if ( $settings->has_api_key_and_secret() ) { | |
| 43 | - $this->api = new ConvertKit_API( | |
| 44 | - $settings->get_api_key(), | |
| 45 | - $settings->get_api_secret(), | |
| 42 | + if ( $settings->has_access_and_refresh_token() ) { | |
| 43 | + $this->api = new ConvertKit_API_V4( | |
| 44 | + CONVERTKIT_OAUTH_CLIENT_ID, | |
| 45 | + CONVERTKIT_OAUTH_CLIENT_REDIRECT_URI, | |
| 46 | + $settings->get_access_token(), | |
| 47 | + $settings->get_refresh_token(), | |
| 46 | 48 | $settings->debug_enabled(), |
| 47 | 49 | $context |
| 48 | 50 | ); |
| 49 | 51 | } |
| 50 | 52 | |
| 51 | - // Call parent initialization function. | |
| 52 | - parent::init(); | |
| 53 | + // Get last query time and existing resources. | |
| 54 | + $this->last_queried = get_option( $this->settings_name . '_last_queried' ); | |
| 55 | + $this->resources = get_option( $this->settings_name ); | |
| 53 | 56 | |
| 54 | 57 | } |
| 55 | 58 | |
| 56 | 59 | /** |
| 60 | + * Returns all non-legacy landing pages (v4 pages with an `embed_url` | |
| 61 | + * property). Legacy landing pages have a `url` property instead and are | |
| 62 | + * excluded here. | |
| 63 | + * | |
| 64 | + * @since 3.3.7 | |
| 65 | + * | |
| 66 | + * @return bool|array | |
| 67 | + */ | |
| 68 | + public function get_non_legacy() { | |
| 69 | + | |
| 70 | + $resources = $this->get(); | |
| 71 | + if ( ! $resources ) { | |
| 72 | + return false; | |
| 73 | + } | |
| 74 | + | |
| 75 | + $non_legacy = array(); | |
| 76 | + foreach ( $resources as $id => $landing_page ) { | |
| 77 | + if ( isset( $landing_page['url'] ) ) { | |
| 78 | + continue; | |
| 79 | + } | |
| 80 | + $non_legacy[ $id ] = $landing_page; | |
| 81 | + } | |
| 82 | + | |
| 83 | + return $non_legacy; | |
| 84 | + | |
| 85 | + } | |
| 86 | + | |
| 87 | + /** | |
| 88 | + * Returns whether any non-legacy landing pages exist in the options table. | |
| 89 | + * | |
| 90 | + * @since 3.3.7 | |
| 91 | + * | |
| 92 | + * @return bool | |
| 93 | + */ | |
| 94 | + public function non_legacy_exist() { | |
| 95 | + | |
| 96 | + return (bool) $this->get_non_legacy(); | |
| 97 | + | |
| 98 | + } | |
| 99 | + | |
| 100 | + /** | |
| 101 | + * Determines whether the given identifier refers to a legacy landing page. | |
| 102 | + * | |
| 103 | + * Handles both storage shapes: a URL string (as saved by Plugin versions | |
| 104 | + * < 1.9.6 which stored the landing page URL directly on the post), and a | |
| 105 | + * numeric ID that resolves to a resource entry containing a `url` key. | |
| 106 | + * | |
| 107 | + * @since 3.3.7 | |
| 108 | + * | |
| 109 | + * @param int|string $id_or_url Landing Page ID or URL. | |
| 110 | + * @return bool | |
| 111 | + */ | |
| 112 | + public function is_legacy( $id_or_url ) { | |
| 113 | + | |
| 114 | + // Bail if no value. | |
| 115 | + if ( ! $id_or_url ) { | |
| 116 | + return false; | |
| 117 | + } | |
| 118 | + | |
| 119 | + // If the value is a URL string, it's a legacy landing page as stored | |
| 120 | + // by Plugin versions < 1.9.6. | |
| 121 | + if ( is_string( $id_or_url ) && strstr( $id_or_url, 'http' ) ) { | |
| 122 | + return true; | |
| 123 | + } | |
| 124 | + | |
| 125 | + // Otherwise resolve the ID against the cached resources. | |
| 126 | + $landing_page = $this->get_by_id( (int) $id_or_url ); | |
| 127 | + | |
| 128 | + if ( ! $landing_page ) { | |
| 129 | + return false; | |
| 130 | + } | |
| 131 | + | |
| 132 | + // Legacy landing pages carry a `url` key; v4 pages carry `embed_url`. | |
| 133 | + return isset( $landing_page['url'] ); | |
| 134 | + | |
| 135 | + } | |
| 136 | + | |
| 137 | + /** | |
| 57 | 138 | * Returns the HTML/JS markup for the given Landing Page ID |
| 58 | 139 | * |
| 59 | 140 | * @since 1.9.6 |
| 60 | 141 | * |
| @@ -63,9 +144,17 @@ | ||
| 63 | 144 | */ |
| 64 | 145 | public function get_html( $id ) { |
| 65 | 146 | |
| 66 | 147 | // Setup API. |
| 67 | - $api = new ConvertKit_API( false, false, true, 'output_landing_page' ); | |
| 148 | + $settings = new ConvertKit_Settings(); | |
| 149 | + $this->api = new ConvertKit_API_V4( | |
| 150 | + CONVERTKIT_OAUTH_CLIENT_ID, | |
| 151 | + CONVERTKIT_OAUTH_CLIENT_REDIRECT_URI, | |
| 152 | + $settings->get_access_token(), | |
| 153 | + $settings->get_refresh_token(), | |
| 154 | + $settings->debug_enabled(), | |
| 155 | + 'output_landing_page' | |
| 156 | + ); | |
| 68 | 157 | |
| 69 | 158 | // If the ID is a URL, this is a Legacy Landing Page defined for use on this Page |
| 70 | 159 | // in a Plugin version < 1.9.6. |
| 71 | 160 | // 1.9.6+ always uses a Landing Page ID. |
| @@ -70,9 +159,9 @@ | ||
| 70 | 159 | // in a Plugin version < 1.9.6. |
| 71 | 160 | // 1.9.6+ always uses a Landing Page ID. |
| 72 | 161 | if ( strstr( $id, 'http' ) ) { |
| 73 | 162 | // Return Legacy Landing Page HTML for url property. |
| 74 | - return $api->get_landing_page_html( $id ); | |
| 163 | + return $this->api->get_landing_page_html( $id, $settings->debug_enabled() ); | |
| 75 | 164 | } |
| 76 | 165 | |
| 77 | 166 | // Cast ID to integer. |
| 78 | 167 | $id = absint( $id ); |
| @@ -87,9 +176,9 @@ | ||
| 87 | 176 | return new WP_Error( |
| 88 | 177 | 'convertkit_resource_landing_pages_get_html', |
| 89 | 178 | sprintf( |
| 90 | 179 | /* translators: ConvertKit Landing Page ID */ |
| 91 | - __( 'ConvertKit Landing Page ID %s does not exist on ConvertKit.', 'convertkit' ), | |
| 180 | + __( 'ConvertKit Landing Page ID %s does not exist on Kit.', 'convertkit' ), | |
| 92 | 181 | $id |
| 93 | 182 | ) |
| 94 | 183 | ); |
| 95 | 184 | } |
| @@ -96,13 +185,13 @@ | ||
| 96 | 185 | |
| 97 | 186 | // If the resource has a 'url' property, this is a Legacy Landing Page, and the 'url' should be used. |
| 98 | 187 | if ( isset( $this->resources[ $id ]['url'] ) ) { |
| 99 | 188 | // Return Legacy Landing Page HTML for url property. |
| 100 | - return $api->get_landing_page_html( $this->resources[ $id ]['url'] ); | |
| 189 | + return $this->api->get_landing_page_html( $this->resources[ $id ]['url'], $settings->debug_enabled() ); | |
| 101 | 190 | } |
| 102 | 191 | |
| 103 | 192 | // Return Landing Page HTML for embed_url property. |
| 104 | - return $api->get_landing_page_html( $this->resources[ $id ]['embed_url'] ); | |
| 193 | + return $this->api->get_landing_page_html( $this->resources[ $id ]['embed_url'], $settings->debug_enabled() ); | |
| 105 | 194 | |
| 106 | 195 | } |
| 107 | 196 | |
| 108 | 197 | /** |