get_results( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE post_title = %s $post_type_where AND post_status = 'publish'", $page_title, $post_type ? $post_type : '' ) ); if ( $results ){ $output = array(); foreach ( $results as $post ){ $output[] = $post; } return $output; } return null; } /*********************************************************************************/ /*amzafs_truncateAmzString*/ /*********************************************************************************/ function amzafs_truncateAmzString($string, $length, $stopanywhere=false) { //truncates a string to a certain char length, stopping on a word if not specified otherwise. if (strlen($string) > $length) { //limit hit! $string = mb_substr($string,0,($length -3)); if ($stopanywhere) { //stop anywhere $string .= '...'; } else{ //stop on a word. $string = mb_substr($string,0,strrpos($string,' ')).'...'; } } return $string; } /*********************************************************************************/ /*Add shortcode*/ /*********************************************************************************/ add_shortcode("amzcode","amzafs_amzshortcodemain"); function amzafs_amzshortcodemain( $attributes, $content = null ) { $amazonContents=""; extract( shortcode_atts( array( 'class' => '', 'lang' => '', 'autoplay' => '' ), $attributes ) ); $idamzshortcode=$attributes[0];//Get id del shortcode $keyword = get_term_meta( $idamzshortcode, '__term_meta_text', true ); //Get keyword del shortcode (cat) from id $overrideLayout = get_term_meta( $idamzshortcode, '__term_meta_layout', true ); $overrideCount = get_term_meta( $idamzshortcode, '__term_meta_productcount', true ); if (($overrideCount=="")||(empty($overrideCount))) $overrideCount=99; $slugcategoria=get_term_by('id', $idamzshortcode, 'amazon_shortcode'); $slugfinal=$slugcategoria->slug; $obj_term = get_term($idamzshortcode, 'amazon_shortcode'); $cuentatotal=$obj_term->count; /*Search product by keyword in Amazon API*/ //DEBUG FORZAR AMAZON //$cuentatotal=0; $cssRow=" "; $amz_options=get_option( 'amz_short_options' ); $select_layout=trim($amz_options["select_layout"]); //Layout grid (1) or row (2) $frontEndContent=""; $layoutTypeClass=""; if ($select_layout==1) { //Grid layout $layoutTypeClass="grid"; } elseif ($select_layout==2) { //Table layout if ( ! is_admin() ) { $frontEndContent= $cssRow; $layoutTypeClass="row"; } } //Layout override in shortcode settings if ($overrideLayout==1) { //Grid layout $layoutTypeClass="grid"; } elseif ($overrideLayout==2) { //Table layout if ( ! is_admin() ) { $frontEndContent= $cssRow; $layoutTypeClass="row"; } } $svgAmazon=''; if ($cuentatotal<=0){ $amazonContents=amzafs_searchItemsAmz($keyword, $idamzshortcode); $frontEndContent.="
$svgAmazon
Please, reload the page to see the Amazon products. If you still not see them, check the plugin settings.
"; } else { //Show database stored products global $post;$backup=$post; $queryCustom = new WP_Query(array( 'post_type' => 'amz_product', // name of post type. 'posts_per_page' => $overrideCount, // name of post type. 'tax_query' => array( array( 'taxonomy' => 'amazon_shortcode', // taxonomy name 'field' => $slugfinal, // term_id, slug or name 'terms' => $idamzshortcode, // term id, term slug or term name ) ) )); if ( $queryCustom->have_posts() ) : if ( ! is_admin() ) { $frontEndContent.="
"; while ( $queryCustom->have_posts() ) : $queryCustom->the_post(); include plugin_dir_path( __FILE__ ) . 'product-template.php'; $lastProductTimestamp=get_post_time(); endwhile; $frontEndContent.="
"; $post=$backup; wp_reset_query(); } else $lastProductTimestamp=time()+999999999999; else: $frontEndContent="
$svgAmazon
Please, reload the page to see the Amazon products. If you still not see them, check the plugin settings.
"; endif; //Si han caducado, vuelvo a sacar productos de amazon con searchItems //¿los borro después de haber comprobado que he sacado resultados nuevos? $currentTimestamp=time(); $amz_options=get_option( 'amz_short_options' ); $access_key=$amz_options["access_key"]; $secret_key=$amz_options["secret_key"]; $asoc_tag=$amz_options["asoc_tag"]; $update_time=$amz_options["update_time"]; $diferenciaTiempoSegundos=$currentTimestamp-$lastProductTimestamp; //echo "
$diferenciaTiempoSegundos > = $update_time
"; if (($diferenciaTiempoSegundos>=$update_time)&&($update_time!=0)) { //ACTUALIZANDO RESULTADOS $post_id=amzafs_searchItemsAmz($keyword, $idamzshortcode); //echo "
ACTUALIZANDO RESULTADOS
"; } } //return "
OUTPUT ||| $cuentatotal productos ||| ID shortcode: $idamzshortcode ||| keyword: $keyword ||| ".$amazonContents; return $frontEndContent; } /*********************************************************************************/ /*Create custom post types*/ /*********************************************************************************/ function amzafs_create_post_type() { register_post_type( 'amz_product', array( 'labels' => array( 'name' => __( 'AMZ Shortcodes' ), /*'all_items' => 'Products', 'add_new_item' => 'Add new',*/ 'add_new' => __( 'Add Product', 'text_domain' ), 'all_items' => __( 'All Products', 'text_domain' ), 'parent_item' => __( 'Parent Item', 'text_domain' ), 'parent_item_colon' => __( 'Parent Item:', 'text_domain' ), 'new_item_name' => __( 'New Item Name', 'text_domain' ), 'add_new_item' => __( 'Add Product', 'text_domain' ), 'edit_item' => __( 'Edit Item', 'text_domain' ), 'update_item' => __( 'Update Item', 'text_domain' ), 'view_item' => __( 'View Item', 'text_domain' ), 'separate_items_with_commas' => __( 'Separate items with commas', 'text_domain' ), 'add_or_remove_items' => __( 'Add or remove items', 'text_domain' ), 'choose_from_most_used' => __( 'Choose from the most used', 'text_domain' ), 'popular_items' => __( 'Popular Items', 'text_domain' ), 'search_items' => __( 'Search Items', 'text_domain' ), 'not_found' => __( 'Not Found', 'text_domain' ), 'no_terms' => __( 'No items', 'text_domain' ), 'items_list' => __( 'Items list', 'text_domain' ), 'singular_name' => __( 'Amazon Shortcode' ) ), 'public' => true, 'publicly_queryable' => false, 'menu_icon' => 'dashicons-cart', 'taxonomies' => array( 'amazon_shortcode' ), 'supports' => array( 'title', 'editor'), 'has_archive' => true, )); } add_action( 'init', 'amzafs_create_post_type' ); /*********************************************************************************/ /*Show metaboxes in backend*/ /*********************************************************************************/ function amzafs_wpamz_custom_box_imgurl($post) { $amzImg = get_post_meta($post->ID, '_wpamz_meta_imgurl', true); ?> ID, '_wpamz_meta_price', true); ?> ID, '_wpamz_meta_affilink', true); ?> ID, '_wpamz_meta_key', true); ?> ".$screen->id."
", 3, "milog.log"); if ( is_admin() && ($screen->id == 'edit-amazon_shortcode') ) { $queried_object = get_queried_object(); $term_id = $queried_object->term_id; $idCategory = $_REQUEST['tag_ID']; $slug=$queried_object->slug; if (isset($idCategory)){ echo '
Copy & paste this shortcode where you want the products to appear:
'; echo '[amzcode "'.$idCategory.'"]'; echo '
Delete all the products linked to a shortcode and publish the shortcode to regenerate them. The shortcodes with 0 products will search and save them using Amazon Product Advertising API 5.0.
'; } ?> id; if ( is_admin() && ($screen->id == 'amz_product') ) { function add_content_after_editor() { global $post; $id = $post->ID; $imageURL = get_post_meta($id, '_wpamz_meta_imgurl', true); if (!empty($imageURL)){ echo '
'; echo ""; echo '
'; } } add_action( 'edit_form_after_title', 'add_content_after_editor' ); } } add_action( 'admin_notices', 'amzafs_options_instructions_example2' ); /*********************************************************************************/ /*Product custom post type admin columns*/ /*********************************************************************************/ // Add the custom columns to the post type: add_filter( 'manage_amz_product_posts_columns', 'amzafs_set_custom_edit_amz_product_columns' ); function amzafs_set_custom_edit_amz_product_columns($columns) { //unset( $columns['author'] ); $columns['book_author'] = __( 'Image', 'your_text_domain' ); //$columns['publisher'] = __( 'Publisher', 'your_text_domain' ); return $columns; } // Add the data to the custom columns for the post type: add_action( 'manage_amz_product_posts_custom_column' , 'amzafs_custom_amz_product_column', 1, 2 ); function amzafs_custom_amz_product_column( $column, $post_id ) { switch ( $column ) { case 'book_author' : $imageURL = get_post_meta($post_id, '_wpamz_meta_imgurl', true); print_r($terms); if ( is_string( $imageURL ) ) echo ""; else _e( 'Unable to get image(s)', 'your_text_domain' ); break; case 'publisher' : echo get_post_meta( $post_id , 'publisher' , true ); break; } } /*********************************************************************************/ /*Reorder colums post type admin*/ /*********************************************************************************/ /* function add_ourteam_columns ( $columns ) { unset($columns['title']); unset($columns['tags']); unset($columns['date']); return array_merge ( $columns, array ( 'name' => __ ('name'), 'category' => __ ( 'Shortcode' ), 'image' => __ ( 'Image' ), 'date' => __('Date') )); } add_filter ( 'manage_amazon_shortcode_posts_columns', 'add_ourteam_columns' ); */ /*********************************************************************************/ // Register & fill the column Shortcode & Keyword in categories (Shortcodes) /*********************************************************************************/ function amzafs_department_add_dynamic_hooks() { $taxonomy = 'amazon_shortcode'; add_filter( 'manage_' . $taxonomy . '_custom_column', 'amzafs_department_taxonomy_rows',15, 3 ); add_filter( 'manage_edit-' . $taxonomy . '_columns', 'amzafs_department_taxonomy_columns' ); } add_action( 'admin_init', 'amzafs_department_add_dynamic_hooks' ); function amzafs_department_taxonomy_columns( $original_columns ) { $new_columns = $original_columns; array_splice( $new_columns, 1 ); $new_columns['frontpage'] = esc_html__( 'Shortcode', 'taxonomy-images' ); $new_columns['frontpage2'] = esc_html__( 'Keyword', 'taxonomy-images2' ); return array_merge( $new_columns, $original_columns ); } function amzafs_department_taxonomy_rows( $row, $column_name, $term_id ) { $t_id = $term_id; $meta = get_option( "taxonomy_$t_id" ); if ( 'frontpage' === $column_name ) { return '[amzcode "'.$term_id.'"]'; } if ( 'frontpage2' === $column_name ) { $value = amzafs___get_term_meta_text( $term_id ); return $value; } } /********************************/ /*Shortcode (category) settings*/ /******************************/ // REGISTER TERM META add_action( 'init', 'amzafs____register_term_meta_text' ); add_action( 'init', 'amzafs___register_term_meta_layout' ); add_action( 'init', 'amzafs___register_term_meta_productcount' ); function amzafs____register_term_meta_text() { register_meta( 'term', '__term_meta_text', 'amzafs___sanitize_term_meta_text' ); } function amzafs___register_term_meta_layout() { register_meta( 'term', '__term_meta_layout', 'amzafs___sanitize_term_meta_layout' ); } function amzafs___register_term_meta_productcount() { register_meta( 'term', '__term_meta_productcount', 'amzafs___sanitize_term_meta_productcount' ); } //SANITIZE function amzafs___sanitize_term_meta_text ( $value ) { return sanitize_text_field ($value); } function amzafs___sanitize_term_meta_layout ( $value ) { return sanitize_text_field ($value); } function amzafs___sanitize_term_meta_productcount ( $value ) { return sanitize_text_field ($value); } // GETTER (will be sanitized) function amzafs___get_term_meta_text( $term_id ) { $value = get_term_meta( $term_id, '__term_meta_text', true ); $value = amzafs___sanitize_term_meta_text( $value ); return $value; } function amzafs___get_term_meta_layout ($term_id ) { $value = get_term_meta( $term_id, '__term_meta_layout', true ); $value = amzafs___sanitize_term_meta_layout( $value ); return $value; } function amzafs___get_term_meta_productcount ($term_id ) { $value = get_term_meta( $term_id, '__term_meta_productcount', true ); $value = amzafs___sanitize_term_meta_productcount( $value ); return $value; } // ADD FIELD TO CATEGORY TERM PAGE add_action( 'amazon_shortcode_add_form_fields', 'amzafs___add_form_field_term_meta_text' ); function amzafs___add_form_field_term_meta_text() { ?>

The keyword of the product that you want to search in Amazon.

Override default (main settings) products layout. Purchase premium version to unlock it and make me happy!

The max amount of products that you want to show. 0 or empty will show ALL stored items. Purchase premium version to unlock it and make me happy!

term_id ); if ( ! $value ) $value = ""; ?>

