plugin_name = ADVANCED_RELATED_POSTS_NAME;
$this->version = ADVANCED_RELATED_POSTS_VERSION;
$widget_options = array(
'classname' => 'advanced_related_posts',
'description' => __( 'Advanced Related Posts' , $this->plugin_name ),
);
parent::__construct( 'advanced_related_posts', 'Advanced Related Posts', $widget_options );
}
/**
* 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 . '-widget';
wp_enqueue_style( $id_prefix, plugin_dir_url( __FILE__ ) . 'css/advanced-related-posts-widget-style.css', array(), $this->version, 'all' );
}
/**
* 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 . '-widget';
wp_enqueue_script( $id_prefix, plugin_dir_url( __FILE__ ) . 'js/advanced-related-posts-widget-public.js', array( 'jquery' ), $this->version, false );
}
public function form( $instance ) {
$title = (isset($instance[ $this->name_prefix . 'title' ]) && sanitize_text_field( $instance[ $this->name_prefix . 'title' ] ) != '') ? esc_attr( sanitize_text_field( $instance[ $this->name_prefix . 'title' ] ) ) : __('Advanced Related Posts' , $this->plugin_name);
?>
name_prefix . 'title' ] = ( ! empty( $new_instance[ $this->name_prefix . 'title' ] ) ) ? strip_tags( $new_instance[ $this->name_prefix . 'title' ] ) : '';
return $instance;
}
public function widget( $args, $instance ) {
$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_widget_validated_data_from_array();
/*******************************************************************************************************/
$flag = $this->check_advanced_related_posts($id);
if ( $flag ) {
$arp_content = $this->show_advanced_related_posts( $id , $instance );
if ( $arp_content ) {
echo $arp_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 . 'widget_exclude_categories_ids' ] ) ) {
$exclude_categories_ids_arr = $this->options[ $this->name_prefix . 'widget_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 . 'widget_exclude_post_ids' ] != '' ) {
$exclude_post_ids_arr = explode(',', $this->options[ $this->name_prefix . 'widget_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 . 'widget_display_on_mobile' ] ) {
$check_device = wp_is_mobile();
if ( $check_device ) {
$flag = false;
}
}
// Enable for all post types
if ( ! $this->options[ $this->name_prefix . 'widget_all_post_type' ] ) {
// For selected post types
if ( ! empty( $this->options[ $this->name_prefix . 'widget_selected_post_type' ] ) ) {
$selected_post_type = $this->options[ $this->name_prefix . 'widget_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 . 'widget_display_front_page' ] ) {
$flag = false;
}else {
if ( ! is_singular() && ! ( (is_archive() || is_home()) ) ) {
$flag = false;
}
}
}
return $flag;
}
public function show_advanced_related_posts( $id , $instance ){
/*******************************************************************************************************/
$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 . 'widget_count' ];
// Only from same
$only_from_same = $this->options[ $this->name_prefix . 'widget_only_from_same' ];
// Strongness of matching
$strongness_of_matching = $this->options[ $this->name_prefix . 'widget_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_widget_get_posts( $id , $index, $got_post_ids );
if ( $posts_arr ) {
foreach ($posts_arr as $k => $v) {
if ( ! in_array( $v->ID, $got_post_ids ) ) {
$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_widget_get_posts( $id , 0, $got_post_ids );
}
if ( empty( $posts ) ) {
// Show when no posts are found
$widget_no_post_found = $this->options[ $this->name_prefix . 'widget_no_post_found' ];
// Dispaly custom text
switch ( $widget_no_post_found ) {
case 'display_custom_text':
$content[] = '