Skip to content

Commit

Permalink
Ability to exclude posts from feed
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikhail Kobzarev committed Dec 27, 2019
1 parent c097492 commit 7391c9e
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 4 deletions.
57 changes: 57 additions & 0 deletions includes/class-main.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,57 @@ private function hooks() {
add_action( 'template_redirect', array( $this, 'send_headers_for_aio_seo_pack' ), 20 );
add_action( 'pre_get_posts', array( $this, 'alter_query' ) );
add_filter( 'plugin_action_links', [ $this, 'add_settings_link' ], 10, 2 );
add_action( 'add_meta_boxes', array( $this, 'add_meta_box' ) );
add_action( 'save_post', array( $this, 'save_meta_box' ) );

register_activation_hook( MIHDAN_MAILRU_PULSE_FEED_FILE, array( $this, 'on_activate' ) );
register_deactivation_hook( MIHDAN_MAILRU_PULSE_FEED_FILE, array( $this, 'on_deactivate' ) );
}

/**
* Add settings metabox for posts.
*/
public function add_meta_box() {
add_meta_box(
$this->slug,
__( 'Pulse Main.ru', 'mihdan-mailru-pulse-feed' ),
[ $this, 'render_meta_box' ],
$this->post_type,
'side',
'high'
);
}

/**
* Render settings metabox for posts.
*/
public function render_meta_box() {
$exclude = (bool) get_post_meta( get_the_ID(), $this->slug . '_exclude', true );
?>
<label for="<?php echo esc_attr( $this->slug ); ?>_exclude" title="Включить/Исключить запись из ленты">
<input type="checkbox" value="1" name="<?php echo esc_attr( $this->slug ); ?>_exclude" id="<?php echo esc_attr( $this->slug ); ?>_exclude" <?php checked( $exclude, true ); ?>> <?php _e( 'Exclude From Feed', 'mihdan-mailru-pulse-feed' ); ?>
</label>
<?php
}
/**
* Созраняем данные метабокса.
*
* @param int $post_id идентификатор записи.
*/
public function save_meta_box( $post_id ) {
if ( wp_is_post_autosave( $post_id ) || wp_is_post_revision( $post_id ) ) {
return;
}
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
if ( isset( $_POST[ $this->slug . '_exclude' ] ) ) {
update_post_meta( $post_id, $this->slug . '_exclude', 1 );
} else {
delete_post_meta( $post_id, $this->slug . '_exclude' );
}
}

/**
* Add plugin action links
*
Expand Down Expand Up @@ -130,6 +176,17 @@ public function alter_query( \WP_Query $wp_query ) {

// Указываем направление сортировки.
$wp_query->set( 'order', $this->wposa_obj->get_option( 'order', 'feed', 'DESC' ) );

// Получаем текущие мета запросы.
$meta_query = $wp_query->get( 'meta_query', array() );

// Добавляем исключения.
$meta_query[] = array(
'key' => $this->slug . '_exclude',
'compare' => 'NOT EXISTS',
);
// Исключаем записи с галочкой в админке
$wp_query->set( 'meta_query', $meta_query );
}
}

Expand Down
6 changes: 3 additions & 3 deletions mihdan-mailru-pulse-feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
* Author URI: https://www.kobzarev.com/
* Requires at least: 4.6
* Tested up to: 5.3
* Version: 0.1.3
* Stable tag: 0.1.3
* Version: 0.1.4
* Stable tag: 0.1.4
*
* Text Domain: mihdan-mailru-pulse-feed
* Domain Path: /languages/
Expand All @@ -26,7 +26,7 @@
exit; // Exit if accessed directly.
}

define( 'MIHDAN_MAILRU_PULSE_FEED_VERSION', '0.1.3' );
define( 'MIHDAN_MAILRU_PULSE_FEED_VERSION', '0.1.4' );
define( 'MIHDAN_MAILRU_PULSE_FEED_PATH', __DIR__ );
define( 'MIHDAN_MAILRU_PULSE_FEED_URL', untrailingslashit( plugin_dir_url( __FILE__ ) ) );
define( 'MIHDAN_MAILRU_PULSE_FEED_FILE', __FILE__ );
Expand Down
6 changes: 5 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Contributors: mihdan
Tags: mailru, pulse, feed, seo, seo-friendly
Requires at least: 4.6
Tested up to: 5.3
Stable tag: 0.1.3
Stable tag: 0.1.4
Requires PHP: 5.6.20

WordPress плагин, формирующий ленту для новой рекомендательной системы Пульс от компании Mail.ru.
Expand Down Expand Up @@ -58,6 +58,10 @@ add_filter(

== Changelog ==

= 0.1.4 (27.12.2019) =
* Ability to exclude posts from feed
* Ability to exclude categories and tags from feed

= 0.1.3 (27.12.2019) =
* Added settings for feed source
* Added new hook `mihdan_mailru_pulse_feed_head`
Expand Down

0 comments on commit 7391c9e

Please sign in to comment.