Plugin Directory

Changeset 1866666


Ignore:
Timestamp:
04/30/2018 10:05:42 PM (8 years ago)
Author:
bdeleasa
Message:

Adding a new setting allowing users to exclude terms from having their posts utilize random thumbnails

Location:
wp-random-post-thumbnails/trunk/classes
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • wp-random-post-thumbnails/trunk/classes/class-wprpt-options.php

    r1742817 r1866666  
    5151     */
    5252    public function __construct() {
    53        
     53
    5454        // Set our title
    5555        $this->title = __( 'Random Post Thumbnails', $this->prefix );
     
    5757        // Set our menu link title
    5858        $this->menu_title = __( 'Random Thumbnails', $this->prefix );
    59        
    60     }
    61 
    62    
     59
     60    }
     61
     62
    6363    /**
    6464     * Initiate our hooks
     
    7070     */
    7171    public function hooks() {
    72        
     72
    7373        add_action( 'admin_init', array( $this, 'init' ) );
    7474        add_action( 'cmb2_admin_init', array( $this, 'add_options_page_tabs' ) );
     
    310310                ) );
    311311
     312
     313                // Group field that houses our repeatable fields
     314                $taxonomy_options->add_field( array(
     315                    'name'           => 'Excluded Term',
     316                    'description'    => 'Select any categories in which the posts should be <strong>excluded</strong> from having random thumbnails.',
     317                    'id'             => "taxonomy_{$taxonomy_object->name}_excluded_terms",
     318                    'taxonomy'       => $taxonomy_slug, //Enter Taxonomy Slug
     319                    'type'           => 'taxonomy_multicheck',
     320                    // Optional :
     321                    'text'           => array(
     322                        'no_terms_text' => 'Sorry, no terms could be found.' // Change default text. Default: "No terms"
     323                    ),
     324                    'remove_default' => 'true' // Removes the default metabox provided by WP core. Pending release as of Aug-10-16
     325                ) );
     326
    312327                // Set the object type and add the metabox to our array of boxes
    313328                $taxonomy_options->object_type( 'options-page' );
     
    416431     *
    417432     * @since 1.0.0
    418      * 
     433     *
    419434     * @param  string  $field Field to retrieve
    420435     * @return mixed          Field value or exception is thrown
  • wp-random-post-thumbnails/trunk/classes/class-wprpt.php

    r1858153 r1866666  
    2828            add_filter( 'wprpt_all_images', array($this, 'add_images_based_on_post_type') );
    2929            add_filter( 'wprpt_all_images', array($this, 'add_images_based_on_taxonomy') );
     30            add_filter( 'wprpt_all_images', array($this, 'exclude_taxonomy_terms') );
    3031        }
    3132
     
    232233
    233234    /**
     235     * Filters the array of all possible random images and adds any that are
     236     * specific to the current taxonomy term.
     237     *
     238     * @since 2.0.0
     239     *
     240     * @param $all_images array
     241     * @return array
     242     */
     243    function exclude_taxonomy_terms( $all_images ) {
     244
     245        $post_id = get_the_ID();
     246        $taxonomies = get_the_taxonomies( $post_id );
     247
     248        if ( !empty($taxonomies) ) {
     249
     250            // Loop through each taxonomy for the current post
     251            foreach( $taxonomies as $slug => $taxonomy ) {
     252                $terms = wp_get_post_terms( $post_id, $slug );
     253                $excluded_terms = wprpt_get_option( "taxonomy_{$slug}_excluded_terms" );
     254
     255                // If the term is in the list of excluded terms, remove all of
     256                // the random images to 'disable' the random image functionality
     257                // for these terms.
     258                foreach( $terms as $term ) {
     259                    if ( !empty($excluded_terms) && in_array($term->slug, $excluded_terms ) ) {
     260                        $all_images = array();
     261                    }
     262                }
     263
     264            }
     265        }
     266
     267        return $all_images;
     268
     269    }
     270
     271
     272    /**
    234273     * Adds custom links to the plugin action links on the Plugins page (Edit,
    235274     * Delete, Deactivate, etc).
Note: See TracChangeset for help on using the changeset viewer.