$this->get_title(), 'description' => __( 'Displays Kit Custom Content for a subscriber if their tag matches the Page\'s tag.', 'convertkit' ), 'icon' => $this->get_icon(), 'category' => 'convertkit', 'keywords' => array( __( 'ConvertKit', 'convertkit' ), __( 'Kit', 'convertkit' ), __( 'Content', 'convertkit' ), ), // TinyMCE / QuickTags Modal Width and Height. 'modal' => array( 'width' => 500, 'height' => 106, ), 'shortcode_include_closing_tag' => true, // Function to call when rendering the block/shortcode on the frontend web site. 'render_callback' => array( $this, 'render' ), ); } /** * Returns this block's Attributes * * @since 1.9.6.5 * * @return array */ public function get_attributes() { return array( 'tag' => array( 'type' => 'string', ), ); } /** * Returns this block's Fields * * @since 1.9.6 * * @return bool|array */ public function get_fields() { // Get ConvertKit Tags. $tags = array(); $convertkit_tags = new ConvertKit_Resource_Tags(); if ( $convertkit_tags->exist() ) { foreach ( $convertkit_tags->get() as $tag ) { $tags[ absint( $tag['id'] ) ] = sanitize_text_field( $tag['name'] ); } } return array( 'tag' => array( 'label' => __( 'Tag', 'convertkit' ), 'type' => 'select', 'values' => $tags, ), ); } /** * Returns this block's UI panels / sections. * * @since 1.9.6 * * @return bool|array */ public function get_panels() { return array( 'general' => array( 'label' => __( 'General', 'convertkit' ), 'fields' => array( 'tag', ), ), ); } /** * Returns this block's Default Values * * @since 1.9.6 * * @return array */ public function get_default_values() { return array( 'tag' => '', ); } /** * Returns the block's output, based on the supplied configuration attributes. * * @since 1.9.6 * * @param array $atts Block / Shortcode Attributes. * @param string $content Content. * @return string Output */ public function render( $atts, $content = '' ) { // Bail if the request is not for the frontend site. if ( is_admin() ) { return ''; } // Parse shortcode attributes, defining fallback defaults if required. $atts = shortcode_atts( $this->get_default_values(), $this->sanitize_atts( $atts ), $this->get_name() ); // Setup Settings class. $settings = new ConvertKit_Settings(); // Bail if the tag isn't specified. if ( ! $atts['tag'] ) { if ( $settings->debug_enabled() ) { return ''; } return ''; } // Bail if there is no subscriber ID from the cookie or request. $subscriber = new ConvertKit_Subscriber(); $subscriber_id = $subscriber->get_subscriber_id(); if ( is_wp_error( $subscriber_id ) ) { if ( $settings->debug_enabled() ) { return ''; } return ''; } if ( ! $subscriber_id ) { if ( $settings->debug_enabled() ) { return ''; } return ''; } // Bail if the API hasn't been configured. $settings = new ConvertKit_Settings(); if ( ! $settings->has_access_and_refresh_token() ) { if ( $settings->debug_enabled() ) { return ''; } return ''; } // Initialize the API. $api = new ConvertKit_API_V4( CONVERTKIT_OAUTH_CLIENT_ID, CONVERTKIT_OAUTH_CLIENT_REDIRECT_URI, $settings->get_access_token(), $settings->get_refresh_token(), $settings->debug_enabled(), 'output_content' ); // Get the subscriber's tags, to see if they subscribed to this tag. if ( is_numeric( $subscriber_id ) ) { $tags = $api->get_subscriber_tags( $subscriber_id ); // Bail if an error occurred. if ( is_wp_error( $tags ) ) { if ( $settings->debug_enabled() ) { return ''; } return ''; } } else { // Subscriber ID is a signed subscriber ID. // Get tags that the subscriber has access to via the profile() method. $result = $api->profile( $subscriber_id ); // If an error occurred, the subscriber ID is invalid. if ( is_wp_error( $result ) ) { if ( $settings->debug_enabled() ) { return ''; } return ''; } // Build tags array. $tags = array( 'tags' => array(), ); foreach ( $result['tags'] as $tag_id ) { $tags['tags'][] = array( 'id' => $tag_id, ); } } // Bail if the subscriber has no tags. if ( ! count( $tags ) ) { if ( $settings->debug_enabled() ) { return ''; } return ''; } // Iterate through ConvertKit Tags to find a match. foreach ( $tags['tags'] as $tag ) { // Skip if this ConvertKit Tag isn't the Tag specified in the block. if ( absint( $tag['id'] ) !== absint( $atts['tag'] ) ) { continue; } /** * Filters the content in the ConvertKit Custom Content block/shortcode * immediately before it is output. * * @since 1.9.6 * * @param string $content Content * @param array $atts Block / Shortcode Attributes * @param int $subscriber_id ConvertKit Subscriber's ID * @param array $tags ConvertKit Subscriber's Tags * @param array $tag ConvertKit Subscriber's Tag that matches $atts['tag'] */ $content = apply_filters( 'convertkit_block_content_render', $content, $atts, $subscriber_id, $tags, $tag ); /** * Backward compat. filter for < 1.9.6. Filters the content in the ConvertKit Custom Content block/shortcode * immediately before it is output. * * @since 1.0.0 * * @param string $content Content * @param array $atts Block / Shortcode Attributes */ $content = apply_filters( 'wp_convertkit_shortcode_custom_content', $content, $atts ); return $content; } // If here, the subscriber does not have the block's tag. if ( $settings->debug_enabled() ) { return ''; } return ''; } }