| 1 |
<?php |
| 2 |
/** |
| 3 |
* ConvertKit Plugin Sidebar Post Settings class. |
| 4 |
* |
| 5 |
* @package ConvertKit |
| 6 |
* @author ConvertKit |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* ConvertKit Plugin Sidebar Post Settings definition for Gutenberg. |
| 11 |
* |
| 12 |
* @package ConvertKit |
| 13 |
* @author ConvertKit |
| 14 |
*/ |
| 15 |
class ConvertKit_Plugin_Sidebar_Post_Settings extends ConvertKit_Plugin_Sidebar { |
| 16 |
|
| 17 |
/** |
| 18 |
* Constructor |
| 19 |
* |
| 20 |
* @since 3.3.0 |
| 21 |
*/ |
| 22 |
public function __construct() { |
| 23 |
|
| 24 |
// Register this as a plugin sidebar in the ConvertKit Plugin. |
| 25 |
add_filter( 'convertkit_plugin_sidebars', array( $this, 'register' ) ); |
| 26 |
|
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Returns this plugin sidebar's programmatic name, excluding the convertkit- prefix. |
| 31 |
* |
| 32 |
* @since 3.3.0 |
| 33 |
* |
| 34 |
* @return string |
| 35 |
*/ |
| 36 |
public function get_name() { |
| 37 |
|
| 38 |
return 'post-settings'; |
| 39 |
|
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Returns this plugin sidebar's meta key. |
| 44 |
* |
| 45 |
* @since 3.3.0 |
| 46 |
* |
| 47 |
* @return string |
| 48 |
*/ |
| 49 |
public function get_meta_key() { |
| 50 |
|
| 51 |
return '_wp_convertkit_post_meta'; |
| 52 |
|
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Returns this plugin sidebar's title. |
| 57 |
* |
| 58 |
* @since 3.3.0 |
| 59 |
*/ |
| 60 |
public function get_title() { |
| 61 |
|
| 62 |
return __( 'Kit', 'convertkit' ); |
| 63 |
|
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Returns this plugin sidebar's icon. |
| 68 |
* |
| 69 |
* @since 3.3.0 |
| 70 |
*/ |
| 71 |
public function get_icon() { |
| 72 |
|
| 73 |
return 'resources/backend/images/kit-logo.svg'; |
| 74 |
|
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Returns this plugin sidebar's attributes |
| 79 |
* |
| 80 |
* @since 3.3.0 |
| 81 |
* |
| 82 |
* @return array |
| 83 |
*/ |
| 84 |
public function get_attributes() { |
| 85 |
|
| 86 |
return array( |
| 87 |
'form' => array( |
| 88 |
'type' => 'string', |
| 89 |
'default' => $this->get_default_value( 'form' ), |
| 90 |
), |
| 91 |
'landing_page' => array( |
| 92 |
'type' => 'string', |
| 93 |
'default' => $this->get_default_value( 'landing_page' ), |
| 94 |
), |
| 95 |
'tag' => array( |
| 96 |
'type' => 'string', |
| 97 |
'default' => $this->get_default_value( 'tag' ), |
| 98 |
), |
| 99 |
'restrict_content' => array( |
| 100 |
'type' => 'string', |
| 101 |
'default' => $this->get_default_value( 'restrict_content' ), |
| 102 |
), |
| 103 |
); |
| 104 |
|
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Returns this plugin sidebar's Fields |
| 109 |
* |
| 110 |
* @since 3.3.0 |
| 111 |
* |
| 112 |
* @return bool|array |
| 113 |
*/ |
| 114 |
public function get_fields() { |
| 115 |
|
| 116 |
// Load resource classes. |
| 117 |
$convertkit_forms = new ConvertKit_Resource_Forms( 'post_settings' ); |
| 118 |
$convertkit_landing_pages = new ConvertKit_Resource_Landing_Pages( 'post_settings' ); |
| 119 |
$convertkit_tags = new ConvertKit_Resource_Tags( 'post_settings' ); |
| 120 |
$convertkit_products = new ConvertKit_Resource_Products( 'post_settings' ); |
| 121 |
|
| 122 |
// Get Forms. Non-legacy forms populate the dropdown; legacy forms are |
| 123 |
// exposed separately as a fallback so a previously-saved legacy form |
| 124 |
// remains visible as the current selection without offering other |
| 125 |
// legacy forms as new choices. |
| 126 |
$forms = array( |
| 127 |
'-1' => esc_html__( 'Default', 'convertkit' ), |
| 128 |
'0' => esc_html__( 'None', 'convertkit' ), |
| 129 |
); |
| 130 |
$legacy_forms = array(); |
| 131 |
if ( $convertkit_forms->exist() ) { |
| 132 |
foreach ( $convertkit_forms->get() as $form ) { |
| 133 |
$label = sprintf( |
| 134 |
'%s [%s]', |
| 135 |
sanitize_text_field( $form['name'] ), |
| 136 |
// Legacy forms don't include a `format` key, so define them as inline. |
| 137 |
( ! empty( $form['format'] ) ? sanitize_text_field( $form['format'] ) : 'inline' ) |
| 138 |
); |
| 139 |
|
| 140 |
if ( ! empty( $form['format'] ) ) { |
| 141 |
$forms[ absint( $form['id'] ) ] = $label; |
| 142 |
} else { |
| 143 |
$legacy_forms[ absint( $form['id'] ) ] = $label; |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
// Get Landing Pages. Non-legacy pages populate the dropdown; legacy |
| 149 |
// pages are exposed separately as a fallback so a previously-saved |
| 150 |
// legacy landing page remains visible as the current selection |
| 151 |
// without offering other legacy pages as new choices. |
| 152 |
$landing_pages = array( |
| 153 |
'0' => esc_html__( 'None', 'convertkit' ), |
| 154 |
); |
| 155 |
$legacy_landing_pages = array(); |
| 156 |
if ( $convertkit_landing_pages->exist() ) { |
| 157 |
foreach ( $convertkit_landing_pages->get() as $landing_page ) { |
| 158 |
if ( isset( $landing_page['url'] ) ) { |
| 159 |
$legacy_landing_pages[ absint( $landing_page['id'] ) ] = sanitize_text_field( $landing_page['name'] ); |
| 160 |
} else { |
| 161 |
$landing_pages[ absint( $landing_page['id'] ) ] = sanitize_text_field( $landing_page['name'] ); |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
// Get Tags. |
| 167 |
$tags = array( |
| 168 |
'0' => esc_html__( 'None', 'convertkit' ), |
| 169 |
); |
| 170 |
if ( $convertkit_tags->exist() ) { |
| 171 |
foreach ( $convertkit_tags->get() as $tag ) { |
| 172 |
$tags[ absint( $tag['id'] ) ] = sanitize_text_field( $tag['name'] ); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
// Get Products. |
| 177 |
$restrict_content = array( |
| 178 |
'0' => esc_html__( 'Do not restrict content to member-only', 'convertkit' ), |
| 179 |
); |
| 180 |
$restrict_content_legacy_forms = array(); |
| 181 |
if ( $convertkit_forms->exist() ) { |
| 182 |
$restrict_content['forms'] = array( |
| 183 |
'label' => esc_html__( 'Forms', 'convertkit' ), |
| 184 |
'values' => array(), |
| 185 |
); |
| 186 |
foreach ( $convertkit_forms->get() as $form ) { |
| 187 |
$key = 'form_' . absint( $form['id'] ); |
| 188 |
$label = sprintf( |
| 189 |
'%s [%s]', |
| 190 |
sanitize_text_field( $form['name'] ), |
| 191 |
// Legacy forms don't include a `format` key, so define them as inline. |
| 192 |
( ! empty( $form['format'] ) ? sanitize_text_field( $form['format'] ) : 'inline' ) |
| 193 |
); |
| 194 |
|
| 195 |
if ( ! empty( $form['format'] ) ) { |
| 196 |
$restrict_content['forms']['values'][ $key ] = $label; |
| 197 |
} else { |
| 198 |
$restrict_content_legacy_forms[ $key ] = $label; |
| 199 |
} |
| 200 |
} |
| 201 |
} |
| 202 |
if ( $convertkit_tags->exist() ) { |
| 203 |
$restrict_content['tags'] = array( |
| 204 |
'label' => esc_html__( 'Tags', 'convertkit' ), |
| 205 |
'values' => array(), |
| 206 |
); |
| 207 |
foreach ( $convertkit_tags->get() as $tag ) { |
| 208 |
$restrict_content['tags']['values'][ 'tag_' . absint( $tag['id'] ) ] = sanitize_text_field( $tag['name'] ); |
| 209 |
} |
| 210 |
} |
| 211 |
if ( $convertkit_products->exist() ) { |
| 212 |
$restrict_content['products'] = array( |
| 213 |
'label' => esc_html__( 'Products', 'convertkit' ), |
| 214 |
'values' => array(), |
| 215 |
); |
| 216 |
foreach ( $convertkit_products->get() as $product ) { |
| 217 |
$restrict_content['products']['values'][ 'product_' . $product['id'] ] = sanitize_text_field( $product['name'] ); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
return array( |
| 222 |
'form' => array( |
| 223 |
'label' => __( 'Form', 'convertkit' ), |
| 224 |
'type' => 'select', |
| 225 |
'description' => array( |
| 226 |
sprintf( |
| 227 |
'<code>%s</code>: %s <a href="%s" target="_blank">%s</a>', |
| 228 |
esc_html__( 'Default', 'convertkit' ), |
| 229 |
esc_html__( 'Uses the form specified on the', 'convertkit' ), |
| 230 |
esc_url( convertkit_get_settings_link() ), |
| 231 |
esc_html__( 'settings page', 'convertkit' ) |
| 232 |
), |
| 233 |
sprintf( |
| 234 |
'<code>%s</code>: %s', |
| 235 |
esc_html__( 'None', 'convertkit' ), |
| 236 |
esc_html__( 'do not display a form.', 'convertkit' ) |
| 237 |
), |
| 238 |
__( 'Any other option will display that form after the main content.', 'convertkit' ), |
| 239 |
sprintf( |
| 240 |
'%s <a href="%s" target="_blank">%s</a>', |
| 241 |
esc_html__( 'To make changes to your forms,', 'convertkit' ), |
| 242 |
esc_url( convertkit_get_sign_in_url() ), |
| 243 |
esc_html__( 'sign in to Kit', 'convertkit' ) |
| 244 |
), |
| 245 |
), |
| 246 |
'values' => $forms, |
| 247 |
'legacy_values' => $legacy_forms, |
| 248 |
'resource_type' => 'forms', |
| 249 |
), |
| 250 |
'landing_page' => array( |
| 251 |
'label' => __( 'Landing Page', 'convertkit' ), |
| 252 |
'type' => 'select', |
| 253 |
'description' => array( |
| 254 |
esc_html__( 'Select a landing page to make it appear in place of this page.', 'convertkit' ), |
| 255 |
sprintf( |
| 256 |
/* translators: Link to sign in to ConvertKit */ |
| 257 |
esc_html__( 'To make changes to your landing pages, %s', 'convertkit' ), |
| 258 |
'<a href="' . esc_url( convertkit_get_sign_in_url() ) . '" target="_blank">' . esc_html__( 'sign in to Kit', 'convertkit' ) . '</a>' |
| 259 |
), |
| 260 |
), |
| 261 |
'values' => $landing_pages, |
| 262 |
'legacy_values' => $legacy_landing_pages, |
| 263 |
'post_type' => 'page', |
| 264 |
'resource_type' => 'landing_pages', |
| 265 |
), |
| 266 |
'tag' => array( |
| 267 |
'label' => __( 'Tag', 'convertkit' ), |
| 268 |
'type' => 'select', |
| 269 |
'description' => array( |
| 270 |
esc_html__( 'Select a tag to apply to visitors of this page who are subscribed.', 'convertkit' ), |
| 271 |
esc_html__( 'A visitor is deemed to be subscribed if they have clicked a link in an email to this site which includes their subscriber ID, or have entered their email address in a Kit Form on this site.', 'convertkit' ), |
| 272 |
), |
| 273 |
'values' => $tags, |
| 274 |
'resource_type' => 'tags', |
| 275 |
), |
| 276 |
'restrict_content' => array( |
| 277 |
'label' => __( 'Restrict Content', 'convertkit' ), |
| 278 |
'type' => 'select', |
| 279 |
'description' => array( |
| 280 |
esc_html__( 'Select the Kit form, tag or product that the visitor must be subscribed to, permitting them access to view this member-only content.', 'convertkit' ), |
| 281 |
sprintf( |
| 282 |
'<code>%s</code>: %s', |
| 283 |
esc_html__( 'Form', 'convertkit' ), |
| 284 |
esc_html__( 'Displays the Kit form. On submission, the email address will be subscribed to the selected form, granting access to the member-only content. Useful to gate free content in return for an email address.', 'convertkit' ) |
| 285 |
), |
| 286 |
sprintf( |
| 287 |
'<code>%s</code>: %s', |
| 288 |
esc_html__( 'Tag', 'convertkit' ), |
| 289 |
esc_html__( 'Displays a WordPress styled subscription form. On submission, the email address will be subscribed to the selected tag, granting access to the member-only content. Useful to gate free content in return for an email address.', 'convertkit' ) |
| 290 |
), |
| 291 |
sprintf( |
| 292 |
'<code>%s</code>: %s', |
| 293 |
esc_html__( 'Product', 'convertkit' ), |
| 294 |
esc_html__( 'Displays a link to the Kit product, and a login form. Useful to gate content that can only be accessed by purchasing the Kit product.', 'convertkit' ) |
| 295 |
), |
| 296 |
), |
| 297 |
'values' => $restrict_content, |
| 298 |
'legacy_values' => $restrict_content_legacy_forms, |
| 299 |
'resource_type' => 'restrict_content', |
| 300 |
), |
| 301 |
); |
| 302 |
|
| 303 |
} |
| 304 |
|
| 305 |
/** |
| 306 |
* Returns this block's Default Values |
| 307 |
* |
| 308 |
* @since 3.3.0 |
| 309 |
* |
| 310 |
* @return array |
| 311 |
*/ |
| 312 |
public function get_default_values() { |
| 313 |
|
| 314 |
return array( |
| 315 |
'form' => '-1', |
| 316 |
'landing_page' => '0', |
| 317 |
'tag' => '0', |
| 318 |
'restrict_content' => '0', |
| 319 |
); |
| 320 |
|
| 321 |
} |
| 322 |
|
| 323 |
} |
| 324 |
|