amazon-grid
' . __( 'A Shortcode for displaying Amazon Prodcts in a Grid.', 'amazon-product-in-a-post-plugin' ) . ''; return $text; } public function do_shortcode( $atts, $content = '' ) { global $post; global $apippopennewwindow; global $apippnewwindowhtml; $lgimg_txt = apply_filters( 'amazon-grid-seelgimg-text', __( 'see larger image', 'amazon-product-in-a-post-plugin' ) ); $defaults = array( 'asin' => '', 'locale' => APIAP_LOCALE, 'partner_id' => APIAP_ASSOC_ID, 'private_key' => APIAP_SECRET_KEY, 'public_key' => APIAP_PUB_KEY, 'fields' => apply_filters( 'amazon-grid-fields', 'image,title,new-button', $post ), 'target' => apply_filters( 'amazon-grid-target', '_blank', $post ), 'button_url' => apply_filters( 'amazon-grid-button-img-url', '', $post ), 'use_carturl' => ( bool )apply_filters( 'amazon-grid-carturl', false, $post ), 'columns' => ( int )apply_filters( 'amazon-grid-columns', 3, $post ), 'newWindow' => '', 'button' => '', 'container' => apply_filters('amazon-grid-container','div'), 'container_class' => apply_filters('amazon-grid-container-class','amazon-grid-wrapper'), ); extract( shortcode_atts( $defaults, $atts ) ); if($target == '' && $newWindow == 'true' ) $target = '_blank'; if($target != '' && $newWindow == '' ) $target = ''; $wrap = str_replace(array('<','>'), array('',''),$container); $prodLinkField = apply_filters( 'amazon-grid-link', 'DetailPageURL', $post ); //CartURL $target = $target != '' ? ' target="' . $target . '" ': ''; $target = $target === '' && (bool) $apippopennewwindow ? $apippnewwindowhtml : $target; $new_button_arr = amazon_product_get_new_button_array($locale); if ( $asin != '' ) { $aws_id = $partner_id; $ASIN = ( is_array( $asin ) && !empty( $asin ) ) ? implode( ',', $asin ) : $asin; $asinR = explode( ",", trim( str_replace( ', ', ',', $ASIN ) ) ); /* New Button functionality */ if($button != ''){ $buttonstemp = explode(',', $button ); unset($button); if( count($buttonstemp) === 1 && count($asinR) > 1){ foreach($asinR as $kba => $kbv ){ $button[] = $buttonstemp[0]; } }else{ foreach($buttonstemp as $buttona){ if(!empty($buttona)){ $button[] = $buttona; } } } }else{ $button = array(); } /* END New Button functionality */ $errors = ''; $pxmlNew = amazon_plugin_aws_signed_request( $locale, array( "Operation" => "ItemLookup", "ItemId" => $ASIN, "ResponseGroup" => "Large", "IdType" => "ASIN", "AssociateTag" => $aws_id ), $public_key, $private_key ); $totalResult1 = array(); $totalResult2 = array(); $errorsArr = array(); $buyamzonbutton = apply_filters( 'appip_amazon_button_url', $button_url, '', $locale ); if ( is_array( $pxmlNew ) && !empty( $pxmlNew ) ) { $pxmle = array(); foreach ( $pxmlNew as $pxmlkey => $pxml ) { if ( !is_array( $pxml ) ) { $pxmle = $pxml; } else { $r1 = appip_plugin_FormatASINResult( $pxml, 0, $asinR ); if ( is_array( $r1 ) && !empty( $r1 ) ) { foreach ( $r1 as $ritem ) { $totalResult1[] = $ritem; } } $r2 = appip_plugin_FormatASINResult( $pxml, 1, $asinR ); if ( is_array( $r2 ) && !empty( $r2 ) ) { foreach ( $r2 as $ritem2 ) { $totalResult2[] = $ritem2; } } } } } $resultarr = array(); if ( !empty( $pxmle ) ) { return false; } else { $resultarr1 = isset( $totalResult1 ) && !empty( $totalResult1 ) ? $totalResult1 : array(); $resultarr2 = isset( $totalResult2 ) && !empty( $totalResult2 ) ? $totalResult2 : array(); if ( is_array( $resultarr1 ) && !empty( $resultarr1 ) ) { foreach ( $resultarr1 as $key1 => $result1 ): $mainAArr = ( array )$result1; $otherArr = ( array )$resultarr2[ $key1 ]; $resultarr[ $key1 ] = ( array )array_merge( $mainAArr, $otherArr ); ksort( $resultarr[ $key1 ] ); endforeach; } $arr_position = 0; if ( is_array( $resultarr ) ): $retarr = array(); $newErr = ''; $thenewret = array(); foreach ( $resultarr as $key => $result ): $currasin = $result[ 'ASIN' ]; if ( isset( $result[ 'NoData' ] ) && $result[ 'NoData' ] == '1' ): echo '<'.$wrap.' style="display:none;" class="appip-errors">APPIP ERROR:nodata[' . str_replace( ']-->', ']->', $result[ 'Error' ] ) . ''; elseif ( empty( $result[ 'ASIN' ] ) || $result[ 'ASIN' ] == 'Array' ): echo '<'.$wrap.' style="display:none;" class="appip-errors">APPIP ERROR:nodata[ (' . $key . ') NO DATA '; else : $img2 = ''; $linkURL = ( $prodLinkField == 'CartURL' ) ? str_replace( array( '##REGION##', '##AFFID##', '##SUBSCRIBEID##' ), array( $locale, $aws_id, $public ), $result[ 'CartURL' ] ) : $result[ $prodLinkField ]; if ( $result[ 'Errors' ] != '' ) $newErr = '<'.$wrap.' style="display:none;" class="appip-errors">HIDDEN APIP ERROR(S): ' . $result[ 'Errors' ] . ''; $fielda = is_array( $fields ) ? $fields : explode( ',', str_replace( ' ', '', $fields ) ); if( (bool) $apippopennewwindow ) $nofollow = ' rel="nofollow noopener"'; else $nofollow = ' rel="nofollow"'; $nofollow = apply_filters( 'appip_template_add_nofollow', $nofollow, $result ); foreach ( $fielda as $fieldarr ) { switch ( strtolower( $fieldarr ) ) { case 'title': $NewTitle = $result[ "Title" ]; $retarr[ $currasin ][ $fieldarr ] = '

' . $NewTitle . '

'; break; case 'image': case 'lg-image': $img1 = isset( $result[ "LargeImage" ] ) && $result[ "LargeImage" ] != '' ? ''.(apply_filters('appip_alt_text_main_img',__('Buy Now','amazon-product-in-a-post-plugin'),$currasin)).'': ''; $img2 = $img1 == '' && isset( $result[ "LargeImage" ] ) ? ''.(apply_filters('appip_alt_text_main_img',__('Buy Now','amazon-product-in-a-post-plugin'),$currasin)).'': $img1; $retarr[ $currasin ][ $fieldarr ] = $img2; break; case 'sm-image': $img1 = isset( $result[ "SmallImage" ] ) && $result[ "SmallImage" ] != '' ? ''.(apply_filters('appip_alt_text_main_img',__('Buy Now','amazon-product-in-a-post-plugin'),$currasin)).'': ''; $img2 = $img1 == '' && isset( $result[ "SmallImage" ] ) ? ''.(apply_filters('appip_alt_text_main_img',__('Buy Now','amazon-product-in-a-post-plugin'),$currasin)).'': $img1; $retarr[ $currasin ][ $fieldarr ] = $img2; break; case 'author': $retarr[ $currasin ][ $fieldarr ] = isset( $result[ "ItemAttributes_Author" ] ) && !empty( $result[ "ItemAttributes_Author" ] ) ? 'Author: ' . $result[ "ItemAttributes_Author" ] : ''; break; case 'new-price': case 'new_price': case 'price': $newPrice = isset($result["NewAmazonPricing"]['New']['Price'] ) ? $result[ "NewAmazonPricing" ]['New']['Price'] : '' ; if ( isset( $result["Binding"] ) && "Kindle Edition" === $result["Binding"] ) { //$newPrice = $result["Offers_Offer_OfferListing_Price_FormattedPrice"]; } elseif ( isset( $result['NewAmazonPricing']['New']['Price'] ) ) { $newPrice = $result["NewAmazonPricing"]["New"]["Price"]; } $subscription = $result['SubscriptionLength'] != '' ? $result['SubscriptionLength'] : '' ; if( $subscription !== '' ){ if($result['SubscriptionLengthUnits'] === 'days' && $subscription === '36599999' ) $subscription = '365'; if($subscription === '1') $result['SubscriptionLengthUnits'] = str_replace(array('days','months','years','weeks'),array('day','month','year','week'),$result['SubscriptionLengthUnits']); $retarr[$currasin][$fieldarr] = ''.__('Subscription: ','amazon-product-in-a-post-plugin').' ' . $newPrice . ' for '. $subscription . ' ' . $result['SubscriptionLengthUnits']; }elseif($newPrice !== ''){ $retarr[$currasin][$fieldarr] = ''.__('New:','amazon-product-in-a-post-plugin').' ' . $newPrice; }else{ $retarr[$currasin][$fieldarr] = __('Check Amazon for Pricing','amazon-product-in-a-post-plugin'); } break; case 'used': $usedPrice = isset( $result[ "OfferSummary_LowestUsedPrice_FormattedPrice" ] ) ? $result[ "OfferSummary_LowestUsedPrice_FormattedPrice" ] : ''; if ( isset( $result[ "NewAmazonPricing" ][ "Used" ][ "Price" ] ) ) { $usedPrice = $result[ "NewAmazonPricing" ][ "Used" ][ "Price" ]; } if ( $usedPrice != '' ){ $retarr[ $currasin ][ $fieldarr ] = ''.__('Used:','amazon-product-in-a-post-plugin').' ' . $newPrice; } break; case 'new-button': $button_class = ' class="btn btn-primary"'; $button_txt = __('Read More','amazon-product-in-a-post-plugin'); $retarr[ $currasin ][ $fieldarr ] = '' . $button_txt . ''; break; case 'button': if ( isset( $button_url[ $arr_position ] ) ) { $retarr[ $currasin ][ $fieldarr ] = ''.(apply_filters('appip_amazon_button_alt_text',__('Buy Now','amazon-product-in-a-post-plugin'),$currasin)).''; } else { if(isset($button[$arr_position])){ $bname = $button[$arr_position]; $brounded = strpos($bname,'rounded') !== false ? true : false; $bclass = isset($new_button_arr[$bname]['color']) ? 'amazon__btn'.$new_button_arr[$bname]['color'].' amazon__price--button--style'.( $brounded ? ' button-rounded' : '') : 'amazon__btn amazon__price--button--style'; $btext = isset($new_button_arr[$bname]['text']) ? esc_attr($new_button_arr[$bname]['text']) : _x('Buy Now', 'button text', 'amazon-product-in-a-post-plugin' ); $retarr[$currasin][$fieldarr] = '' . $btext . ''; }else{ $retarr[ $currasin ][ $fieldarr ] = ''.(apply_filters('appip_amazon_button_alt_text',__('Buy Now','amazon-product-in-a-post-plugin'),$currasin)).''; } } break; case 'large-image-link': $appip_text_lgimage = apply_filters('appip_text_lgimage', __( "See larger image", 'amazon-product-in-a-post-plugin' )); if(isset($result['LargeImage']) && $result['LargeImage'] !== '' ){ $retarr[$currasin][$fieldarr] = '<'.$wrap.' class="amazon-image-link-wrapper">'.$appip_text_lgimage.''; } break; default: if ( preg_match( '/\_clean$/', $fieldarr ) ) { $tempfieldarr = str_replace( '_clean', '', $fieldarr ); $retarr[ $currasin ][ $fieldarr ] = isset( $result[ $tempfieldarr ] ) && $result[ $tempfieldarr ] != '' ? $result[ $tempfieldarr ] : ''; } else { if ( isset( $result[ $fieldarr ] ) && $result[ $fieldarr ] != '' && $result[ $fieldarr ] != '0' ) { $retarr[ $currasin ][ $fieldarr ] = $result[ $fieldarr ]; } else { $retarr[ $currasin ][ $fieldarr ] = ''; } } break; } } endif; /* NEW Filter Version - only applies filter to current ASIN while not breaking the filter. */ $temparr = array('temp' => $retarr[ $currasin ] ); $temparr = apply_filters( 'amazon-grid-elements-filter', $temparr ); $retarr[ $currasin ] = $temparr['temp']; /* OLD Filter version - applied the filter to the entire array everytime instead of just the current ASIN. BAD!! $retarr = apply_filters( 'amazon-grid-elements-filter', $retarr ); */ if ( is_array( $retarr[ $currasin ] ) && !empty( $retarr[ $currasin ] ) ) { $thenewret[] = '<'.$wrap.' class="amazon-grid-element amz-grid-' . $columns . '">'; foreach ( $retarr[ $currasin ] as $key => $val ) { if ( $key != '' ) { if ( preg_match( '/\_clean$/', $key ) ) $thenewret[] = $val; else $thenewret[] = '<'.$wrap.' class="amazon-grid-' . $key . '">' . $val . ''; } } $thenewret[] = ''; } $arr_position++; endforeach; if ( $newErr != '' ) echo $newErr; if ( is_array( $thenewret ) ) { $final = '<'.$wrap.' class="'.$container_class.'">' . implode( "\n", $thenewret ) . ''; return $final; } return false; endif; } } else { return false; } } public function do_added_shortcode_help_content( $content = array(), $current_tab = '' ) { $pageTxtArr = array(); $pageTxtArr[] = ' '; $content[] = implode( "\n", $pageTxtArr ); return $content; } public function do_added_shortcode_help_tab($tab = array(), $current_tab = '' ) { $tab[] = '' . __( 'Product Grid', 'amazon-product-in-a-post-plugin' ) . ''; return $tab; } public function front_enqueue() { if ( file_exists( get_stylesheet_directory() . '/appip-styles.css' ) || file_exists( get_stylesheet_directory() . '/css/appip-styles.css' ) ) { wp_enqueue_style( 'amazon-grid-shortcode', dirname( plugin_dir_url( __FILE__ ) ) . '/css/amazon-grid.css', array('appip-theme-styles'), filemtime( dirname( plugin_dir_path( __FILE__ ) ) . '/css/amazon-grid.css') ); } else { wp_enqueue_style( 'amazon-grid-shortcode', dirname( plugin_dir_url( __FILE__ ) ) . '/css/amazon-grid.css', array('amazon-default-styles'), filemtime( dirname( plugin_dir_path( __FILE__ ) ) . '/css/amazon-grid.css') ); } } public function grid_columns( $count = 3 ) { if ( $count == 0 ) $count = 3; return ( int )$count; } public function add_fields( $fields = '' ) { $tempfields = is_array( $fields ) && !empty( $fields ) ? explode( ',', $fields ) : array(); if ( is_array( $tempfields ) && !empty( $tempfields ) ) return $tempfields; //this is an oevrride for shortcode if nothing present $tempfields[] = 'image'; $tempfields[] = 'title'; $tempfields[] = 'author'; $tempfields[] = 'price'; $tempfields[] = 'button'; return implode( ",", $tempfields ); } } new AmazonProduct_Shortcode_Grid( 'amazon-grid' ); function appip_grid_php_block_init() { if( function_exists('register_block_type') ){ wp_register_script( 'amazon-grid', plugins_url( '/blocks/php-block-grid.js', __FILE__ ), array( 'wp-blocks', 'wp-element', 'wp-components', 'wp-editor' ) ); wp_register_style( 'amazon-grid-styles', plugins_url( '/blocks/css/php-block-grid.css', __FILE__ ), array( 'wp-edit-blocks' ), filemtime( plugin_dir_path( __FILE__ ) . 'blocks/css/php-block-grid.css' ) ); register_block_type( 'amazon-pip/amazon-grid', array( 'attributes' => array( 'fields' => array( 'type' => 'string', ), 'asin' => array( 'type' => 'string', ), 'columns' => array( 'type' => 'int', ), 'newWindow' => array( 'type' => 'string', ), ), 'editor_style' => 'amazon-grid-styles', 'editor_script' => 'amazon-grid', // The script name we gave in the wp_register_script() call. 'render_callback' => array('AmazonProduct_Shortcode_Grid', 'do_shortcode'), ) ); } } //add_action( 'init', 'appip_grid_php_block_init'); }