Making WordPress.org

Changeset 1654


Ignore:
Timestamp:
06/10/2015 09:48:58 PM (11 years ago)
Author:
coffee2code
Message:

jobs.wordpress.net: Enable searches in the admin for the job post type to also search certain custom fields.

Searches first_name, last_name, email, and company. Facilitates finding a job when contacted by a job poster who fails to specify the job they posted, or for finding other jobs posted by the same person/company.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/jobs.wordpress.net/public_html/wp-content/plugins/jobswp/jobswp.php

    r1602 r1654  
    22/*
    33Plugin Name: JobsWP
    4 Version: 1.1
     4Version: 1.2
    55Plugin URI: http://jobs.wordpress.net
    66Author: Scott Reilly
     
    110110                add_action( 'admin_action_close-job',         array( $this, 'handle_close_job' ) );
    111111                add_action( 'post_row_actions',               array( $this, 'post_row_actions' ), 10, 2 );
     112
     113                // For enabling admin job post type searches to also search custom fields.
     114                add_filter( 'posts_join',                     array( $this, 'admin_search_job_posts_join' ), 10, 2 );
     115                add_filter( 'posts_search',                   array( $this, 'admin_search_job_posts_search' ), 10, 2 );
     116                add_filter( 'posts_request',                  array( $this, 'admin_search_job_posts_request' ), 10, 2 );
    112117
    113118                foreach ( array( 'the_content', 'the_title', 'single_post_title' ) as $filter )
     
    613618
    614619        /**
     620         * Indicates if the query is an admin search against the job post type.
     621         *
     622         * @param object $query The query object.
     623         * @return bool  True if the query is an admin search against the job post type, false otherwise.
     624         */
     625        protected function is_admin_job_search( $query ) {
     626                return is_admin() && 'edit.php' === $GLOBALS['pagenow'] && $query->is_search() && 'job' === $query->query['post_type'] && '' !== $query->query_vars['s'];
     627        }
     628
     629        /**
     630         * Returns the SQL clauses to append to a query in order to query for
     631         * job-specific meta query.
     632         *
     633         * @param WP_Query $query The WP_Query instance.
     634         * @return array   Associative array of `JOIN` and `WHERE` SQL.
     635         */
     636        protected function get_admin_search_job_meta_sql( $query ) {
     637                global $wpdb;
     638                $fields_to_search = array( 'first_name', 'last_name', 'email', 'company' );
     639
     640                $meta_query = array( 'relation' => 'OR' );
     641
     642                foreach( $fields_to_search as $field ) {
     643                        array_push( $meta_query, array(
     644                                'compare' => 'LIKE',
     645                                'key'     => $field,
     646                                'value'   => $query->query_vars['s'],
     647                        ) );
     648                }
     649
     650                return get_meta_sql( $meta_query, 'post', $wpdb->posts, 'ID', $query );
     651        }
     652
     653
     654        /**
     655         * Modifies the JOIN clause of the query for searches in the admin for the job
     656         * post type to include the postmeta table.
     657         *
     658         * @param string   $join  JOIN clause of the query.
     659         * @param WP_Query $query The WP_Query instance.
     660         * @return string
     661         */
     662        public function admin_search_job_posts_join( $join, $query ) {
     663                // Don't change anything if not an admin job search.
     664                if ( ! $this->is_admin_job_search( $query ) ) {
     665                        return $join;
     666                }
     667
     668                // Get the necessary JOIN clause needed for a meta search.
     669                $meta_sql = $this->get_admin_search_job_meta_sql( $query );
     670
     671                return $join . $meta_sql['join'];
     672        }
     673
     674        /**
     675         * Modifies the search SQL for searches in the admin for the job post type to
     676         * also search certain custom fields.
     677         *
     678         * @param string   $search Search SQL for WHERE clause
     679         * @param WP_Query $query  The WP_Query instance.
     680         * @return string
     681         */
     682        public function admin_search_job_posts_search( $search, $query ) {
     683                // Don't change anything if not an admin job search.
     684                if ( ! $this->is_admin_job_search( $query ) ) {
     685                        return $search;
     686                }
     687
     688                // Get the necessary WHERE clause for the meta fields to be searched.
     689                $meta_sql = $this->get_admin_search_job_meta_sql( $query );
     690                $where = $meta_sql['where'];
     691
     692                // Change the default AND to an OR.
     693                if ( 0 === strpos( $where, ' AND ' ) ) {
     694                        $where = ' OR ' . substr( $where, 5 );
     695                }
     696
     697                // Insert the meta query into the search SQL.
     698                $search = substr( $search, 0, -3 ) . $where . '))';
     699
     700                return $search;
     701        }
     702
     703        /**
     704         * Modifies the completed SQL for searches in the admin for the job post type
     705         * to ensure only distinct results are returned.
     706         *
     707         * @param string   $request The complete SQL query.
     708         * @param WP_Query $query   The WP_Query instance.
     709         * @return string
     710         */
     711        public function admin_search_job_posts_request( $request, $query ) {
     712                // Don't change anything if not an admin job search.
     713                if ( ! $this->is_admin_job_search( $query ) ) {
     714                        return $request;
     715                }
     716
     717                // Make the query distinct, unless it already is.
     718                if ( 0 !== strpos( $request, 'SELECT DISTINCT' ) ) {
     719                        $request = 'SELECT DISTINCT' . substr( $request, 6 );
     720                }
     721
     722                return $request;
     723        }
     724
     725        /**
    615726         * Schedules job pruning if it isn't already scheduled.
    616727         *
Note: See TracChangeset for help on using the changeset viewer.