| 1 |
<?php |
| 2 |
/** |
| 3 |
* ConvertKit Custom Content Block class. |
| 4 |
* |
| 5 |
* @package ConvertKit |
| 6 |
* @author ConvertKit |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* ConvertKit Custom Content Block for Gutenberg and Shortcode. |
| 11 |
* |
| 12 |
* @package ConvertKit |
| 13 |
* @author ConvertKit |
| 14 |
*/ |
| 15 |
class ConvertKit_Block_Content extends ConvertKit_Block { |
| 16 |
|
| 17 |
/** |
| 18 |
* Constructor |
| 19 |
* |
| 20 |
* @since 1.9.6 |
| 21 |
*/ |
| 22 |
public function __construct() { |
| 23 |
|
| 24 |
// Register this as a shortcode in the ConvertKit Plugin. |
| 25 |
add_filter( 'convertkit_shortcodes', array( $this, 'register' ) ); |
| 26 |
|
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Returns this block's programmatic name, excluding the convertkit- prefix. |
| 31 |
* |
| 32 |
* @since 1.9.6 |
| 33 |
* |
| 34 |
* @return string |
| 35 |
*/ |
| 36 |
public function get_name() { |
| 37 |
|
| 38 |
/** |
| 39 |
* This will register as: |
| 40 |
* - a shortcode, with the name [convertkit_content] |
| 41 |
* - a Gutenberg block, with the name convertkit/content |
| 42 |
*/ |
| 43 |
return 'content'; |
| 44 |
|
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Returns this block's Title, Icon, Categories, Keywords and properties. |
| 49 |
* |
| 50 |
* @since 1.9.6 |
| 51 |
* |
| 52 |
* @return array |
| 53 |
*/ |
| 54 |
public function get_overview() { |
| 55 |
|
| 56 |
return array( |
| 57 |
'title' => __( 'Kit Custom Content', 'convertkit' ), |
| 58 |
'description' => __( 'Displays Kit Custom Content for a subscriber if their tag matches the Page\'s tag.', 'convertkit' ), |
| 59 |
'icon' => 'resources/backend/images/block-icon-content.svg', |
| 60 |
'category' => 'convertkit', |
| 61 |
'keywords' => array( |
| 62 |
__( 'ConvertKit', 'convertkit' ), |
| 63 |
__( 'Kit', 'convertkit' ), |
| 64 |
__( 'Content', 'convertkit' ), |
| 65 |
), |
| 66 |
|
| 67 |
// TinyMCE / QuickTags Modal Width and Height. |
| 68 |
'modal' => array( |
| 69 |
'width' => 500, |
| 70 |
'height' => 106, |
| 71 |
), |
| 72 |
|
| 73 |
'shortcode_include_closing_tag' => true, |
| 74 |
|
| 75 |
// Function to call when rendering the block/shortcode on the frontend web site. |
| 76 |
'render_callback' => array( $this, 'render' ), |
| 77 |
); |
| 78 |
|
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Returns this block's Attributes |
| 83 |
* |
| 84 |
* @since 1.9.6.5 |
| 85 |
* |
| 86 |
* @return array |
| 87 |
*/ |
| 88 |
public function get_attributes() { |
| 89 |
|
| 90 |
return array( |
| 91 |
'tag' => array( |
| 92 |
'type' => 'string', |
| 93 |
), |
| 94 |
); |
| 95 |
|
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Returns this block's Fields |
| 100 |
* |
| 101 |
* @since 1.9.6 |
| 102 |
* |
| 103 |
* @return bool|array |
| 104 |
*/ |
| 105 |
public function get_fields() { |
| 106 |
|
| 107 |
// Bail if the request is not for the WordPress Administration or frontend editor. |
| 108 |
if ( ! WP_ConvertKit()->is_admin_or_frontend_editor() ) { |
| 109 |
return false; |
| 110 |
} |
| 111 |
|
| 112 |
// Get ConvertKit Tags. |
| 113 |
$tags = array(); |
| 114 |
$convertkit_tags = new ConvertKit_Resource_Tags(); |
| 115 |
if ( $convertkit_tags->exist() ) { |
| 116 |
foreach ( $convertkit_tags->get() as $tag ) { |
| 117 |
$tags[ absint( $tag['id'] ) ] = sanitize_text_field( $tag['name'] ); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
return array( |
| 122 |
'tag' => array( |
| 123 |
'label' => __( 'Tag', 'convertkit' ), |
| 124 |
'type' => 'select', |
| 125 |
'values' => $tags, |
| 126 |
), |
| 127 |
); |
| 128 |
|
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Returns this block's UI panels / sections. |
| 133 |
* |
| 134 |
* @since 1.9.6 |
| 135 |
* |
| 136 |
* @return bool|array |
| 137 |
*/ |
| 138 |
public function get_panels() { |
| 139 |
|
| 140 |
// Bail if the request is not for the WordPress Administration or frontend editor. |
| 141 |
if ( ! WP_ConvertKit()->is_admin_or_frontend_editor() ) { |
| 142 |
return false; |
| 143 |
} |
| 144 |
|
| 145 |
return array( |
| 146 |
'general' => array( |
| 147 |
'label' => __( 'General', 'convertkit' ), |
| 148 |
'fields' => array( |
| 149 |
'tag', |
| 150 |
), |
| 151 |
), |
| 152 |
); |
| 153 |
|
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Returns this block's Default Values |
| 158 |
* |
| 159 |
* @since 1.9.6 |
| 160 |
* |
| 161 |
* @return array |
| 162 |
*/ |
| 163 |
public function get_default_values() { |
| 164 |
|
| 165 |
return array( |
| 166 |
'tag' => '', |
| 167 |
); |
| 168 |
|
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Returns the block's output, based on the supplied configuration attributes. |
| 173 |
* |
| 174 |
* @since 1.9.6 |
| 175 |
* |
| 176 |
* @param array $atts Block / Shortcode Attributes. |
| 177 |
* @param string $content Content. |
| 178 |
* @return string Output |
| 179 |
*/ |
| 180 |
public function render( $atts, $content = '' ) { |
| 181 |
|
| 182 |
// Bail if the request is not for the frontend site. |
| 183 |
if ( is_admin() ) { |
| 184 |
return ''; |
| 185 |
} |
| 186 |
|
| 187 |
// Parse shortcode attributes, defining fallback defaults if required. |
| 188 |
$atts = shortcode_atts( |
| 189 |
$this->get_default_values(), |
| 190 |
$this->sanitize_atts( $atts ), |
| 191 |
$this->get_name() |
| 192 |
); |
| 193 |
|
| 194 |
// Setup Settings class. |
| 195 |
$settings = new ConvertKit_Settings(); |
| 196 |
|
| 197 |
// Bail if the tag isn't specified. |
| 198 |
if ( ! $atts['tag'] ) { |
| 199 |
if ( $settings->debug_enabled() ) { |
| 200 |
return '<!-- Kit Custom Content: No Tag Specified -->'; |
| 201 |
} |
| 202 |
|
| 203 |
return ''; |
| 204 |
} |
| 205 |
|
| 206 |
// Bail if there is no subscriber ID from the cookie or request. |
| 207 |
$subscriber = new ConvertKit_Subscriber(); |
| 208 |
$subscriber_id = $subscriber->get_subscriber_id(); |
| 209 |
if ( is_wp_error( $subscriber_id ) ) { |
| 210 |
if ( $settings->debug_enabled() ) { |
| 211 |
return '<!-- Kit Custom Content: Subscriber ID Error: ' . $subscriber_id->get_error_message() . ' -->'; |
| 212 |
} |
| 213 |
|
| 214 |
return ''; |
| 215 |
} |
| 216 |
if ( ! $subscriber_id ) { |
| 217 |
if ( $settings->debug_enabled() ) { |
| 218 |
return '<!-- Kit Custom Content: Subscriber ID does not exist -->'; |
| 219 |
} |
| 220 |
|
| 221 |
return ''; |
| 222 |
} |
| 223 |
|
| 224 |
// Bail if the API hasn't been configured. |
| 225 |
$settings = new ConvertKit_Settings(); |
| 226 |
if ( ! $settings->has_access_and_refresh_token() ) { |
| 227 |
if ( $settings->debug_enabled() ) { |
| 228 |
return '<!-- Kit Custom Content: No Access Token -->'; |
| 229 |
} |
| 230 |
|
| 231 |
return ''; |
| 232 |
} |
| 233 |
|
| 234 |
// Initialize the API. |
| 235 |
$api = new ConvertKit_API_V4( |
| 236 |
CONVERTKIT_OAUTH_CLIENT_ID, |
| 237 |
CONVERTKIT_OAUTH_CLIENT_REDIRECT_URI, |
| 238 |
$settings->get_access_token(), |
| 239 |
$settings->get_refresh_token(), |
| 240 |
$settings->debug_enabled(), |
| 241 |
'output_content' |
| 242 |
); |
| 243 |
|
| 244 |
// Get the subscriber's tags, to see if they subscribed to this tag. |
| 245 |
$tags = $api->get_subscriber_tags( $subscriber_id ); |
| 246 |
|
| 247 |
// Bail if an error occured. |
| 248 |
if ( is_wp_error( $tags ) ) { |
| 249 |
if ( $settings->debug_enabled() ) { |
| 250 |
return '<!-- Kit Custom Content: ' . $tags->get_error_message() . ' -->'; |
| 251 |
} |
| 252 |
|
| 253 |
return ''; |
| 254 |
} |
| 255 |
|
| 256 |
// Bail if the subscriber has no tags. |
| 257 |
if ( ! count( $tags ) ) { |
| 258 |
if ( $settings->debug_enabled() ) { |
| 259 |
return '<!-- Kit Custom Content: Subscriber has no tags -->'; |
| 260 |
} |
| 261 |
|
| 262 |
return ''; |
| 263 |
} |
| 264 |
|
| 265 |
// Iterate through ConvertKit Tags to find a match. |
| 266 |
foreach ( $tags['tags'] as $tag ) { |
| 267 |
// Skip if this ConvertKit Tag isn't the Tag specified in the block. |
| 268 |
if ( absint( $tag['id'] ) !== absint( $atts['tag'] ) ) { |
| 269 |
continue; |
| 270 |
} |
| 271 |
|
| 272 |
/** |
| 273 |
* Filters the content in the ConvertKit Custom Content block/shortcode |
| 274 |
* immediately before it is output. |
| 275 |
* |
| 276 |
* @since 1.9.6 |
| 277 |
* |
| 278 |
* @param string $content Content |
| 279 |
* @param array $atts Block / Shortcode Attributes |
| 280 |
* @param int $subscriber_id ConvertKit Subscriber's ID |
| 281 |
* @param array $tags ConvertKit Subscriber's Tags |
| 282 |
* @param array $tag ConvertKit Subscriber's Tag that matches $atts['tag'] |
| 283 |
*/ |
| 284 |
$content = apply_filters( 'convertkit_block_content_render', $content, $atts, $subscriber_id, $tags, $tag ); |
| 285 |
|
| 286 |
/** |
| 287 |
* Backward compat. filter for < 1.9.6. Filters the content in the ConvertKit Custom Content block/shortcode |
| 288 |
* immediately before it is output. |
| 289 |
* |
| 290 |
* @since 1.0.0 |
| 291 |
* |
| 292 |
* @param string $content Content |
| 293 |
* @param array $atts Block / Shortcode Attributes |
| 294 |
*/ |
| 295 |
$content = apply_filters( 'wp_convertkit_shortcode_custom_content', $content, $atts ); |
| 296 |
|
| 297 |
return $content; |
| 298 |
} |
| 299 |
|
| 300 |
// If here, the subscriber does not have the block's tag. |
| 301 |
if ( $settings->debug_enabled() ) { |
| 302 |
return '<!-- Kit Custom Content: Subscriber does not have tag -->'; |
| 303 |
} |
| 304 |
|
| 305 |
return ''; |
| 306 |
|
| 307 |
} |
| 308 |
|
| 309 |
} |
| 310 |
|