has_access_and_refresh_token() ) { $this->api = new ConvertKit_API_V4( CONVERTKIT_OAUTH_CLIENT_ID, CONVERTKIT_OAUTH_CLIENT_REDIRECT_URI, $settings->get_access_token(), $settings->get_refresh_token(), $settings->debug_enabled(), $context ); } // Get last query time and existing resources. $this->last_queried = get_option( $this->settings_name . '_last_queried' ); $this->resources = get_option( $this->settings_name ); } /** * Returns all non-legacy landing pages (v4 pages with an `embed_url` * property). Legacy landing pages have a `url` property instead and are * excluded here. * * @since 3.3.7 * * @return bool|array */ public function get_non_legacy() { $resources = $this->get(); if ( ! $resources ) { return false; } $non_legacy = array(); foreach ( $resources as $id => $landing_page ) { if ( isset( $landing_page['url'] ) ) { continue; } $non_legacy[ $id ] = $landing_page; } return $non_legacy; } /** * Returns whether any non-legacy landing pages exist in the options table. * * @since 3.3.7 * * @return bool */ public function non_legacy_exist() { return (bool) $this->get_non_legacy(); } /** * Determines whether the given identifier refers to a legacy landing page. * * Handles both storage shapes: a URL string (as saved by Plugin versions * < 1.9.6 which stored the landing page URL directly on the post), and a * numeric ID that resolves to a resource entry containing a `url` key. * * @since 3.3.7 * * @param int|string $id_or_url Landing Page ID or URL. * @return bool */ public function is_legacy( $id_or_url ) { // Bail if no value. if ( ! $id_or_url ) { return false; } // If the value is a URL string, it's a legacy landing page as stored // by Plugin versions < 1.9.6. if ( is_string( $id_or_url ) && strstr( $id_or_url, 'http' ) ) { return true; } // Otherwise resolve the ID against the cached resources. $landing_page = $this->get_by_id( (int) $id_or_url ); if ( ! $landing_page ) { return false; } // Legacy landing pages carry a `url` key; v4 pages carry `embed_url`. return isset( $landing_page['url'] ); } /** * Returns the HTML/JS markup for the given Landing Page ID * * @since 1.9.6 * * @param int|string $id Landing Page ID or Legacy Landing Page URL. * @return WP_Error|string */ public function get_html( $id ) { // Setup API. $settings = new ConvertKit_Settings(); $this->api = new ConvertKit_API_V4( CONVERTKIT_OAUTH_CLIENT_ID, CONVERTKIT_OAUTH_CLIENT_REDIRECT_URI, $settings->get_access_token(), $settings->get_refresh_token(), $settings->debug_enabled(), 'output_landing_page' ); // If the ID is a URL, this is a Legacy Landing Page defined for use on this Page // in a Plugin version < 1.9.6. // 1.9.6+ always uses a Landing Page ID. if ( strstr( $id, 'http' ) ) { // Return Legacy Landing Page HTML for url property. return $this->api->get_landing_page_html( $id, $settings->debug_enabled() ); } // Cast ID to integer. $id = absint( $id ); // Bail if the resources are a WP_Error. if ( is_wp_error( $this->resources ) ) { return $this->resources; } // Bail if the resource doesn't exist. if ( ! isset( $this->resources[ $id ] ) ) { return new WP_Error( 'convertkit_resource_landing_pages_get_html', sprintf( /* translators: ConvertKit Landing Page ID */ __( 'ConvertKit Landing Page ID %s does not exist on Kit.', 'convertkit' ), $id ) ); } // If the resource has a 'url' property, this is a Legacy Landing Page, and the 'url' should be used. if ( isset( $this->resources[ $id ]['url'] ) ) { // Return Legacy Landing Page HTML for url property. return $this->api->get_landing_page_html( $this->resources[ $id ]['url'], $settings->debug_enabled() ); } // Return Landing Page HTML for embed_url property. return $this->api->get_landing_page_html( $this->resources[ $id ]['embed_url'], $settings->debug_enabled() ); } /** * Replaces the favicon in the given Landing Page HTML with the site icon specified * in WordPress. * * If no site icon is specified in WordPress, returns the original Landing Page HTML. * * @since 2.3.0 * * @param string $html Landing Page HTML. * @return string Landing Page HTML */ public function replace_favicon( $html ) { // Get link rel and meta tags for site icon. $site_icon = $this->get_site_icon(); // Bail if no site icon specified. if ( empty( $site_icon ) ) { return $html; } // Define the ConvertKit favicon tag that exists in Landing Pages. $convertkit_favicon_tag = ''; // 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". if ( strpos( $html, $convertkit_favicon_tag ) === false ) { // Prepend the WordPress site icon tags imemdiately before the closing tag. $html = str_replace( '', $site_icon . "\n" . '', $html ); return $html; } // This is a standard landing page that contains link rel="shortcut icon". // Replace the link rel="shortcut icon" with the above. $html = str_replace( $convertkit_favicon_tag, $site_icon, $html ); // Return. return $html; } /** * Returns the output of the WordPress wp_site_icon() function, which returns * the necessary link rel and meta tags to display this site's favicon. * * If no site icon is specified in WordPress, returns a blank string. * * @since 2.3.0 * * @return string */ private function get_site_icon() { ob_start(); wp_site_icon(); return trim( ob_get_clean() ); } }