| 1 |
<?php |
| 2 |
/** |
| 3 |
* ConvertKit Forms Resource class. |
| 4 |
* |
| 5 |
* @package ConvertKit |
| 6 |
* @author ConvertKit |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Reads ConvertKit Forms from the options table, and refreshes |
| 11 |
* ConvertKit Forms data stored locally from the API. |
| 12 |
* |
| 13 |
* @since 2.2.7 |
| 14 |
*/ |
| 15 |
class ConvertKit_Resource_Creator_Network_Recommendations extends ConvertKit_Resource_V4 { |
| 16 |
|
| 17 |
/** |
| 18 |
* Holds the Settings Key that stores site wide ConvertKit settings |
| 19 |
* |
| 20 |
* @since 2.2.7 |
| 21 |
* |
| 22 |
* @var string |
| 23 |
*/ |
| 24 |
public $settings_name = 'convertkit_creator_network_recommendations'; |
| 25 |
|
| 26 |
/** |
| 27 |
* Constructor. |
| 28 |
* |
| 29 |
* @since 2.2.7 |
| 30 |
* |
| 31 |
* @param bool|string $context Context. |
| 32 |
*/ |
| 33 |
public function __construct( $context = false ) { |
| 34 |
|
| 35 |
// Initialize the API if the Access Token has been defined in the Plugin Settings. |
| 36 |
$settings = new ConvertKit_Settings(); |
| 37 |
if ( $settings->has_access_and_refresh_token() ) { |
| 38 |
$this->api = new ConvertKit_API_V4( |
| 39 |
CONVERTKIT_OAUTH_CLIENT_ID, |
| 40 |
CONVERTKIT_OAUTH_CLIENT_REDIRECT_URI, |
| 41 |
$settings->get_access_token(), |
| 42 |
$settings->get_refresh_token(), |
| 43 |
$settings->debug_enabled(), |
| 44 |
$context |
| 45 |
); |
| 46 |
} |
| 47 |
|
| 48 |
// Call parent initialization function. |
| 49 |
parent::init(); |
| 50 |
|
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Queries the API to determine whether the Creator Network Recommendation feature is enabled |
| 55 |
* on the ConvertKit account. |
| 56 |
* |
| 57 |
* If enabled, caches the script to use on the frontend site. |
| 58 |
* |
| 59 |
* @since 2.2.7 |
| 60 |
* |
| 61 |
* @return bool |
| 62 |
*/ |
| 63 |
public function enabled() { |
| 64 |
|
| 65 |
if ( ! $this->api ) { |
| 66 |
return false; |
| 67 |
} |
| 68 |
|
| 69 |
// Sanity check that we're using the ConvertKit WordPress Libraries 1.3.7 or higher. |
| 70 |
// If another ConvertKit Plugin is active and out of date, its libraries might |
| 71 |
// be loaded that don't have this method. |
| 72 |
if ( ! method_exists( $this->api, 'recommendations_script' ) ) { |
| 73 |
return false; |
| 74 |
} |
| 75 |
|
| 76 |
// Get script from API. |
| 77 |
$result = $this->api->recommendations_script(); |
| 78 |
|
| 79 |
// Bail if an error occurred. |
| 80 |
if ( is_wp_error( $result ) ) { |
| 81 |
delete_option( $this->settings_name ); |
| 82 |
return false; |
| 83 |
} |
| 84 |
|
| 85 |
// Bail if not enabled. |
| 86 |
if ( ! $result['enabled'] ) { |
| 87 |
delete_option( $this->settings_name ); |
| 88 |
return false; |
| 89 |
} |
| 90 |
|
| 91 |
// Store script URL, as Creator Network Recommendations are available on this account. |
| 92 |
update_option( $this->settings_name, $result['embed_js'] ); |
| 93 |
|
| 94 |
return true; |
| 95 |
|
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* |
| 100 |
* Returns the embed script, or false if no script exists i.e. Creator Network Recommendations |
| 101 |
* are disabled. |
| 102 |
* |
| 103 |
* Overrides the get() function of the ConvertKit_Resource class as we store a string |
| 104 |
* containing the embed script, not an array of data. |
| 105 |
* |
| 106 |
* @since 2.2.7 |
| 107 |
* |
| 108 |
* @return bool|string|array |
| 109 |
*/ |
| 110 |
public function get() { |
| 111 |
|
| 112 |
return get_option( $this->settings_name ); |
| 113 |
|
| 114 |
} |
| 115 |
|
| 116 |
} |
| 117 |
|