true ) ); wp_localize_script( AFFIAI_PLUGIN_NAME . '-box-editor-script', 'affiaiLiteBlockMetadata', array( 'attributes' => is_array( $block_metadata ) && isset( $block_metadata['attributes'] ) ? $block_metadata['attributes'] : array() ) ); /** * Registers block type from metadata. * * @param string $directory The directory path of the block.json file. * @param array $args Array of block type arguments. * * @return void */ register_block_type_from_metadata( AFFIAI_PLUGIN_DIR . 'admin/affi/block.json', array( // Block type arguments 'attributes' => array( 'noOffersMessage' => array( 'type' => 'string', 'default' => '' ), 'noOffersKeepProduct' => array( 'type' => 'boolean', 'default' => false ), 'searchType' => array( 'type' => 'string', 'default' => 'pick', ), 'searchEngine' => array( 'type' => 'string', ), 'selectedMerch' => array( 'type' => 'string', 'default' => null, ), 'formatBox' => array( 'type' => 'string', ), 'nbMerchant' => array( 'type' => 'integer', ), 'nbProduct' => array( 'type' => 'integer', ), 'minPrice' => array( 'type' => 'string', ), 'maxPrice' => array( 'type' => 'string', ), 'reduc' => array( 'type' => 'integer', ), 'searchText' => array( 'type' => 'string', ), 'content' => array( 'type' => 'string', ), 'searchType2' => array( 'type' => 'string', ), 'searchPick' => array( 'type' => 'string', ), 'ctaType' => array( 'type' => 'string', ), 'ratingType' => array( 'type' => 'array', ), 'ratingType0' => array( 'type' => 'string', ), 'btnText' => array( 'type' => 'array', ), 'intialPick' => array( 'type' => 'string', ), 'modalPick' => array( 'type' => 'boolean', ), 'modalFilt' => array( 'type' => 'boolean', ), 'addRank' => array( 'type' => 'boolean', ), 'btnColor' => array( 'type' => 'object', ), 'textProd' => array( 'type' => 'array', ), 'addRate' => array( 'type' => 'array', ), 'nbStar' => array( 'type' => 'array', ), 'Descr' => array( 'type' => 'array', ), 'Pros' => array( 'type' => 'array', ), 'Cons' => array( 'type' => 'array', ), 'mediaId' => array( 'type' => 'array', ), 'mediaUrl' => array( 'type' => 'array', ), 'ccm' => array( 'type' => 'array', ), 'custimgt' => array( 'type' => 'array', ), 'RemUrl' => array( 'type' => 'array', ), 'UrlMerch1' => array( 'type' => 'array', ), 'UrlMerch2' => array( 'type' => 'array', ), 'UrlMerch3' => array( 'type' => 'array', ), 'UrlHome1' => array( 'type' => 'array', ), 'UrlHome2' => array( 'type' => 'array', ), 'UrlHome3' => array( 'type' => 'array', ), 'convURL1' => array( 'type' => 'array', ), 'convURL2' => array( 'type' => 'array', ), 'convURL3' => array( 'type' => 'array', ), 'showPrice1' => array( 'type' => 'array', ), 'showPrice2' => array( 'type' => 'array', ), 'showPrice3' => array( 'type' => 'array', ), 'custPrice1' => array( 'type' => 'array', ), 'custPrice2' => array( 'type' => 'array', ), 'custPrice3' => array( 'type' => 'array', ), 'custCurr1' => array( 'type' => 'array', ), 'custCurr2' => array( 'type' => 'array', ), 'custCurr3' => array( 'type' => 'array', ), 'hideTitle' => array( 'type' => 'boolean', ), 'hideDescr' => array( 'type' => 'boolean', ), 'sData' => array( 'type' => 'boolean', ), 'linkWidth' => array( 'type' => 'string', ), 'linkUrl' => array( 'type' => 'string', ), 'linkText' => array( 'type' => 'string', ), 'linkObf' => array( 'type' => 'boolean', ), 'linkType' => array( 'type' => 'string', ), 'linkTrack' => array( 'type' => 'boolean', ), 'linkSize' => array( 'type' => 'string', ), 'linkIcon' => array( 'type' => 'object', ), 'linkAlign' => array( 'type' => 'string', ), ), 'render_callback' => 'affiai_render_gut', ) ); $merch_filter = array(); $active_se = array(); $merch_filter_f = $merch_filter; $pf_active = AffiAI::active_merchants(); if ( in_array( 'amazon', $pf_active ) ) { $merch_filter['Généraliste']['options'][] = array( 'label' => 'Amazon', 'value' => 'amazon', ); $active_se[] = array( 'label' => 'Amazon', 'value' => 'amazon', ); } $icon_show['icon'] = get_option( AFFIAI_PLUGIN_OPTION_PREFIX . '_shopping_icon' ); if (is_array($icon_show['icon'])) { $icon_show['icon'] = array_values( $icon_show['icon'] ); } else { $icon_show['icon'] = array(); } $icon_show['text'] = get_option( AFFIAI_PLUGIN_OPTION_PREFIX . '_shopping_text_icon' ); if (is_array($icon_show['text'])) { $icon_show['text'] = array_values( $icon_show['text'] ); } else { $icon_show['text'] = array(); } $param['radius'] = get_option( AFFIAI_PLUGIN_OPTION_PREFIX . '_shopping_border_radius', 0 ) . 'px'; $param['text'] = get_option( AFFIAI_PLUGIN_OPTION_PREFIX . '_shopping_cta', __( 'View offer', 'affiliate-ai-lite' ) ); $param['color'] = get_option( AFFIAI_PLUGIN_OPTION_PREFIX . '_shopping_color', '#bc00bc' ); $param['count'] = (int) get_option( AFFIAI_PLUGIN_OPTION_PREFIX . '_shopping_count', 3 ); $param['template'] = get_option( AFFIAI_PLUGIN_OPTION_PREFIX . '_shopping_template', 'grid' ); $param['column'] = (int) get_option( AFFIAI_PLUGIN_OPTION_PREFIX . '_shopping_column', 33 ); $param['cta'] = get_option( AFFIAI_PLUGIN_OPTION_PREFIX . '_shopping_cta_type', 'all' ); $param['btn_type'] = get_option( AFFIAI_PLUGIN_OPTION_PREFIX . '_shopping_btn_type', 'gradient' ); $param['rating_type'] = get_option( AFFIAI_PLUGIN_OPTION_PREFIX . '_shopping_rating_type', 'scircle' ); $param['hide_price'] = get_option( AFFIAI_PLUGIN_OPTION_PREFIX . '_shopping_hide_price', false ); $param['pros_title'] = get_option( AFFIAI_PLUGIN_OPTION_PREFIX . '_shopping_pros_title', __( 'PROS', 'affiliate-ai-lite' ) ); $param['cons_title'] = get_option( AFFIAI_PLUGIN_OPTION_PREFIX . '_shopping_cons_title', __( 'CONS', 'affiliate-ai-lite' ) ); $param['mcount'] = 5; $param['block_name'] = AFFIAI_PLUGIN_NAME; wp_localize_script( AFFIAI_PLUGIN_NAME . '-box-editor-script', 'affiai_blockse', array( 'searchengine' => array_values( $active_se ), ) ); wp_localize_script( AFFIAI_PLUGIN_NAME . '-box-editor-script', 'affiai_blockmerch', array( 'merchants' => array_values( $merch_filter ) ) ); wp_localize_script( AFFIAI_PLUGIN_NAME . '-box-editor-script', 'affiai_blockmerchf', array( 'merchantsf' => array_values( $merch_filter_f ) ) ); wp_localize_script( AFFIAI_PLUGIN_NAME . '-box-editor-script', 'affiai_blockparam', array( 'icons' => array_values( $icon_show ), 'params' => array_values( $param ), ) ); wp_set_script_translations( AFFIAI_PLUGIN_NAME . '-box-editor-script', 'affiliate-ai-lite', AFFIAI_PLUGIN_DIR . 'languages/' ); } //Initializes the Plugin Block add_action( 'init', 'affiai_merch_block_init' ); /** * Formalize details for products (description, pros and cons...) * * @param array $atts An array of attributes. * @param string $label The label to use. * * @return string The generated details. */ function affiai_details_prod( $atts, $label ) { $arrVal = array(); $count = 0; array_shift( $atts ); foreach ( $atts as $val ) { if ( isset( $val ) && $val != '' ) { $val = str_replace( array( '[', ']' ), array( '[', ']' ), esc_html( $val ) ); $count++; } if ( $label == 'title' || $label == 'btn_text' ) { $arrVal[] = str_replace( array( '|', '~' ), '', $val ); } else { $arrVal[] = preg_replace( "/\n/", '~', str_replace( array( '|', '~' ), '-', $val ) ); } } if ( $count > 0 ) { return ' ' . $label . '="' . implode( '|', $arrVal ) . '"'; } else { return ''; } } /** * Gutenberg Renderer * * Renders the Gutenberg block with the given attributes. * * * @param array $attributes An array of attributes for the Gut block. * @param bool $tracking Whether or not to follow up on block change. * * @return string The rendered Gutenberg block (shortcode). */ function affiai_render_gut( $attributes, $tracking = false ) { $empty_options = array_intersect_key( $attributes, array( 'noOffersMessage' => true, 'noOffersKeepProduct' => true, 'textProd' => true, 'RemUrl' => true ) ); unset( $attributes['noOffersMessage'], $attributes['noOffersKeepProduct'], $attributes['textProd'], $attributes['RemUrl'] ); // If the selected merchant is set and the search type is not 'pick', decode the selected merchant. if ( isset( $attributes['selectedMerch'] ) && $attributes['searchType'] != 'pick' ) { $selectedMerch = json_decode( $attributes['selectedMerch'], true ); } // Walk recursively through the attributes and sanitize them. array_walk_recursive( $attributes, function ( &$item, $key ) { if ( !empty($item) && is_string( $item ) ) { if ( substr( $item, 0, 4 ) !== 'http' ) { $item = esc_html( str_replace( array( '[', ']' ), array( '[', ']' ), $item ) ); } } } ); // If in admin mode and not tracking, return. $attributes = array_merge( $attributes, $empty_options ); if ( is_admin() && $tracking == false ) { return; } // If in REST request and format box is not 'select', return. if ( defined( 'REST_REQUEST' ) && REST_REQUEST && ( empty( $attributes['formatBox'] ) || $attributes['formatBox'] != 'select' ) ) { return; } // If search type is 'link' and link URL is not empty, build the link. if ( ! empty( $attributes['searchType'] ) && $attributes['searchType'] == 'link' && ! empty( $attributes['linkUrl'] ) ) { $URL = ( isset( $attributes['linkUrl'] ) ) ? ' url="' . $attributes['linkUrl'] . '"' : ''; $ALIGN = ( isset( $attributes['linkAlign'] ) ) ? ' align="' . $attributes['linkAlign'] . '"' : ' align="left"'; $TEXT = ( isset( $attributes['linkText'] ) ) ? ' text="' . $attributes['linkText'] . '"' : ''; $TYPE = ( isset( $attributes['linkType'] ) ) ? ' type="' . $attributes['linkType'] . '"' : ''; $WIDTH = ( isset( $attributes['linkWidth'] ) ) ? ' width="' . $attributes['linkWidth'] . '"' : ''; $FSIZE = ( isset( $attributes['linkSize'] ) ) ? ' fsize="' . $attributes['linkSize'] . '"' : ''; $COLOR = ( isset( $attributes['btnColor']['hex'] ) ) ? ' color="' . $attributes['btnColor']['hex'] . '"' : ''; $TRACK = ( isset( $attributes['linkTrack'] ) ) ? ' track="1"' : ''; $ICON = ( isset( $attributes['linkIcon'] ) ) ? ' icon="' . $attributes['linkIcon']['value'] . '"' : ''; $OBF = ( isset( $attributes['linkObf'] ) ) ? ' obf="' . $attributes['linkObf'] . '"' : ''; return do_shortcode( '[affiai_link' . $URL . $TEXT . $TYPE . $WIDTH . $TRACK . $ICON . $OBF . $FSIZE . $COLOR . $ALIGN . ']' ); } // Set the count of products to display. $NB = ( isset( $attributes['nbProduct'] ) ) ? ' count="' . $attributes['nbProduct'] . '"' : ' count="3"'; // Set the maximum number of merchants to display. $MAX_MARCH = ( isset( $attributes['nbMerchant'] ) ) ? ' max_merch="' . $attributes['nbMerchant'] . '"' : ''; // Set the promotion percentage. $PROMO = ( isset( $attributes['reduc'] ) && $attributes['reduc'] > 0 ) ? ' promo="' . $attributes['reduc'] . '"' : ''; // Set the search parameters. $SRCH = ''; $MERCH = ''; if ( ! empty( $attributes['searchType2'] ) && ( $attributes['searchType2'] != 'all' && AFFIAI_PLUGIN_NAME == 'affiai' ) ) { $MERCH = ' merchants="amazon"'; } if ( ! empty( $attributes['searchType'] ) && $attributes['searchType'] == 'pick' && ! empty( $attributes['searchPick'] ) ) { if ( ! empty( $attributes['searchType2'] ) && ( $attributes['searchType2'] == 'all' && AFFIAI_PLUGIN_NAME == 'affiai' ) ) { $SRCH = ' pid="'; } else { $SRCH = ' asin="'; } $SRCH .= $attributes['searchPick'] . '"'; } elseif ( ! empty( $attributes['searchType'] ) && $attributes['searchType'] == 'custom' && ! empty( $attributes['UrlMerch1'] ) ) { $SRCH = ' custom="1"'; } elseif ( ! empty( $attributes['searchType'] ) && $attributes['searchType'] != 'keyword' ) { if ( $attributes['searchType'] != 'auto' ) { if ( ! isset( $attributes['searchText'] ) || $attributes['searchText'] == '' ) { return; } elseif ( ! empty( $attributes['searchText'] ) && $attributes['searchType'] == 'pick' && empty($attributes['searchPick']) ) { $SRCH = ' keyword="' . $attributes['searchText'] . '"'; } else { $SRCH = ' ' . $attributes['searchType'] . '="' . $attributes['searchText'] . '"'; } } else { $SRCH = ''; } } elseif ( isset( $attributes['searchText'] ) && $attributes['searchText'] != '' ) { $SRCH = ' keyword="' . $attributes['searchText'] . '"'; } // Set the style parameters. $STYLE = ''; if ( isset( $attributes['addRank'] ) && $attributes['addRank'] == 1 ) { $STYLE = ' rank="on"'; } if ( isset( $attributes['formatBox'] ) ) { $STYLE .= ' template="' . $attributes['formatBox'] . '"'; } else { $STYLE .= ' template="grid"'; } // Set the price parameters. $PRICE = ''; if ( $attributes['searchType'] != 'pick' && $attributes['searchType'] != 'custom' ) { $PRICE = ( isset( $attributes['minPrice'] ) && $attributes['minPrice'] > 0 ) ? ' price="' . $attributes['minPrice'] : ''; if ( isset( $attributes['maxPrice'] ) && $attributes['maxPrice'] > 0 ) { if ( $PRICE != '' ) { $PRICE .= '-' . $attributes['maxPrice'] . '"'; } else { $PRICE = ' price="0-' . $attributes['maxPrice'] . '"'; } } else { if ( $PRICE != '' ) { $PRICE .= '-100000"'; } else { $PRICE = ''; } } } // If the selected merchant is set and the search type is not 'pick', decode the selected merchant. // if ( isset( $attributes['selectedMerch'] ) && $attributes['searchType'] != 'pick' ) { // $selectedMerch = json_decode( $attributes['selectedMerch'], true ); // } if ( isset( $selectedMerch ) && is_array( $selectedMerch ) ) { foreach ( $selectedMerch as $val ) { if ( isset( $val['value'] ) ) { $arrMERCH[] = $val['value']; } } if ( isset( $arrMERCH ) && ! empty( $arrMERCH ) && empty( $MERCH ) ) { $MERCH = ' merchants="' . implode( ',', $arrMERCH ) . '"'; } } // TITLE if ( isset( $attributes['hideTitle'] ) && $attributes['hideTitle'] == true ) { $TITLE = ' title="0"'; } elseif ( isset( $attributes['textProd'] ) ) { $TITLE = affiai_details_prod( $attributes['textProd'], 'title' ); } else { $TITLE = ''; } // BTN if ( isset( $attributes['btnText'] ) ) { $BTN_TEXT = affiai_details_prod( $attributes['btnText'], 'btn_text' ); } else { $BTN_TEXT = ''; } //BTN TYPE if ( isset( $attributes['ctaType'] ) ) { $BTN_TYPE = ' cta_type="' . $attributes['ctaType'] . '"'; } else { $BTN_TYPE = ''; } // DESCR if ( isset( $attributes['hideDescr'] ) && $attributes['hideDescr'] == true ) { $DESCRI = ' descr="0"'; } elseif ( isset( $attributes['Descr'] ) ) { $DESCRI = affiai_details_prod( $attributes['Descr'], 'descr' ); } else { $DESCRI = ''; } // SDATA if ( isset( $attributes['sData'] ) && $attributes['sData'] == true ) { $SDATA = ' sdata="1"'; } else { $SDATA = ''; } // PROS & CONS if ( isset( $attributes['Pros'] ) ) { $PROSI = affiai_details_prod( $attributes['Pros'], 'pros' ); } else { $PROSI = ''; } if ( isset( $attributes['Cons'] ) ) { $CONSI = affiai_details_prod( $attributes['Cons'], 'cons' ); } else { $CONSI = ''; } // RATING if ( isset( $attributes['addRate'] ) && isset( $attributes['nbStar'] ) ) { $arrNOTE = array(); $arrNT = array(); $valNOTE = 0; array_shift( $attributes['addRate'] ); array_shift( $attributes['nbStar'] ); if ( isset( $attributes['ratingType'] ) ) { array_shift( $attributes['ratingType'] ); } foreach ( $attributes['addRate'] as $k => $v ) { if ( $v ) { // var_dump($v); if ( isset( $attributes['nbStar'][ $k ] ) ) { $arrNOTE[] = $attributes['nbStar'][ $k ]; if ( isset( $attributes['ratingType'][ $k ] ) ) { $arrNT[] = $attributes['ratingType'][ $k ]; } else { $arrNT[] = ''; } $valNOTE++; } else { $arrNOTE[] = ''; $arrNT[] = ''; } } else { $arrNOTE[] = ''; $arrNT[] = ''; } } if ( $valNOTE > 0 ) { $NOTA = ' rating="' . implode( '|', $arrNOTE ) . '"'; $NOTAT = ' rating_type="' . implode( '|', $arrNT ) . '"'; } else { $NOTA = ''; $NOTAT = ''; } } else { $NOTA = ''; $NOTAT = ''; } // IMAGE $arrImg = array(); $valImg = 0; $IMG = ''; if ( ! empty( $attributes['custimgt'] ) && ( ! empty( $attributes['mediaId'] ) || ! empty( $attributes['RemUrl'] ) ) ) { array_shift( $attributes['custimgt'] ); if ( ! empty( $attributes['mediaId'] ) ) { array_shift( $attributes['mediaId'] );} if ( ! empty( $attributes['RemUrl'] ) ) { array_shift( $attributes['RemUrl'] );} foreach ( $attributes['custimgt'] as $k => $v ) { if ( $v == 'local' && ! empty( $attributes['mediaId'][ $k ] ) ) { $arrImg[ $k ] = $attributes['mediaId'][ $k ]; $valImg++; } elseif ( $v == 'remote' && ! empty( $attributes['RemUrl'][ $k ] ) ) { $arrImg[ $k ] = str_replace( array( '[', ']' ), array( '[', ']' ), esc_attr( $attributes['RemUrl'][ $k ] ) ); $valImg++; } else { $arrImg[ $k ] = ''; } } } if ( $valImg > 0 ) { $IMG = ' image="' . implode( '|', $arrImg ) . '"'; } // CUSTOM MERCHANTS $arrCMERCH = array(); $arrINT = array(); $valINT = 0; if ( ! empty( $attributes['ccm'] ) && is_array( $attributes['ccm'] ) ) { array_shift( $attributes['ccm'] ); for ( $i = 1; $i <= 3; $i++ ) { if ( ! empty( $attributes[ 'UrlMerch' . $i ] ) ) { array_shift( $attributes[ 'UrlMerch' . $i ] ); if ( ! empty( $attributes[ 'convURL' . $i ] ) ) { array_shift( $attributes[ 'convURL' . $i ] ); } if ( ! empty( $attributes[ 'showPrice' . $i ] ) ) { array_shift( $attributes[ 'showPrice' . $i ] ); } if ( ! empty( $attributes[ 'custPrice' . $i ] ) ) { array_shift( $attributes[ 'custPrice' . $i ] ); } if ( ! empty( $attributes[ 'custCurr' . $i ] ) ) { array_shift( $attributes[ 'custCurr' . $i ] ); } if ( ! empty( $attributes[ 'UrlHome' . $i ] ) ) { array_shift( $attributes[ 'UrlHome' . $i ] ); } foreach ( $attributes[ 'UrlMerch' . $i ] as $k => $v ) { if ( empty( $attributes['ccm'][ $k ] ) ) { $arrINT[ $k ][1] = ''; continue;} if ( $attributes['ccm'][ $k ] < $i ) { continue;} if ( empty( $attributes[ 'UrlMerch' . $i ][ $k ] ) ) { $arrINT[ $k ][ $i ] = ''; continue; } $valINT++; $price = ''; $curr = '€'; $track = ''; if ( ! empty( $attributes[ 'convURL' . $i ][ $k ] ) ) { $track = '::on'; } else { if ( ! empty( $attributes[ 'UrlHome' . $i ][ $k ] ) ) { $track = '::' . $attributes[ 'UrlHome' . $i ][ $k ]; } else { $track = '::off'; } } if ( ! empty( $attributes[ 'showPrice' . $i ][ $k ] ) && !empty($attributes[ 'custPrice' . $i ]) && $attributes[ 'custPrice' . $i ][ $k ] > 0 ) { if ( ! empty( $attributes[ 'custCurr' . $i ][ $k ] ) ) { $curr = $attributes[ 'custCurr' . $i ][ $k ]; } $price = '::' . $attributes[ 'custPrice' . $i ][ $k ] . $curr; } else { $price = '::off'; } $arrINT[ $k ][ $i ] = str_replace( array( '::', '##', '|' ), '', $attributes[ 'UrlMerch' . $i ][ $k ] ) . $track . $price; } } } } if ( $valINT > 0 && ! empty( $arrINT ) && is_array( $arrINT ) ) { foreach ( $arrINT as $k => $v ) { $arrCMERCH[ $k ] = implode( '##', $arrINT[ $k ] ); } $CUSTMERCH = ' custmerch="' . implode( '|', $arrCMERCH ) . '"'; } else { $CUSTMERCH = ''; } $NO_OFFERS = Affiai_Offer_Integrity::block_options( $attributes ); if ($tracking) { return '[affiai' . $SRCH . $PROMO . $NB . $MAX_MARCH . $BTN_TEXT . $BTN_TYPE . $MERCH . $STYLE . $PRICE . $TITLE . $NOTA . $NOTAT . $DESCRI . $PROSI . $CONSI . $IMG . $CUSTMERCH . $SDATA . $NO_OFFERS . ']'; } else { return do_shortcode( '[affiai' . $SRCH . $PROMO . $NB . $MAX_MARCH . $BTN_TEXT . $BTN_TYPE . $MERCH . $STYLE . $PRICE . $TITLE . $NOTA . $NOTAT . $DESCRI . $PROSI . $CONSI . $IMG . $CUSTMERCH . $SDATA . $NO_OFFERS . ']' ); } }