← All changes
|
includes/class-convertkit-resource-landing-pages.php
+171
-15
2.2.2
→
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,80 @@ | ||
| 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() ); | |
| 194 | + | |
| 195 | + } | |
| 196 | + | |
| 197 | + /** | |
| 198 | + * Replaces the favicon in the given Landing Page HTML with the site icon specified | |
| 199 | + * in WordPress. | |
| 200 | + * | |
| 201 | + * If no site icon is specified in WordPress, returns the original Landing Page HTML. | |
| 202 | + * | |
| 203 | + * @since 2.3.0 | |
| 204 | + * | |
| 205 | + * @param string $html Landing Page HTML. | |
| 206 | + * @return string Landing Page HTML | |
| 207 | + */ | |
| 208 | + public function replace_favicon( $html ) { | |
| 209 | + | |
| 210 | + // Get link rel and meta tags for site icon. | |
| 211 | + $site_icon = $this->get_site_icon(); | |
| 212 | + | |
| 213 | + // Bail if no site icon specified. | |
| 214 | + if ( empty( $site_icon ) ) { | |
| 215 | + return $html; | |
| 216 | + } | |
| 217 | + | |
| 218 | + // Define the ConvertKit favicon tag that exists in Landing Pages. | |
| 219 | + $convertkit_favicon_tag = '<link rel="shortcut icon" type="image/x-icon" href="https://pages.convertkit.com/templates/favicon.ico">'; | |
| 220 | + | |
| 221 | + // If the ConvertKit favicon tag does not exist in the HTML, this is a legacy landing page, which doesn't specify a link rel="shortcut icon". | |
| 222 | + if ( strpos( $html, $convertkit_favicon_tag ) === false ) { | |
| 223 | + // Prepend the WordPress site icon tags imemdiately before the closing </head> tag. | |
| 224 | + $html = str_replace( | |
| 225 | + '</head>', | |
| 226 | + $site_icon . "\n" . '</head>', | |
| 227 | + $html | |
| 228 | + ); | |
| 229 | + | |
| 230 | + return $html; | |
| 231 | + } | |
| 232 | + | |
| 233 | + // This is a standard landing page that contains link rel="shortcut icon". | |
| 234 | + // Replace the link rel="shortcut icon" with the above. | |
| 235 | + $html = str_replace( | |
| 236 | + $convertkit_favicon_tag, | |
| 237 | + $site_icon, | |
| 238 | + $html | |
| 239 | + ); | |
| 240 | + | |
| 241 | + // Return. | |
| 242 | + return $html; | |
| 243 | + | |
| 244 | + } | |
| 245 | + | |
| 246 | + /** | |
| 247 | + * Returns the output of the WordPress wp_site_icon() function, which returns | |
| 248 | + * the necessary link rel and meta tags to display this site's favicon. | |
| 249 | + * | |
| 250 | + * If no site icon is specified in WordPress, returns a blank string. | |
| 251 | + * | |
| 252 | + * @since 2.3.0 | |
| 253 | + * | |
| 254 | + * @return string | |
| 255 | + */ | |
| 256 | + private function get_site_icon() { | |
| 257 | + | |
| 258 | + ob_start(); | |
| 259 | + wp_site_icon(); | |
| 260 | + return trim( ob_get_clean() ); | |
| 105 | 261 | |
| 106 | 262 | } |
| 107 | 263 | |
| 108 | 264 | } |