* @return void */ public function __construct() { // Add custom page templates from the plugin add_filter( 'body_class', array( $this, 'body_classes' ), 999 ); // Add custom scripts to the plugin, the WordPress way add_action( 'wp_enqueue_scripts', array( $this, 'public_register_assets' ) ); add_filter( 'affilizz_has_affilizz_content', array( $this, 'identify_content_from_regexp' ), 10, 2 ); // Add needed meta tags in the header add_action( 'wp_head', array( $this, 'add_media_meta_tag' ) ); // Add styles to the classic editor styles add_action( 'admin_init', array( $this, 'add_editor_style_classic' ) ); add_action( 'enqueue_block_assets', array( $this, 'add_editor_style_classic' ) ); // Wrap the renders in a custom container add_filter( 'affilizz_publication_content_render', array( $this, 'wrap_affilizz_webcomponents' ) ); // Allow custom HTML tags in the content add_filter( 'wp_kses_allowed_html', array( $this, 'allow_affilizz_custom_tag_in_content' ), 10, 2 ); add_filter( 'tiny_mce_before_init', array( $this, 'allow_affilizz_custom_tag_in_editor' ), 10, 1 ); // Keep the SSR fragments of the hybrid mode intact : block themes texturize the whole page after the shortcodes add_filter( 'no_texturize_tags', array( $this, 'do_not_texturize_templates' ) ); } /** * Excludes the template elements (the declarative shadow DOM of the SSR fragments) from wptexturize(). * @author Affilizz * @since 1.18.0 * @param $tags The HTML elements not to texturize. * @return Array The updated list. */ public function do_not_texturize_templates( $tags ) { $tags[] = 'template'; return $tags; } /** * Updates the body classes for custom templates. * @param $template The current body classes. * @author Affilizz * @return String The updated body classes. */ public function body_classes( $body_classes ) { global $post; if ( $post && isset( $post->post_content ) && stripos( $post->post_content, '[affilizz-publication' ) ) { $body_classes[] = 'affilizz-content'; } return $body_classes; } /** * Register scripts and stylesheets for the public part. * @author Affilizz * @return void */ public function public_register_assets() { // Do not load the script if disabled by the user $disabled_javascript = get_option( 'affilizz_disable_javascript' ); if ( ! empty( $disabled_javascript ) && $disabled_javascript === '1' ) { return; } // Do not load the script if we have selective enqueuing enabled and no Affilizz tag global $post; $selective_enqueing = get_option( 'affilizz_selective_enqueue', __( 'N/A', 'affilizz' ) ); if ( $selective_enqueing == 1 ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Intentionally applies WordPress core content filters before checking for Affilizz tags. $post_content = apply_filters( 'the_content', ( ! empty( $post ) && ! empty( $post->post_content ) ? $post->post_content : '' ) ); $has_affilizz_content = apply_filters( 'affilizz_has_affilizz_content', $post_content, false ); if ( ! $has_affilizz_content ) { return; } } // Add and enqueue custom scripts, the WordPress way wp_enqueue_script( 'affilizz-rendering', ( Assets::get_instance() )->get_diverted_js_file_url(), array(), ( Assets::get_instance() )->get_diverted_js_file_version(), array( 'strategy' => 'defer' ) ); } /** * Adds custom styles to the tinymce editor styles. * @author Affilizz * @return void */ public function add_editor_style_classic() { if ( ! function_exists( 'get_current_screen' ) ) { return; } add_theme_support( 'editor-style' ); if ( ! empty( get_current_screen() ) && get_current_screen()->is_block_editor() ) { return; } add_editor_style( AFFILIZZ_URL . 'assets/dist/css/admin.css?affilizz_version = ' . AFFILIZZ_ASSETS_BUILD_VERSION ); } public function add_editor_style_gutenberg() { if ( ! function_exists( 'get_current_screen' ) ) { return; } if ( ! is_admin() ) { return; } if ( empty( get_current_screen() ) || ! get_current_screen()->is_block_editor() ) { return; } wp_enqueue_style( 'affilizz-block-admin-css', AFFILIZZ_URL . 'assets/dist/css/admin.css', array(), AFFILIZZ_ASSETS_BUILD_VERSION ); } /** * Wraps the components in a wrapper div to allow for easier identification in the webpage source. * @author Affilizz * @return String The wrapper content. */ public function wrap_affilizz_webcomponents( $content ) { return '
' . $content . '
'; } /** * Allows affilizz-rendering-component tags in the WordPress content areas. * @author Affilizz * @since 1.12.0 * @param $tags The currently allowed tags ; * @param $context The context in which we update the tags. * @return Array An updated list of allowed tags. */ public function allow_affilizz_custom_tag_in_content( $tags, $context ) { if ( $context != 'post' ) { return $tags; } $attributes = array( 'affilizz-rendering-component' => array( 'loading', 'prevent-font-loading', 'subtag', 'design-id', 'render-api-url', 'redirect-api-url', 'render-api-cache', 'page-session-id', 'custom-locale', 'custom-css', 'publication-content-id', 'cacheable', 'client-cache', ), 'affilizz-deal-component' => array( 'loading', 'prevent-font-loading', 'subtag', 'design-id', 'render-api-url', 'redirect-api-url', 'render-api-cache', 'page-session-id', 'custom-locale', 'custom-css', 'content-container-id', 'content-container-tinyCode', 'deal-id', 'deal-tiny-code', 'client-cache', ), 'affilizz-magic-match' => array( 'loading', 'prevent-font-loading', 'subtag', 'design-id', 'render-api-url', 'redirect-api-url', 'render-api-cache', 'page-session-id', 'custom-locale', 'custom-css', ), ); foreach ( $attributes as $tag => $tag_attributes ) { if ( ! isset( $tags[ $tag ] ) ) { $tags[ $tag ] = array(); } foreach ( $tag_attributes as $attribute ) { if ( ! in_array( $attribute, $tags[ $tag ] ) ) { $tags[ $tag ][ $attribute ] = true; } } } return $tags; } /** * Allows affilizz-rendering-component tags in the WordPress editors. * @author Affilizz * @since 1.12.0 * @param $init The TinyMCE init array in its current state. * @return Array An updated init array. */ public function allow_affilizz_custom_tag_in_editor( $init ) { // Command separated string of extended elements $added_tags = 'affilizz-rendering-component'; // Add to extended_valid_elements if it already exists if ( isset( $init['extended_valid_elements'] ) ) { $init['extended_valid_elements'] .= ',affilizz-rendering-component,affilizz-rendering-component[*],affilizz-rendering-ssr,affilizz-rendering-ssr[*],rnd-rendering,rnd-rendering[*]'; } else { $init['extended_valid_elements'] = 'affilizz-rendering-component,affilizz-rendering-component[*],affilizz-rendering-ssr,affilizz-rendering-ssr[*],rnd-rendering,rnd-rendering[*]'; } if ( ! isset( $init['valid_children'] ) ) { $init['valid_children'] = '+body[affilizz-rendering-component]'; } else { $init['valid_children'] .= ',+body[affilizz-rendering-component]'; } if ( ! isset( $init['custom_elements'] ) ) { $init['custom_elements'] = 'affilizz-rendering-component,affilizz-rendering-component[*],affilizz-rendering-ssr,affilizz-rendering-ssr[*],rnd-rendering,rnd-rendering[*]'; } else { $init['custom_elements'] .= ',affilizz-rendering-component,affilizz-rendering-component[*],affilizz-rendering-ssr,affilizz-rendering-ssr[*],rnd-rendering,rnd-rendering[*]'; } return $init; } /** * Remove the enqueues on pages that do not have Affilizz content. * @author Affilizz * @return String The CDN caching root URL, (potentially) filtered. */ public function identify_content_from_regexp( $content = '', $bypass_option = false ) { $affilizz_selective_enqueue = get_option( 'affilizz_selective_enqueue' ); if ( ( empty( $affilizz_selective_enqueue ) || $affilizz_selective_enqueue !== '1' ) && ! $bypass_option ) { false; } $has_affilizz_content = preg_match( \Affilizz\Core::get_selective_enqueuing_regex(), $content ); return $has_affilizz_content; } /** * Adds a meta tag if we specified the media. * @author Affilizz * @return void */ public function add_media_meta_tag() { $affilizz_media = esc_attr( trim( get_option( 'affilizz_media', '' ) ) ); if ( empty( $affilizz_media ) ) { return; } printf( '' . "\r\n", esc_attr( $affilizz_media ) ); } }