The keyword of the product that you want to search in Amazon.

term_id ); if ( ! $value ) $value = ""; ?>

Override default (main settings) products layout. Purchase premium version to unlock it and make me happy!

term_id ); if ( ! $value ) $value = ""; ?>

The max amount of products that you want to show. 0 or empty will show ALL stored items. Purchase premium version to unlock it and make me happy!

"; else _e( 'Unable to get image(s)', 'your_text_domain' ); break; case 'publisher' : echo get_post_meta( $post_id , 'publisher' , true ); break; } } /*********************************************************************************/ // Register Custom Taxonomy /*********************************************************************************/ function amzafs_custom_taxonomy() { $labels = array( 'name' => _x( 'Shortcodes', 'Taxonomy General Name', 'text_domain' ), 'singular_name' => _x( 'AMZ shortcode', 'Taxonomy Singular Name', 'text_domain' ), 'menu_name' => __( 'Shortcodes', 'text_domain' ), 'all_items' => __( 'All Items', 'text_domain' ), 'parent_item' => __( 'Parent Item', 'text_domain' ), 'parent_item_colon' => __( 'Parent Item:', 'text_domain' ), 'new_item_name' => __( 'New Item Name', 'text_domain' ), 'add_new_item' => __( 'Add New Item', 'text_domain' ), 'edit_item' => __( 'Edit Item', 'text_domain' ), 'update_item' => __( 'Update Item', 'text_domain' ), 'view_item' => __( 'View Item', 'text_domain' ), 'separate_items_with_commas' => __( 'Separate items with commas', 'text_domain' ), 'add_or_remove_items' => __( 'Add or remove items', 'text_domain' ), 'choose_from_most_used' => __( 'Choose from the most used', 'text_domain' ), 'popular_items' => __( 'Popular Items', 'text_domain' ), 'search_items' => __( 'Search Items', 'text_domain' ), 'not_found' => __( 'Not Found', 'text_domain' ), 'no_terms' => __( 'No items', 'text_domain' ), 'items_list' => __( 'Items list', 'text_domain' ), 'items_list_navigation' => __( 'Items list navigation', 'text_domain' ), ); $args = array( 'labels' => $labels, 'hierarchical' => true, 'parent_item' => false, 'public' => true, 'show_ui' => true, 'show_admin_column' => true, 'show_in_nav_menus' => true, 'show_tagcloud' => true, ); register_taxonomy( 'amazon_shortcode', array( 'amz_shortcode' ), $args ); } add_action( 'init', 'amzafs_custom_taxonomy', 0 ); /*Plugin settings link*/ add_filter( 'plugin_action_links_' . plugin_basename(__FILE__), 'amzafs_my_plugin_action_links' ); function amzafs_my_plugin_action_links( $links ) { global $premiumURL; $links[] = 'Settings'; $links[] = 'Go Premium'; return $links; }