*/ class Advanced_Related_Posts_Public { /** * The ID of this plugin. * * @since 1.0.0 * @access private * @var string $plugin_name The ID of this plugin. */ private $plugin_name; /** * The version of this plugin. * * @since 1.0.0 * @access private * @var string $version The current version of this plugin. */ private $version; private $html_class_prefix = 'ays-arp-'; private $html_name_prefix = 'ays-arp-'; private $name_prefix = 'arp_'; private $unique_id; private $unique_id_in_class; private $current_post_obj; private $options; private $meta_values; private $layout_obj; /** * Initialize the class and set its properties. * * @since 1.0.0 * @param string $plugin_name The name of the plugin. * @param string $version The version of this plugin. */ public function __construct( $plugin_name, $version ) { $this->plugin_name = $plugin_name; $this->version = $version; } /** * Register the stylesheets for the public-facing side of the site. * * @since 1.0.0 */ public function enqueue_styles() { /** * This function is provided for demonstration purposes only. * * An instance of this class should be passed to the run() function * defined in Advanced_Related_Posts_Loader as all of the hooks are defined * in that particular class. * * The Advanced_Related_Posts_Loader will then create the relationship * between the defined hooks and the functions defined in this * class. */ $id_prefix = $this->plugin_name . '-under-posts'; } /** * Register the JavaScript for the public-facing side of the site. * * @since 1.0.0 */ public function enqueue_scripts() { /** * This function is provided for demonstration purposes only. * * An instance of this class should be passed to the run() function * defined in Advanced_Related_Posts_Loader as all of the hooks are defined * in that particular class. * * The Advanced_Related_Posts_Loader will then create the relationship * between the defined hooks and the functions defined in this * class. */ $id_prefix = $this->plugin_name . '-under-posts'; wp_enqueue_script( $id_prefix , plugin_dir_url( __FILE__ ) . 'js/advanced-related-posts-public.js', array( 'jquery' ), $this->version, false ); wp_enqueue_script( $id_prefix . '-ajax', plugin_dir_url( __FILE__ ) . 'js/advanced-related-posts-public-ajax.js', array( 'jquery' ), $this->version, false ); wp_localize_script($id_prefix , 'aysAdvancedRelatedPostsAjaxPublic', array( 'ajaxUrl' => admin_url('admin-ajax.php'), ) ); wp_localize_script( $id_prefix, 'aysAdvancedRelatedPostsLangObj', array( 'notAnsweredText' => __( 'You have not answered this question', $this->plugin_name ), ) ); } public function enqueue_styles_early(){ wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/advanced-related-posts-public.css', array(), $this->version, 'all' ); } public function ays_generate_advanced_related_posts_method( $content ){ $id = get_the_ID(); // Post ID $flag = true; if ( $id ) { /*******************************************************************************************************/ $this->enqueue_styles(); $this->enqueue_scripts(); /*******************************************************************************************************/ $this->current_post_obj = get_post( $id, "OBJECT" ); $this->options = Advanced_Related_Posts_Data::get_arp_validated_data_from_array(); $this->meta_values = get_post_meta( $id, $this->name_prefix . 'post_meta', false ); /*******************************************************************************************************/ $flag = $this->check_advanced_related_posts($id); if ( $flag ) { $arp_content = $this->show_advanced_related_posts($id); if ( $arp_content ) { $content .= $arp_content; } } } return $content; } public function check_advanced_related_posts( $id ){ $cat_id = get_the_category( $id ); $flag = true; $exclude_categories_ids = array(); $exclude_posts_ids = array(); // Exclude for categories if ( ! empty( $this->options[ $this->name_prefix . 'under_posts_exclude_categories_ids' ] ) ) { $exclude_categories_ids_arr = $this->options[ $this->name_prefix . 'under_posts_exclude_categories_ids' ]; foreach ($exclude_categories_ids_arr as $key => $categories_id) { if ($categories_id == '') { continue; } $categories_id_val = absint( $categories_id ); if ($categories_id_val == 0) { continue; } if ( ! in_array($categories_id_val, $exclude_categories_ids) ) { $exclude_categories_ids[] = $categories_id_val; } } } // Exclude posts ids if ( $this->options[ $this->name_prefix . 'under_posts_exclude_post_ids' ] != '' ) { $exclude_post_ids_arr = explode(',', $this->options[ $this->name_prefix . 'under_posts_exclude_post_ids' ] ); foreach ($exclude_post_ids_arr as $key => $post_id) { if ($post_id == '') { continue; } $post_id_val = absint( $post_id ); if ($post_id_val == 0) { continue; } if ( ! in_array($post_id_val, $exclude_posts_ids) ) { $exclude_posts_ids[] = $post_id_val; } } } // Check currrent post category id if (! empty($cat_id) || ! is_null($cat_id) ) { foreach ($cat_id as $key => $c_id) { if( in_array($c_id->term_id, $exclude_categories_ids) ){ $flag = false; break; } } } // Check currrent post id if( in_array($id, $exclude_posts_ids) ){ $flag = false; } // Disable on mobile devices if ( $this->options[ $this->name_prefix . 'under_posts_display_on_mobile' ] ) { $check_device = wp_is_mobile(); if ( $check_device ) { $flag = false; } } // Enable for all post types if ( ! $this->options[ $this->name_prefix . 'under_posts_all_post_type' ] ) { // For selected post types if ( ! empty( $this->options[ $this->name_prefix . 'under_posts_selected_post_type' ] ) ) { $selected_post_type = $this->options[ $this->name_prefix . 'under_posts_selected_post_type' ]; $current_post_type = (isset( $this->current_post_obj->post_type ) && $this->current_post_obj->post_type != '') ? $this->current_post_obj->post_type : null; if ( ! is_null( $current_post_type ) ) { // Check currrent post type if( ! in_array( $current_post_type , $selected_post_type) ){ $flag = false; } } } } // Display on the front page if ( is_front_page() ) { if ( ! $this->options[ $this->name_prefix . 'under_posts_display_front_page' ] ) { $flag = false; }else { if ( ! is_singular() && ! ( (is_archive() || is_home()) ) ) { $flag = false; } } } if( is_archive() || is_category() || is_home() || is_search() ) { $flag = false; } return $flag; } public function show_advanced_related_posts( $id ){ /*******************************************************************************************************/ $unique_id = uniqid(); $this->unique_id = $unique_id; $this->unique_id_in_class = $id . "-" . $unique_id;; /*******************************************************************************************************/ $content = array(); $posts = array(); // Uunder posts count $posts_count = $this->options[ $this->name_prefix . 'under_posts_count' ]; // Only from same $only_from_same = $this->options[ $this->name_prefix . 'under_posts_only_from_same' ]; // Strongness of matching $strongness_of_matching = $this->options[ $this->name_prefix . 'under_posts_strongness_of_matching' ]; $user_id = get_current_user_id(); /*******************************************************************************************************/ $got_post_ids = array(); if ( ! empty( $only_from_same ) && $strongness_of_matching != 'and' ) { $only_from_same = array( "same_post_type", "categories", "post_tag", "post_format", ); foreach ($only_from_same as $index => $value) { // Get posts $posts_arr = $this->ays_under_posts_get_posts( $id , $index, $got_post_ids ); if ( $posts_arr ) { foreach ($posts_arr as $k => $v) { if ( $k == 'ID' ) { $got_post_ids[] = $v->ID; } } $posts = array_merge($posts, $posts_arr); $count_of_posts_arr = count( $posts ); if ( $count_of_posts_arr > $posts_count ) { $posts = array_slice( $posts , 0, $posts_count ); break; } elseif ( $count_of_posts_arr == $posts_count ) { break; } } } }else { // Get posts $posts = $this->ays_under_posts_get_posts( $id , 0, $got_post_ids ); } if ( empty( $posts ) ) { // Show when no posts are found $under_posts_no_post_found = $this->options[ $this->name_prefix . 'under_posts_no_post_found' ]; // Dispaly custom text switch ( $under_posts_no_post_found ) { case 'display_custom_text': $content[] = '