Support » Developing with WordPress » Hack function for “post” to “post AND product”

  • Hi

    I post here for the first time. Hope this is the good forum lol

    I need help for update functions that display “rate system” for post_type=post. (using in Boombox thème)

    I need this function work for “classic post” (as it work fine with this code), and for post_type=product.

    Somebody can help me with this? it would be really really appreciate 🙂

    /**
     * Get Trending Posts
     *
     * @param       $type
     * @param       $posts_per_page
     * @param array $params
     *
     * @return bool|WP_Query
     */
    function boombox_get_trending_posts( $type, $posts_per_page, $params = array() ) {
    
    	$params = wp_parse_args( $params, array(
    		'paged'                 => 1,
    		'is_grid'               => false,
    		'page_ad'               => 'none',
    		'instead_ad'            => 1,
    		'page_newsletter'       => 'none',
    		'instead_newsletter'    => 1,
    		'page_product'          => 'none',
    		'page_product_position' => 1,
    		'page_product_count'    => 1,
    		'is_widget'             => false,
    		'ignore_sticky_posts'   => true,
    	) );
    
    	static $boombox_trending_query;
    
    	if( $params[ 'is_widget' ] ) {
    		unset( $boombox_trending_query[ $type ] );
    	}
    
    	if( ! isset( $boombox_trending_query[ $type ] ) ) {
    		$post_type = 'post' && 'product';
    		$query = null;
    		$fake_meta_key = null;
    		$criteria_name = boombox_get_theme_option( 'extras_post_ranking_system_trending_conditions' );
    
    		if( 'trending' == $type ) {
    			$time_range = 'day';
    			$posts_count = boombox_get_theme_option( 'extras_post_ranking_system_trending_posts_count' );
    			$fake_meta_key = 'boombox_keep_trending';
    		} else if( 'hot' == $type ) {
    			$time_range = 'week';
    			$posts_count = boombox_get_theme_option( 'extras_post_ranking_system_hot_posts_count' );
    			$fake_meta_key = 'boombox_keep_hot';
    		} else if( 'popular' == $type ) {
    			$time_range = 'month';
    			$posts_count = boombox_get_theme_option( 'extras_post_ranking_system_popular_posts_count' );
    			$fake_meta_key = 'boombox_keep_popular';
    		}
    
    		$is_module_active = boombox_module_management_service()->is_module_active( 'prs' );
    		if( $is_module_active && Boombox_Rate_Criteria::get_criteria_by_name( $criteria_name ) && $job = Boombox_Rate_Job::get_job_by_name( $type ) ) {
    			$args = array(
    				'nopaging'            => false,
    				'ignore_sticky_posts' => $params[ 'ignore_sticky_posts' ],
    			);
    
    			if( -1 != $posts_per_page ) {
    				$args[ 'posts_per_page' ] = $posts_per_page;
    			} else {
    				$args[ 'nopaging' ] = true;
    			}
    
    			if( $params[ 'paged' ] ) {
    				$args[ 'paged' ] = $params[ 'paged' ];
    			}
    
    			$is_adv_enabled = boombox_is_adv_enabled( $params[ 'page_ad' ] );
    			$is_newsletter_enabled = boombox_is_newsletter_enabled( $params[ 'page_newsletter' ] );
    			$is_product_enabled = boombox_is_product_enabled( $params[ 'page_product' ] );
    
    			if( $is_adv_enabled || $is_newsletter_enabled || $is_product_enabled ) {
    				Boombox_Loop_Helper::init( array(
    					'is_adv_enabled'        => $is_adv_enabled,
    					'instead_adv'           => $params[ 'instead_ad' ],
    					'is_newsletter_enabled' => $is_newsletter_enabled,
    					'instead_newsletter'    => $params[ 'instead_newsletter' ],
    					'is_product_enabled'    => $is_product_enabled,
    					'page_product_position' => $params[ 'page_product_position' ],
    					'page_product_count'    => $params[ 'page_product_count' ],
    					'skip'                  => $params[ 'is_grid' ],
    					'posts_per_page'        => $posts_per_page,
    					'paged'                 => $params[ 'paged' ],
    				) );
    			}
    
    			$rate_query = new Boombox_Rate_Query( $args, $job, $fake_meta_key );
    
    			$query = $rate_query->get_wp_query();
    
    		} else if( 'most_shared' == $criteria_name || 'recent' === $criteria_name || 'featured' === $criteria_name ) {
    			$categories = array();
    			$tags = array();
    			$is_page_query = true;
    			$excluded_categories = array();
    			$is_live = false;
    			$query = boombox_get_posts_query( $criteria_name, $time_range, array(
    				'category' => $categories,
    				'tag'      => $tags,
    			), array(
    				'posts_per_page'      => $posts_per_page,
    				'post_type'           => $post_type,
    				'paged'               => $params[ 'paged' ],
    				'posts_count'         => $posts_count,
    				'is_grid'             => $params[ 'is_grid' ],
    				'is_page_query'       => $is_page_query,
    				'excluded_categories' => $excluded_categories,
    				'is_live'             => $is_live,
    				'fake_meta_key'       => $fake_meta_key,
    			) );
    		}
    
    		if( $query ) {
    			if( 'trending' == $type ) {
    				$boombox_trending_query[ 'trending' ] = $query;
    			} else if( 'hot' == $type ) {
    				$boombox_trending_query[ 'hot' ] = $query;
    			} else if( 'popular' == $type ) {
    				$boombox_trending_query[ 'popular' ] = $query;
    			}
    
    			return $boombox_trending_query[ $type ];
    		}
    
    	} else {
    		return $boombox_trending_query[ $type ];
    	}
    
    	return false;
    }
    
    /**
     * Get trending page id by type
     *
     * @param $type 'trending' |'hot' |'popular'
     *
     * @return int|mixed
     */
    function boombox_get_trending_page_id( $type ) {
    	$customize_setting_slug = "extras_post_ranking_system_{$type}_page";
    	$trending_page_id = boombox_get_theme_option( $customize_setting_slug );
    
    	return $trending_page_id;
    }
    
    /**
     * Return true if is trending page
     *
     * @param     $type 'trending' |'hot' |'popular'
     * @param int $post_id
     *
     * @return bool
     */
    function boombox_is_trending_page( $type, $post_id = 0 ) {
    	$is_trending = false;
    
    	if( 0 == $post_id ) {
    		global $post;
    		if( is_object( $post ) ) {
    			$post_id = $post->ID;
    		}
    	}
    
    	if( $post_id ) {
    		$trending_page_id = boombox_get_trending_page_id( $type );
    		$is_trending = ( $trending_page_id == $post_id );
    	}
    
    	return $is_trending;
    }
    
    /**
     * Check, if post is trending
     *
     * @param string $type     trending,hot,popular
     * @param null|int|WP_Post $post Post
     *
     * @return bool
     */
    function boombox_is_post_trending( $type, $post = null ) {
    	$post = get_post( $post );
    
    	$time_range = false;
    	$posts_count = false;
    	$set = boombox_get_theme_options_set( array(
    		'extras_post_ranking_system_trending_conditions',
    		'extras_post_ranking_system_trending_enable',
    		'extras_post_ranking_system_hot_enable',
    		'extras_post_ranking_system_popular_enable',
    		'extras_post_ranking_system_trending_posts_count',
    		'extras_post_ranking_system_hot_posts_count',
    		'extras_post_ranking_system_popular_posts_count',
    	) );
    
    	if( ( 'trending' === $type && ! $set[ 'extras_post_ranking_system_trending_enable' ] ) || ( 'hot' === $type && ! $set[ 'extras_post_ranking_system_hot_enable' ] ) || ( 'popular' === $type && ! $set[ 'extras_post_ranking_system_popular_enable' ] ) ) {
    		return false;
    	}
    
    	$is_module_active = boombox_module_management_service()->is_module_active( 'prs' );
    	if( $is_module_active && Boombox_Rate_Criteria::get_criteria_by_name( $set[ 'extras_post_ranking_system_trending_conditions' ] ) ) {
    		$keep_trending = ! ! ( boombox_get_post_meta( $post->ID, 'boombox_keep_' . $type ) );
    
    		if( ! $keep_trending && $job = Boombox_Rate_Job::get_job_by_name( $type ) ) {
    			$keep_trending = Boombox_Rate_Cron::is_post_rated( $job, $post->ID );
    		}
    
    		return $keep_trending;
    	}
    
    	if( 'trending' === $type && $set[ 'extras_post_ranking_system_trending_enable' ] && $set[ 'extras_post_ranking_system_trending_posts_count' ] ) {
    		$time_range = 'day';
    		$posts_count = $set[ 'extras_post_ranking_system_trending_posts_count' ];
    	} else {
    		if( 'hot' === $type && $set[ 'extras_post_ranking_system_hot_enable' ] && $set[ 'extras_post_ranking_system_hot_posts_count' ] ) {
    			$time_range = 'week';
    			$posts_count = $set[ 'extras_post_ranking_system_hot_posts_count' ];
    		} else {
    			if( 'popular' === $type && $set[ 'extras_post_ranking_system_popular_enable' ] && $set[ 'extras_post_ranking_system_popular_posts_count' ] ) {
    				$time_range = 'month';
    				$posts_count = $set[ 'extras_post_ranking_system_popular_posts_count' ];
    			}
    		}
    	}
    
    	$is_trending = false;
    	if( $time_range && $posts_count && in_array( $set[ 'extras_post_ranking_system_trending_conditions' ], array(
    			'most_shared',
    			'recent',
    		) ) ) {
    
    		$cache_key = 'trending_posts_' . md5( json_encode( array( 'type' => $type, 'count' => $posts_count ) ) );
    		$trending_ids = boombox_cache_get( $cache_key );
    		if( false === $trending_ids ) {
    			$trending_ids = array();
    			$query = boombox_get_trending_posts( $type, $posts_count );
    			if ( is_object( $query ) && $query->have_posts() ) {
    				$trending_ids = wp_list_pluck( $query->posts, 'ID' );
    				boombox_cache_set( $cache_key, $trending_ids );
    			}
    		}
    		$is_trending  = in_array( $post->ID, $trending_ids );
    	}
    
    	return $is_trending;
    }
    
    /**
     * Return true, if is trending, hot or popular page
     *
     * @param int $post_id Page ID to check
     *
     * @return bool
     */
    function boombox_is_trending( $post_id = 0 ) {
    
    	$post_id = absint( $post_id );
    	if( ! $post_id ) {
    		$post_id = get_the_ID();
    	}
    	$cache_key = 'is_trending_' . $post_id;
    
    	$is_trending = boombox_cache_get( $cache_key );
    	if( ! $is_trending ) {
    
    		$is_trending = false;
    
    		if( $post_id && ( boombox_is_trending_page( 'trending', $post_id ) || boombox_is_trending_page( 'hot', $post_id ) || boombox_is_trending_page( 'popular', $post_id ) ) ) {
    			$is_trending = true;
    		}
    
    		boombox_cache_set( $cache_key, $is_trending );
    
    	}
    
    	return $is_trending;
    }
    
    /**
     * Get Trending Navigation Items
     *
     * @return array
     */
    function boombox_get_trending_navigation_items() {
    	$set = boombox_get_theme_options_set( array(
    		'extras_badges_trending_icon',
    		'extras_post_ranking_system_trending_enable',
    		'extras_post_ranking_system_hot_enable',
    		'extras_post_ranking_system_popular_enable',
    	) );
    
    	$trending_pages = array(
    		'trending' => array(
    			'page'     => 'trending',
    			'icon'     => $set[ 'extras_badges_trending_icon' ],
    			'enable'   => $set[ 'extras_post_ranking_system_trending_enable' ],
    		),
    		'hot'      => array(
    			'page'     => 'hot',
    			'icon'     => 'hot',
    			'enable' => $set[ 'extras_post_ranking_system_hot_enable' ],
    		),
    		'popular'  => array(
    			'page'     => 'popular',
    			'icon'     => 'popular',
    			'enable' => $set[ 'extras_post_ranking_system_popular_enable' ],
    		),
    	);
    	$trending_pages_nav = array();
    	foreach ( $trending_pages as $trending_page_key => $tr_page_options ) {
    		$page_id = boombox_get_trending_page_id( $tr_page_options[ 'page' ] );
    		if( $page_id && $tr_page_options[ 'enable' ] ) {
    			$trending_page = get_post( $page_id );
    			if( $trending_page ) {
    				$trending_pages_nav[ $trending_page_key ][ 'id' ] = $page_id;
    				$trending_pages_nav[ $trending_page_key ][ 'key' ] = $tr_page_options[ 'page' ];
    				$trending_pages_nav[ $trending_page_key ][ 'href' ] = get_permalink( $trending_page->ID );
    				$trending_pages_nav[ $trending_page_key ][ 'name' ] = esc_html( get_the_title( $trending_page ) );
    				$trending_pages_nav[ $trending_page_key ][ 'icon' ] = $tr_page_options[ 'icon' ];
    			}
    
    		}
    	}
    
    	return $trending_pages_nav;
    }
Viewing 3 replies - 1 through 3 (of 3 total)
  • I attempt to change/modify ligne as an example :

    if( ! isset( $boombox_trending_query[ $type ] ) ) {
    $post_type = 'post' && 'product';

    but don’t work…

    Moderator bcworkz

    (@bcworkz)

    The correct syntax for a post type query argument would be
    $post_type = array('post', 'product');
    I’m not confident this will enable the keeping of trending data for posts, only that posts will be included as a post type in the query. Related data may still be missing. It’s still required to include it as post type, but there may be more to this puzzle than that.

    If data is not kept for posts still, my advice is to seek direction from the theme’s devs. They’ll know more than anyone how to enable tracking of posts.

    Okay thanks for intervention. I will try with this.

    Thanks 🙂

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.