| 1 |
<?php |
| 2 |
/** |
| 3 |
* ConvertKit Landing Pages Resource class. |
| 4 |
* |
| 5 |
* @package ConvertKit |
| 6 |
* @author ConvertKit |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Reads ConvertKit Landing Pages from the options table, and refreshes |
| 11 |
* ConvertKit Landing Pages data stored locally from the API. |
| 12 |
* |
| 13 |
* @since 1.9.6 |
| 14 |
*/ |
| 15 |
class ConvertKit_Resource_Landing_Pages extends ConvertKit_Resource_V4 { |
| 16 |
|
| 17 |
/** |
| 18 |
* Holds the Settings Key that stores site wide ConvertKit settings |
| 19 |
* |
| 20 |
* @var string |
| 21 |
*/ |
| 22 |
public $settings_name = 'convertkit_landing_pages'; |
| 23 |
|
| 24 |
/** |
| 25 |
* The type of resource |
| 26 |
* |
| 27 |
* @var string |
| 28 |
*/ |
| 29 |
public $type = 'landing_pages'; |
| 30 |
|
| 31 |
/** |
| 32 |
* Constructor. |
| 33 |
* |
| 34 |
* @since 1.9.8.4 |
| 35 |
* |
| 36 |
* @param string $context Context. |
| 37 |
*/ |
| 38 |
public function __construct( $context = 'landing_pages' ) { |
| 39 |
|
| 40 |
// Initialize the API if the Access Token has been defined in the Plugin Settings. |
| 41 |
$settings = new ConvertKit_Settings(); |
| 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(), |
| 48 |
$settings->debug_enabled(), |
| 49 |
$context |
| 50 |
); |
| 51 |
} |
| 52 |
|
| 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 ); |
| 56 |
|
| 57 |
} |
| 58 |
|
| 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 |
/** |
| 138 |
* Returns the HTML/JS markup for the given Landing Page ID |
| 139 |
* |
| 140 |
* @since 1.9.6 |
| 141 |
* |
| 142 |
* @param int|string $id Landing Page ID or Legacy Landing Page URL. |
| 143 |
* @return WP_Error|string |
| 144 |
*/ |
| 145 |
public function get_html( $id ) { |
| 146 |
|
| 147 |
// Setup API. |
| 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 |
); |
| 157 |
|
| 158 |
// If the ID is a URL, this is a Legacy Landing Page defined for use on this Page |
| 159 |
// in a Plugin version < 1.9.6. |
| 160 |
// 1.9.6+ always uses a Landing Page ID. |
| 161 |
if ( strstr( $id, 'http' ) ) { |
| 162 |
// Return Legacy Landing Page HTML for url property. |
| 163 |
return $this->api->get_landing_page_html( $id, $settings->debug_enabled() ); |
| 164 |
} |
| 165 |
|
| 166 |
// Cast ID to integer. |
| 167 |
$id = absint( $id ); |
| 168 |
|
| 169 |
// Bail if the resources are a WP_Error. |
| 170 |
if ( is_wp_error( $this->resources ) ) { |
| 171 |
return $this->resources; |
| 172 |
} |
| 173 |
|
| 174 |
// Bail if the resource doesn't exist. |
| 175 |
if ( ! isset( $this->resources[ $id ] ) ) { |
| 176 |
return new WP_Error( |
| 177 |
'convertkit_resource_landing_pages_get_html', |
| 178 |
sprintf( |
| 179 |
/* translators: ConvertKit Landing Page ID */ |
| 180 |
__( 'ConvertKit Landing Page ID %s does not exist on Kit.', 'convertkit' ), |
| 181 |
$id |
| 182 |
) |
| 183 |
); |
| 184 |
} |
| 185 |
|
| 186 |
// If the resource has a 'url' property, this is a Legacy Landing Page, and the 'url' should be used. |
| 187 |
if ( isset( $this->resources[ $id ]['url'] ) ) { |
| 188 |
// Return Legacy Landing Page HTML for url property. |
| 189 |
return $this->api->get_landing_page_html( $this->resources[ $id ]['url'], $settings->debug_enabled() ); |
| 190 |
} |
| 191 |
|
| 192 |
// Return Landing Page HTML for embed_url property. |
| 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() ); |
| 261 |
|
| 262 |
} |
| 263 |
|
| 264 |
} |
| 265 |
|