| 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 1.9.6 |
| 14 |
*/ |
| 15 |
class ConvertKit_Resource_Forms extends ConvertKit_Resource { |
| 16 |
|
| 17 |
/** |
| 18 |
* Holds the Settings Key that stores site wide ConvertKit settings |
| 19 |
* |
| 20 |
* @var string |
| 21 |
*/ |
| 22 |
public $settings_name = 'convertkit_forms'; |
| 23 |
|
| 24 |
/** |
| 25 |
* The type of resource |
| 26 |
* |
| 27 |
* @var string |
| 28 |
*/ |
| 29 |
public $type = 'forms'; |
| 30 |
|
| 31 |
/** |
| 32 |
* Constructor. |
| 33 |
* |
| 34 |
* @since 1.9.8.4 |
| 35 |
* |
| 36 |
* @param bool|string $context Context. |
| 37 |
*/ |
| 38 |
public function __construct( $context = false ) { |
| 39 |
|
| 40 |
// Initialize the API if the API Key and Secret have been defined in the Plugin Settings. |
| 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(), |
| 46 |
$settings->debug_enabled(), |
| 47 |
$context |
| 48 |
); |
| 49 |
} |
| 50 |
|
| 51 |
// Call parent initialization function. |
| 52 |
parent::init(); |
| 53 |
|
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Returns all non-inline forms based on the sort order. |
| 58 |
* |
| 59 |
* @since 2.2.4 |
| 60 |
* |
| 61 |
* @return bool|array |
| 62 |
*/ |
| 63 |
public function get_non_inline() { |
| 64 |
|
| 65 |
// If the ConvertKit WordPress Libraries are < 1.3.6 (e.g. loaded by an outdated |
| 66 |
// addon), or a WordPress site updates this Plugin before other ConvertKit Plugins, |
| 67 |
// get_by() won't be available and will cause an E_ERROR, crashing the site. |
| 68 |
// @see https://wordpress.org/support/topic/error-1795/. |
| 69 |
if ( ! method_exists( $this, 'get_by' ) ) { |
| 70 |
return false; |
| 71 |
} |
| 72 |
|
| 73 |
return $this->get_by( 'format', array( 'modal', 'slide in', 'sticky bar' ) ); |
| 74 |
|
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
/** |
| 79 |
* Returns whether any non-inline forms exist in the options table. |
| 80 |
* |
| 81 |
* @since 2.2.4 |
| 82 |
* |
| 83 |
* @return bool |
| 84 |
*/ |
| 85 |
public function non_inline_exist() { |
| 86 |
|
| 87 |
if ( ! $this->get_non_inline() ) { |
| 88 |
return false; |
| 89 |
} |
| 90 |
|
| 91 |
return true; |
| 92 |
|
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Returns the HTML/JS markup for the given Form ID. |
| 97 |
* |
| 98 |
* Legacy Forms will return HTML. |
| 99 |
* Current Forms will return a <script> embed string. |
| 100 |
* |
| 101 |
* @since 1.9.6 |
| 102 |
* |
| 103 |
* @param int $id Form ID. |
| 104 |
* @return WP_Error|string |
| 105 |
*/ |
| 106 |
public function get_html( $id ) { |
| 107 |
|
| 108 |
// Cast ID to integer. |
| 109 |
$id = absint( $id ); |
| 110 |
|
| 111 |
// Bail if the resources are a WP_Error. |
| 112 |
if ( is_wp_error( $this->resources ) ) { |
| 113 |
return $this->resources; |
| 114 |
} |
| 115 |
|
| 116 |
// Bail if the resource doesn't exist. |
| 117 |
if ( ! isset( $this->resources[ $id ] ) ) { |
| 118 |
return new WP_Error( |
| 119 |
'convertkit_resource_forms_get_html', |
| 120 |
sprintf( |
| 121 |
/* translators: ConvertKit Form ID */ |
| 122 |
__( 'ConvertKit Form ID %s does not exist on ConvertKit.', 'convertkit' ), |
| 123 |
$id |
| 124 |
) |
| 125 |
); |
| 126 |
} |
| 127 |
|
| 128 |
// If no uid is present in the Form API data, this is a legacy form that's served by directly fetching the HTML |
| 129 |
// from forms.convertkit.com. |
| 130 |
if ( ! isset( $this->resources[ $id ]['uid'] ) ) { |
| 131 |
// Initialize Settings. |
| 132 |
$settings = new ConvertKit_Settings(); |
| 133 |
|
| 134 |
// Bail if no API Key is specified in the Plugin Settings. |
| 135 |
if ( ! $settings->has_api_key() ) { |
| 136 |
return new WP_Error( |
| 137 |
'convertkit_resource_forms_get_html', |
| 138 |
__( 'ConvertKit Legacy Form could not be fetched as no API Key specified in Plugin Settings', 'convertkit' ) |
| 139 |
); |
| 140 |
} |
| 141 |
|
| 142 |
// Initialize the API. |
| 143 |
$api = new ConvertKit_API( $settings->get_api_key(), $settings->get_api_secret(), $settings->debug_enabled(), 'output_form' ); |
| 144 |
|
| 145 |
// Return Legacy Form HTML. |
| 146 |
return $api->get_form_html( $id ); |
| 147 |
} |
| 148 |
|
| 149 |
// If the form's format is not an inline form, add the inline script before the closing </body> tag. |
| 150 |
// This prevents a modal form's overlay being constrained by the WordPress Theme's styles, |
| 151 |
// and accidentally embedding the same non-inline form twice, which would result in e.g. the same modal form |
| 152 |
// displaying twice. |
| 153 |
if ( $this->resources[ $id ]['format'] !== 'inline' ) { |
| 154 |
add_filter( |
| 155 |
'convertkit_output_scripts_footer', |
| 156 |
function ( $scripts ) use ( $id ) { |
| 157 |
|
| 158 |
$scripts[] = array( |
| 159 |
'async' => true, |
| 160 |
'data-uid' => $this->resources[ $id ]['uid'], |
| 161 |
'src' => $this->resources[ $id ]['embed_js'], |
| 162 |
); |
| 163 |
|
| 164 |
return $scripts; |
| 165 |
|
| 166 |
} |
| 167 |
); |
| 168 |
|
| 169 |
// Don't return a script for output, as it'll be output in the site's footer. |
| 170 |
return ''; |
| 171 |
} |
| 172 |
|
| 173 |
// If here, return Form <script> embed now, as we want the inline form to display at this specific point of the content. |
| 174 |
return '<script async data-uid="' . esc_attr( $this->resources[ $id ]['uid'] ) . '" src="' . esc_url( $this->resources[ $id ]['embed_js'] ) . '"></script>'; |
| 175 |
|
| 176 |
} |
| 177 |
|
| 178 |
} |
| 179 |
|