post type. They are only * offered while that module is on, and saved rows keep them when it is off. */ const LISTINGS = array( 'rankings' => 'cpod_bs_ranking', 'collections' => 'cpod_collection', ); public static function default_row() { return array( 'id' => '', 'enabled' => 1, 'title' => '', 'type' => 'shortcode', 'custom_id' => 0, 'pattern_id' => 0, 'block_id' => 0, 'content' => '', 'page' => 'all_products', 'products' => array(), 'categories' => array(), 'tags' => array(), 'rankings' => array(), 'collections' => array(), 'hook' => 'after_add_to_cart_form', 'custom_hook' => '', 'priority' => '', 'device' => 'all', ); } /** * Since 2.5.2 the Order number is the WordPress priority for every hook point * (empty = the default of the hook point), like the other modules. Earlier it * only sorted blocks inside their hook point, so convert saved numbers once: * blocks keep their order and get default, default + 1, … of their hook point. */ private static function migrate( $rows ) { if ( (int) get_option( self::OPTION . '_ver', 1 ) >= 2 ) { return $rows; } $hooks = self::hooks( true ); $groups = array(); foreach ( $rows as $i => $row ) { if ( is_array( $row ) && isset( $row['hook'], $hooks[ $row['hook'] ] ) ) { $groups[ $row['hook'] ][] = array( isset( $row['priority'] ) ? (int) $row['priority'] : 10, $i ); } } foreach ( $groups as $hook => $items ) { usort( $items, static function ( $a, $b ) { return $a <=> $b; } ); foreach ( $items as $rank => $item ) { $rows[ $item[1] ]['priority'] = $hooks[ $hook ][2] + $rank; } } update_option( self::OPTION, array( 'rows' => array_values( $rows ) ) ); update_option( self::OPTION . '_ver', 2 ); return $rows; } /** * Where a block may appear: key => label. * * @param bool $all Also the listing pages of modules that are off. */ public static function pages( $all = false ) { $pages = array( 'all_products' => __( 'All products', 'cpod-kit' ), 'products' => __( 'Specific products', 'cpod-kit' ), 'categories' => __( 'Product categories', 'cpod-kit' ), 'tags' => __( 'Product tags', 'cpod-kit' ), 'shop' => __( 'Shop page', 'cpod-kit' ), 'cart' => __( 'Cart', 'cpod-kit' ), 'checkout' => __( 'Checkout', 'cpod-kit' ), 'thankyou' => __( 'Thank-you page (order received)', 'cpod-kit' ), 'rankings' => __( 'Best-selling ranking pages', 'cpod-kit' ), 'collections' => __( 'Collection pages', 'cpod-kit' ), 'everywhere' => __( 'Every page of the site', 'cpod-kit' ), ); foreach ( self::LISTINGS as $key => $post_type ) { if ( ! $all && ! post_type_exists( $post_type ) ) { unset( $pages[ $key ] ); } } return $pages; } /** Whether a listing module (rankings or collections) is on. */ public static function has_listings() { foreach ( self::LISTINGS as $post_type ) { if ( post_type_exists( $post_type ) ) { return true; } } return false; } /** * What a row prints: key => label. UX Block needs Flatsome, so it is only * offered with it (a saved row keeps it). * * @param bool $all Also UX Block without Flatsome. */ public static function types( $all = false ) { $types = array( 'custom' => __( 'Custom block', 'cpod-kit' ), 'pattern' => __( 'Synced pattern', 'cpod-kit' ), 'block' => __( 'Flatsome UX Block', 'cpod-kit' ), 'shortcode' => __( 'Shortcode or HTML', 'cpod-kit' ), ); if ( ! $all && ! post_type_exists( 'blocks' ) ) { unset( $types['block'] ); } return $types; } public static function devices() { return array( 'all' => __( 'Computers and phones', 'cpod-kit' ), 'desktop' => __( 'Computers only', 'cpod-kit' ), 'mobile' => __( 'Phones only', 'cpod-kit' ), ); } /** * Hook points with friendly names, grouped by page: * group label => array( key => array( label, hook, WordPress priority ) ). * * The priorities place the block between WooCommerce's own parts (title 5, * price 10, excerpt 20, add to cart 30, meta 40, sharing 50; tabs 10, * upsells 15, related 20) and avoid the ones used by Size Chart and * Estimated Delivery. * * The listing hook points are fired by the ranking and collection pages * (cpod_listing_before_products / cpod_listing_after_products). * * @param bool $all Also the listing hook points when no listing module is on. * @return array */ public static function hook_groups( $all = false ) { $groups = array( __( 'Product page', 'cpod-kit' ) => array( 'before_single_product' => array( __( 'Top of the product page', 'cpod-kit' ), 'woocommerce_before_single_product', 20 ), 'after_title' => array( __( 'Below the product title', 'cpod-kit' ), 'woocommerce_single_product_summary', 7 ), 'after_price' => array( __( 'Below the price', 'cpod-kit' ), 'woocommerce_single_product_summary', 15 ), 'after_excerpt' => array( __( 'Below the short description', 'cpod-kit' ), 'woocommerce_single_product_summary', 25 ), 'before_add_to_cart_form' => array( __( 'Above the add-to-cart area', 'cpod-kit' ), 'woocommerce_before_add_to_cart_form', 30 ), 'before_add_to_cart_button' => array( __( 'Right above the Add to cart button', 'cpod-kit' ), 'woocommerce_before_add_to_cart_button', 20 ), 'after_add_to_cart_button' => array( __( 'Right after the Add to cart button', 'cpod-kit' ), 'woocommerce_after_add_to_cart_button', 20 ), 'after_add_to_cart_form' => array( __( 'Below the add-to-cart area', 'cpod-kit' ), 'woocommerce_after_add_to_cart_form', 30 ), 'after_meta' => array( __( 'Below SKU / categories', 'cpod-kit' ), 'woocommerce_single_product_summary', 48 ), 'summary_end' => array( __( 'End of the product summary', 'cpod-kit' ), 'woocommerce_single_product_summary', 70 ), 'after_tabs' => array( __( 'Below the product tabs', 'cpod-kit' ), 'woocommerce_after_single_product_summary', 12 ), 'after_related' => array( __( 'Below related products', 'cpod-kit' ), 'woocommerce_after_single_product_summary', 25 ), 'after_single_product' => array( __( 'Bottom of the product page', 'cpod-kit' ), 'woocommerce_after_single_product', 20 ), ), __( 'Shop and categories', 'cpod-kit' ) => array( 'archive_description' => array( __( 'Below the category description', 'cpod-kit' ), 'woocommerce_archive_description', 20 ), 'before_shop_loop' => array( __( 'Above the product list', 'cpod-kit' ), 'woocommerce_before_shop_loop', 40 ), 'after_shop_loop' => array( __( 'Below the product list', 'cpod-kit' ), 'woocommerce_after_shop_loop', 20 ), ), __( 'Cart', 'cpod-kit' ) => array( 'before_cart' => array( __( 'Above the cart', 'cpod-kit' ), 'woocommerce_before_cart', 20 ), 'after_cart_table' => array( __( 'Below the cart table', 'cpod-kit' ), 'woocommerce_after_cart_table', 20 ), 'before_cart_totals' => array( __( 'Above the cart totals', 'cpod-kit' ), 'woocommerce_before_cart_totals', 20 ), 'after_checkout_btn' => array( __( 'Below the Proceed to checkout button', 'cpod-kit' ), 'woocommerce_proceed_to_checkout', 30 ), 'after_cart' => array( __( 'Below the cart', 'cpod-kit' ), 'woocommerce_after_cart', 20 ), ), __( 'Checkout', 'cpod-kit' ) => array( 'before_checkout_form' => array( __( 'Above the checkout form', 'cpod-kit' ), 'woocommerce_before_checkout_form', 20 ), 'before_payment' => array( __( 'Above the payment methods', 'cpod-kit' ), 'woocommerce_review_order_before_payment', 20 ), 'before_place_order' => array( __( 'Above the Place order button', 'cpod-kit' ), 'woocommerce_review_order_before_submit', 20 ), 'after_place_order' => array( __( 'Below the Place order button', 'cpod-kit' ), 'woocommerce_review_order_after_submit', 20 ), 'after_checkout_form' => array( __( 'Below the checkout form', 'cpod-kit' ), 'woocommerce_after_checkout_form', 20 ), ), __( 'Thank-you page', 'cpod-kit' ) => array( 'thankyou_top' => array( __( 'Top of the thank-you page', 'cpod-kit' ), 'woocommerce_before_thankyou', 20 ), 'thankyou_before_order' => array( __( 'Above the order details', 'cpod-kit' ), 'woocommerce_thankyou', 5 ), 'thankyou_after_order' => array( __( 'Below the order details', 'cpod-kit' ), 'woocommerce_thankyou', 20 ), ), __( 'Whole site', 'cpod-kit' ) => array( 'body_open' => array( __( 'Top of every page (after )', 'cpod-kit' ), 'wp_body_open', 20 ), // Before wp_print_footer_scripts (20), which prints the block styles a block asks for while it renders. 'footer' => array( __( 'End of every page (footer)', 'cpod-kit' ), 'wp_footer', 15 ), ), ); if ( $all || self::has_listings() ) { $listing = array( __( 'Ranking and collection pages', 'cpod-kit' ) => array( 'listing_before' => array( __( 'Above the products', 'cpod-kit' ), 'cpod_listing_before_products', 20 ), 'listing_after' => array( __( 'Below the products', 'cpod-kit' ), 'cpod_listing_after_products', 20 ), ), ); // Before "Whole site". $groups = array_slice( $groups, 0, -1, true ) + $listing + array_slice( $groups, -1, null, true ); } return $groups; } /** * Flat list of hook points: key => array( label, hook, priority ). * * @param bool $all Also the listing hook points when no listing module is on. */ public static function hooks( $all = false ) { $out = array(); foreach ( self::hook_groups( $all ) as $items ) { $out += $items; } return $out; } /** Every saved row, in table order. */ public static function all() { if ( null === self::$cache ) { $saved = get_option( self::OPTION, array() ); $rows = self::migrate( is_array( $saved ) && isset( $saved['rows'] ) ? (array) $saved['rows'] : array() ); self::$cache = array(); foreach ( $rows as $row ) { self::$cache[] = wp_parse_args( (array) $row, self::default_row() ); } } return self::$cache; } /** WordPress hook and priority a row is attached to. */ public static function target( $row ) { if ( 'custom' === $row['hook'] ) { return '' !== $row['custom_hook'] ? array( $row['custom_hook'], (int) $row['priority'] ) : null; } $hooks = self::hooks( true ); if ( ! isset( $hooks[ $row['hook'] ] ) ) { return null; } $priority = '' !== (string) $row['priority'] ? (int) $row['priority'] : $hooks[ $row['hook'] ][2]; return array( $hooks[ $row['hook'] ][1], $priority ); } /** Shortcode / HTML a row prints (before do_shortcode). */ public static function source( $row ) { switch ( $row['type'] ) { case 'custom': return $row['custom_id'] ? '[' . Renderer::SHORTCODE . ' id="' . (int) $row['custom_id'] . '"]' : ''; case 'pattern': return $row['pattern_id'] ? '[' . Renderer::SHORTCODE . ' id="' . (int) $row['pattern_id'] . '"]' : ''; case 'block': return $row['block_id'] ? '[block id="' . (int) $row['block_id'] . '"]' : ''; } return (string) $row['content']; } /** Post id of the Custom block or pattern a row prints, 0 for the other types. */ public static function post_id( $row ) { if ( 'custom' === $row['type'] ) { return (int) $row['custom_id']; } return 'pattern' === $row['type'] ? (int) $row['pattern_id'] : 0; } /** * Rows that print a Custom block or pattern. * * @param int $post_id Post id. * @return array index => row. */ public static function rows_using( $post_id ) { $out = array(); foreach ( self::all() as $index => $row ) { if ( (int) $post_id === self::post_id( $row ) ) { $out[ $index ] = $row; } } return $out; } private static function ids( $value ) { if ( is_string( $value ) ) { $value = explode( ',', $value ); } return array_values( array_unique( array_filter( array_map( 'absint', (array) $value ) ) ) ); } public static function sanitize( $input ) { $input = is_array( $input ) ? $input : array(); $rows = isset( $input['rows'] ) && is_array( $input['rows'] ) ? $input['rows'] : array(); $out = array(); $seen = array(); $pages = self::pages( true ); $hooks = self::hooks( true ); $devices = self::devices(); $types = self::types( true ); $removed = false; foreach ( $rows as $row ) { if ( ! is_array( $row ) ) { continue; } $row = wp_parse_args( $row, array( 'enabled' => 0 ) + self::default_row() ); $clean = array(); $id = sanitize_key( $row['id'] ); if ( '' === $id || isset( $seen[ $id ] ) ) { $id = 'hb' . strtolower( wp_generate_password( 8, false, false ) ); } $seen[ $id ] = true; $clean['id'] = $id; $clean['enabled'] = empty( $row['enabled'] ) ? 0 : 1; $clean['title'] = sanitize_text_field( $row['title'] ); $clean['type'] = isset( $types[ $row['type'] ] ) ? $row['type'] : 'shortcode'; $clean['custom_id'] = absint( $row['custom_id'] ); $clean['pattern_id'] = absint( $row['pattern_id'] ); $clean['block_id'] = absint( $row['block_id'] ); $content = (string) $row['content']; $clean['content'] = cpod_strip_code_tags( $content ); $removed = $removed || (bool) preg_match( '#<(script|style)\b#i', $content ); $clean['page'] = isset( $pages[ $row['page'] ] ) ? $row['page'] : 'all_products'; $clean['products'] = self::ids( $row['products'] ); $clean['categories'] = self::ids( $row['categories'] ); $clean['tags'] = self::ids( $row['tags'] ); $clean['rankings'] = self::ids( $row['rankings'] ); $clean['collections'] = self::ids( $row['collections'] ); $clean['hook'] = isset( $hooks[ $row['hook'] ] ) || 'custom' === $row['hook'] ? $row['hook'] : 'after_add_to_cart_form'; $clean['custom_hook'] = preg_replace( '/[^A-Za-z0-9_\-\/.:]/', '', (string) $row['custom_hook'] ); $clean['priority'] = is_numeric( $row['priority'] ) ? max( -9999, min( 9999, (int) $row['priority'] ) ) : ( 'custom' === $clean['hook'] ? 10 : '' ); $clean['device'] = isset( $devices[ $row['device'] ] ) ? $row['device'] : 'all'; // Skip rows left completely empty. if ( '' === trim( $clean['content'] ) && ! $clean['block_id'] && ! $clean['custom_id'] && ! $clean['pattern_id'] && '' === $clean['title'] ) { continue; } $out[] = $clean; } if ( $removed && function_exists( 'add_settings_error' ) ) { add_settings_error( self::OPTION, 'cpod_hb_code', __( 'Scripts and styles are not allowed in Hook Blocks and were removed. Use a shortcode or a Custom block instead.', 'cpod-kit' ), 'warning' ); } self::$cache = null; return array( 'rows' => $out ); } }