From 154587e4c3ae5afed86e36711eda14dc04bb7a59 Mon Sep 17 00:00:00 2001 From: Roman Pronskiy Date: Sat, 16 Apr 2016 15:59:34 +0300 Subject: [PATCH] Sync Github repo with WordPress SVN plugin directory --- README.md | 73 +++++++++++ css/style.css | 9 ++ js/search_exclude.js | 52 ++++++++ readme.txt | 77 ++++++++++- search-exclude.php | 213 ++++++++++++++++++++++++++----- views/bulk_edit.php | 14 ++ views/column_cell.php | 7 + metabox.php => views/metabox.php | 0 options.php => views/options.php | 0 views/quick_edit.php | 11 ++ 10 files changed, 417 insertions(+), 39 deletions(-) mode change 100644 => 100755 README.md create mode 100755 css/style.css create mode 100644 js/search_exclude.js mode change 100644 => 100755 readme.txt mode change 100644 => 100755 search-exclude.php create mode 100755 views/bulk_edit.php create mode 100755 views/column_cell.php rename metabox.php => views/metabox.php (100%) mode change 100644 => 100755 rename options.php => views/options.php (100%) mode change 100644 => 100755 create mode 100755 views/quick_edit.php diff --git a/README.md b/README.md old mode 100644 new mode 100755 index f16b547..81a8ec0 --- a/README.md +++ b/README.md @@ -2,12 +2,85 @@ WordPress Search Exclude ================= Search Exclude – WordPress plugin that lets you hide any page, post or whatever from the search results. Just check off the corresponding checkbox on post/page edit page. +Supports quick and bulk edit. You can also view the list of all the items that are excluded from search on the plugin settings page. +### Installation + +1. Upload `search-exclude` directory to the `/wp-content/plugins/` directory +2. Activate the plugin through the 'Plugins' menu in WordPress +3. Go to any post/page edit page and check off the checkbox `Exclude from Search Results` if you don't want the post/page to be shown in the search results + +### Frequently Asked Questions + +#### Does this plugin affect SEO? + +No, it does not affect crawling and indexing by search engines. +The only thing it does is hiding selected post/pages from your site search page. Not altering SEO indexing. + +#### Are there any hooks or actions available to customize plugin behaviour? + +Yes. +There is an action `searchexclude_hide_from_search`. +You can pass any post/page/custom_post ids as an array in the first parameter. +The second parameter specifies state of visibility in search. Pass true if you want to hide posts/pages, +or false - if you want show them in the search results. + +Example: +Let's say you want "Exclude from Search Results" checkbox to be checked off by default +for newly created posts, but not pages. In this case you can add following code +to your theme's function.php: + +```php +add_filter('default_content', 'excludeNewPostByDefault', 10, 2); +function excludeNewPostByDefault($content, $post) +{ + if ('post' === $post->post_type) { + do_action('searchexclude_hide_from_search', array($post->ID), true); + } +} +``` + +Also there is a filter `searchexclude_filter_search`. +With this filter you can turn on/off search filtering dynamically. +Parameters: +$exclude - current search filtering state (specifies whether to filter search or not) +$query - current WP_Query object + +By returning true or false you can turn search filtering respectively. + +Example: +Let's say you need to disable search filtering if searching by specific post_type. +In this case you could add following code to you functions.php: +```php +add_filter('searchexclude_filter_search', 'filterForProducts', 10, 2); +function filterForProducts($exclude, $query) +{ + return $exclude && 'product' !== $query->get('post_type'); +} +``` ### Changelog +#### 1.2.2 +* Added action searchexclude_hide_from_search +* Added filter searchexclude_filter_search +* Fixed Bulk actions for Firefox + +#### 1.2.1 +* Fixed bug when unable to save post on PHP <5.5 because of boolval() usage + +#### 1.2.0 +* Added quick and bulk edit support +* Tested up to WP 4.1 + +#### 1.1.0 +* Tested up to WP 4.0 +* Do not show Plugin on some service pages in Admin +* Fixed conflict with bbPress +* Fixed deprecation warning when DEBUG is on + #### 1.0.6 * Fixed search filtering for AJAX requests diff --git a/css/style.css b/css/style.css new file mode 100755 index 0000000..721010f --- /dev/null +++ b/css/style.css @@ -0,0 +1,9 @@ +.fixed .column-search_exclude { + width: 12%; +} +.fixed .column-search_exclude div { + text-align: center; +} +.inline-edit-row fieldset label span.title.search-exclude-label { + width: 11em; +} \ No newline at end of file diff --git a/js/search_exclude.js b/js/search_exclude.js new file mode 100644 index 0000000..c03e1ab --- /dev/null +++ b/js/search_exclude.js @@ -0,0 +1,52 @@ +(function($) { + + // we create a copy of the WP inline edit post function + var $wp_inline_edit = inlineEditPost.edit; + + // and then we overwrite the function with our own code + inlineEditPost.edit = function( id ) { + + // "call" the original WP edit function + // we don't want to leave WordPress hanging + $wp_inline_edit.apply( this, arguments ); + + var $post_id = 0; + if ( typeof( id ) == 'object' ) { + $post_id = parseInt(this.getId(id)); + } + + if ( $post_id > 0 ) { + var $edit_row = $( '#edit-' + $post_id ); + var $exclude = $( '#search-exclude-' + $post_id).data("search_exclude"); + $edit_row.find( 'input[name="sep[exclude]"]' ).prop('checked', $exclude); + } + }; + + $('#bulk_edit').live( 'click', function() { + // define the bulk edit row + var $bulk_row = $( '#bulk-edit' ); + + // get the selected post ids that are being edited + var $post_ids = new Array(); + $bulk_row.find('#bulk-titles').children().each( function() { + $post_ids.push( $( this ).attr( 'id' ).replace( /^(ttle)/i, '' ) ); + }); + + // get the search exclude value + var $exclude = $bulk_row.find('select[name="sep[exclude]"]').val(); + + // save the data + $.ajax({ + url: ajaxurl, + type: 'POST', + cache: false, + async: false, // Fixes bulk editing in FF, see https://wordpress.org/support/topic/bulk-search-exclude-doesnt-work + data: { + action: 'search_exclude_save_bulk_edit', + post_ids: $post_ids, + sep_exclude: $exclude + } + }); + }); + +})(jQuery); diff --git a/readme.txt b/readme.txt old mode 100644 new mode 100755 index 7a863a0..bb3cd62 --- a/readme.txt +++ b/readme.txt @@ -2,8 +2,8 @@ Contributors: pronskiy, williamdodson, stevelock Tags: admin, plugin, search Requires at least: 3.3 -Tested up to: 3.9 -Stable tag: 1.0.6 +Tested up to: 4.5 +Stable tag: 1.2.2 License: GPLv2 or later License URI: http://www.gnu.org/licenses/gpl-2.0.html @@ -11,9 +11,10 @@ Hide any post or page from the search results. == Description == -With this plugin you can exclude any page, post or whatever from the search results by checking off the corresponding checkbox on post/page edit page. +With this plugin you can exclude any page, post or whatever from the WordPress search results by checking off the corresponding checkbox on post/page edit page. +Supports quick and bulk edit. -You can also view the list of all the items that are excluded from search on the plugin settings page. +On the plugin settings page you can also see the list of all the items that are hidden from search. == Installation == @@ -21,6 +22,55 @@ You can also view the list of all the items that are excluded from search on the 2. Activate the plugin through the 'Plugins' menu in WordPress 3. Go to any post/page edit page and check off the checkbox `Exclude from Search Results` if you don't want the post/page to be shown in the search results +== Frequently Asked Questions == + += Does this plugin affect SEO? = + +No, it does not affect crawling and indexing by search engines. +The only thing it does is hiding selected post/pages from your site search page. Not altering SEO indexing. + += Are there any hooks or actions available to customize plugin behaviour? = + +Yes. +There is an action `searchexclude_hide_from_search`. +You can pass any post/page/custom_post ids as an array in the first parameter. +The second parameter specifies state of visibility in search. Pass true if you want to hide posts/pages, +or false - if you want show them in the search results. + +Example: +Let's say you want "Exclude from Search Results" checkbox to be checked off by default +for newly created posts, but not pages. In this case you can add following code +to your theme's function.php: + +` +add_filter('default_content', 'excludeNewPostByDefault', 10, 2); +function excludeNewPostByDefault($content, $post) +{ + if ('post' === $post->post_type) { + do_action('searchexclude_hide_from_search', array($post->ID), true); + } +} +` + +Also there is a filter `searchexclude_filter_search`. +With this filter you can turn on/off search filtering dynamically. +Parameters: +$exclude - current search filtering state (specifies whether to filter search or not) +$query - current WP_Query object + +By returning true or false you can turn search filtering respectively. + +Example: +Let's say you need to disable search filtering if searching by specific post_type. +In this case you could add following code to you functions.php: +` +add_filter('searchexclude_filter_search', 'filterForProducts', 10, 2); +function filterForProducts($exclude, $query) +{ + return $exclude && 'product' !== $query->get('post_type'); +} +` + == Screenshots == 1. screenshot-1.png @@ -28,6 +78,25 @@ You can also view the list of all the items that are excluded from search on the == Changelog == += 1.2.2 = +* Added action searchexclude_hide_from_search +* Added filter searchexclude_filter_search +* Fixed Bulk actions for Firefox + += 1.2.1 = +* Fixed bug when unable to save post on PHP <5.5 because of boolval() usage + + += 1.2.0 = +* Added quick and bulk edit support +* Tested up to WP 4.1 + += 1.1.0 = +* Tested up to WP 4.0 +* Do not show Plugin on some service pages in Admin +* Fixed conflict with bbPress +* Fixed deprecation warning when DEBUG is on + = 1.0.6 = * Fixed search filtering for AJAX requests diff --git a/search-exclude.php b/search-exclude.php old mode 100644 new mode 100755 index 3c6893c..64e99fc --- a/search-exclude.php +++ b/search-exclude.php @@ -1,8 +1,8 @@ registerHooks(); @@ -40,27 +42,66 @@ public function registerHooks() add_action('post_updated', array($this, 'postSave')); add_action('edit_attachment', array($this, 'postSave')); add_action('add_meta_boxes', array($this, 'addMetabox') ); - add_filter('pre_get_posts',array($this, 'searchFilter')); + add_filter('pre_get_posts', array($this, 'searchFilter')); + + add_filter('bbp_has_replies_query', array($this, 'flagBbPress')); + + add_filter('manage_posts_columns', array($this, 'addColumn')); + add_filter('manage_pages_columns', array($this, 'addColumn')); + add_action('manage_posts_custom_column', array($this, 'populateColumnValue'), 10, 2); + add_action('manage_pages_custom_column', array($this, 'populateColumnValue'), 10, 2); + add_action('bulk_edit_custom_box', array($this, 'addBulkEditCustomBox')); + add_action('quick_edit_custom_box', array($this, 'addQuickEditCustomBox')); + add_action('admin_print_scripts-edit.php', array($this, 'enqueueEditScripts')); + add_action('wp_ajax_search_exclude_save_bulk_edit', array($this, 'saveBulkEdit')); + add_action('admin_enqueue_scripts', array($this, 'addStyle')); + + /** + * Hook can be used outside of the plugin. + * + * You can pass any post/page/custom_post ids in the array with first parameter. + * The second parameter specifies states of visibility in search to be set. + * Pass true if you want to hide posts/pages, or false - if you want show them in the search results. + * + * Example: + * Let's say you want "Exclude from Search Results" checkbox to be checked off by default + * for newly created posts, but not pages. In this case you can add following code + * to your theme's function.php: + * + * + * add_filter('default_content', 'excludeNewPostByDefault', 10, 2); + * function excludeNewPostByDefault($content, $post) + * { + * if ('post' === $post->post_type) { + * do_action('searchexclude_hide_from_search', array($post->ID), true); + * } + * } + * + * + * @param array $postIds array of post IDs + * @param bool $hide + */ + add_action('searchexclude_hide_from_search', array($this, 'savePostIdsToSearchExclude'), 10, 2); } /** * @param $postId int the ID of the post - * @param $value bool indicates whether post should be excluded from the search results or not + * @param $exclude bool indicates whether post should be excluded from the search results or not */ - protected function savePostIdToSearchExclude($postId, $value) + protected function savePostIdToSearchExclude($postId, $exclude) { + $this->savePostIdsToSearchExclude(array(intval($postId)), $exclude); + } + + public function savePostIdsToSearchExclude($postIds, $exclude) + { + $exclude = (bool) $exclude; $excluded = $this->getExcluded(); - $indSep = array_search($postId, $excluded); - if ($value) { - if (false === $indSep) { - $excluded[] = $postId; - } - } - else { - if (false !== $indSep) { - unset($excluded[$indSep]); - } + if ($exclude) { + $excluded = array_unique(array_merge($excluded, $postIds)); + } else { + $excluded = array_diff($excluded, $postIds); } $this->saveExcluded($excluded); } @@ -71,19 +112,86 @@ protected function savePostIdToSearchExclude($postId, $value) protected function saveExcluded($excluded) { update_option('sep_exclude', $excluded); + $this->excluded = $excluded; } protected function getExcluded() { - $excluded = get_option('sep_exclude'); + if (null === $this->excluded) { + $this->excluded = get_option('sep_exclude'); + if (!is_array($this->excluded)) { + $this->excluded = array(); + } + } + + return $this->excluded; + } + + protected function isExcluded($postId) + { + return false !== array_search($postId, $this->getExcluded()); + } + + protected function view($view, $params = array()) + { + extract($params); + include(dirname(__FILE__) . '/views/' . $view . '.php'); + } - if (!is_array($excluded)) { - $excluded = array(); + public function saveBulkEdit() + { + $postIds = !empty($_POST['post_ids']) ? $_POST[ 'post_ids' ] : false; + $exclude = isset($_POST['sep_exclude']) && '' !== $_POST['sep_exclude'] ? $_POST[ 'sep_exclude' ] : null; + if (is_array($postIds) && null !== $exclude) { + $this->savePostIdsToSearchExclude($postIds, $exclude); } - return $excluded; } - function activate() + public function enqueueEditScripts() + { + wp_enqueue_script( + 'search-exclude-admin-edit', + plugin_dir_url( __FILE__ ) . 'js/search_exclude.js', + array( 'jquery', 'inline-edit-post' ), + '', + true + ); + } + + public function addStyle() + { + wp_register_style('search-exclude-stylesheet', plugins_url('/css/style.css', __FILE__ )); + wp_enqueue_style('search-exclude-stylesheet'); + } + + public function addQuickEditCustomBox($columnName) + { + if ('search_exclude' == $columnName) { + $this->view('quick_edit'); + } + } + + public function addBulkEditCustomBox($columnName) + { + if ('search_exclude' == $columnName) { + $this->view('bulk_edit'); + } + } + + public function addColumn($columns) + { + $columns['search_exclude'] = 'Search Exclude'; + return $columns; + } + + public function populateColumnValue($columnName, $postId) + { + if ('search_exclude' == $columnName) { + $this->view('column_cell', array('exclude' => $this->isExcluded($postId), 'postId' => $postId)); + } + } + + public function activate() { $excluded = $this->getExcluded(); @@ -94,16 +202,18 @@ function activate() public function addMetabox() { - add_meta_box( 'sep_metabox_id', 'Search Exclude', array($this, 'metabox'), null); + $currentScreen = get_current_screen(); + /* Do not show meta box on service pages */ + if (empty($currentScreen->post_type)) { + return; + } + add_meta_box('sep_metabox_id', 'Search Exclude', array($this, 'metabox'), null, 'side'); } - public function metabox( $post ) + public function metabox($post) { - $excluded = $this->getExcluded(); - $exclude = !(false === array_search($post->ID, $excluded)); - wp_nonce_field( 'sep_metabox_nonce', 'metabox_nonce' ); - include(dirname(__FILE__) . '/metabox.php'); + $this->view('metabox', array('exclude' => $this->isExcluded($post->ID))); } public function adminMenu() @@ -111,7 +221,7 @@ public function adminMenu() add_options_page( 'Search Exclude', 'Search Exclude', - 10, + 'manage_options', 'search_exclude', array($this, 'options') ); @@ -119,28 +229,55 @@ public function adminMenu() public function searchFilter($query) { - if ((!is_admin() || (defined('DOING_AJAX') && DOING_AJAX)) && $query->is_search) { - $query->set('post__not_in', array_merge($query->get('post__not_in'), $this->getExcluded())); + $exclude = + (!is_admin() || (defined('DOING_AJAX') && DOING_AJAX)) + && $query->is_search + && !$this->isBbPress($query); + + $exclude = apply_filters('searchexclude_filter_search', $exclude, $query); + + if ($exclude) { + $query->set('post__not_in', array_merge(array(), $this->getExcluded())); } + return $query; } - public function postSave( $post_id ) + public function isBbPress($query) + { + return $query->get('___s2_is_bbp_has_replies'); + } + + /** + * Flags a WP Query has being a `bbp_has_replies()` query. + * @attaches-to ``add_filter('bbp_has_replies_query');`` + * + * @param array $args Query arguments passed by the filter. + * + * @return array The array of ``$args``. + * + * @see Workaround for bbPress and the `s` key. See: + */ + public function flagBbPress($args) { - if (!isset($_POST['sep'])) return $post_id; + return array_merge($args, array('___s2_is_bbp_has_replies' => true)); + } + + public function postSave($postId) + { + if (!isset($_POST['sep'])) return $postId; $sep = $_POST['sep']; $exclude = (isset($sep['exclude'])) ? $sep['exclude'] : 0 ; - $this->savePostIdToSearchExclude($post_id, $exclude); + $this->savePostIdToSearchExclude($postId, $exclude); - return $post_id; + return $postId; } public function options() { $excluded = $this->getExcluded(); - $query = new WP_Query( array( 'post_type' => 'any', 'post_status' => 'any', @@ -148,7 +285,13 @@ public function options() 'order'=>'ASC', 'nopaging' => true, )); - include(dirname(__FILE__) . '/options.php'); + $this->view( + 'options', + array( + 'excluded' => $excluded, + 'query' => $query, + ) + ); } public function saveOptions() diff --git a/views/bulk_edit.php b/views/bulk_edit.php new file mode 100755 index 0000000..4655871 --- /dev/null +++ b/views/bulk_edit.php @@ -0,0 +1,14 @@ +
+
+
+ +
+
+
\ No newline at end of file diff --git a/views/column_cell.php b/views/column_cell.php new file mode 100755 index 0000000..09080bf --- /dev/null +++ b/views/column_cell.php @@ -0,0 +1,7 @@ +
+ title="Hidden from search results">Hidden + + title="Visible in search results">Visible + +
\ No newline at end of file diff --git a/metabox.php b/views/metabox.php old mode 100644 new mode 100755 similarity index 100% rename from metabox.php rename to views/metabox.php diff --git a/options.php b/views/options.php old mode 100644 new mode 100755 similarity index 100% rename from options.php rename to views/options.php diff --git a/views/quick_edit.php b/views/quick_edit.php new file mode 100755 index 0000000..33492d3 --- /dev/null +++ b/views/quick_edit.php @@ -0,0 +1,11 @@ +
+
+
+ +
+
+
\ No newline at end of file