WordPress.org

Plugin Directory

Changeset 1737208


Ignore:
Timestamp:
09/27/17 16:05:31 (3 months ago)
Author:
greenshady
Message:

Uploading version 1.0.0 of the Custom Classes plugin.

Location:
custom-classes
Files:
32 added
1 deleted
2 edited

Legend:

Unmodified
Added
Removed
  • custom-classes/trunk/custom-classes.php

    r501186 r1737208  
    22/** 
    33 * Plugin Name: Custom Classes 
    4  * Plugin URI: http://justintadlock.com/archives/2012/02/06/custom-classes-wordpress-plugin 
    5  * Description: Allows users to input custom post and <code>&lt;body></code> classes on a per-post basis. 
    6  * Version: 0.1 
    7  * Author: Justin Tadlock 
    8  * Author URI: http://justintadlock.com 
     4 * Plugin URI:  https://themehybrid.com/plugins/custom-classes 
     5 * Description: Allows users to add custom post and body classes from the admin. 
     6 * Version:     1.0.0 
     7 * Author:      Justin Tadlock 
     8 * Author URI:  http://justintadlock.com 
    99 * 
    10  * This program is free software; you can redistribute it and/or modify it under the terms of the GNU  
    11  * General Public License version 2, as published by the Free Software Foundation.  You may NOT assume  
     10 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU 
     11 * General Public License version 2, as published by the Free Software Foundation.  You may NOT assume 
    1212 * that you can use any other version of the GPL. 
    1313 * 
    14  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without  
     14 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
    1515 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
    1616 * 
    17  * @package CustomClasses 
    18  * @version 0.1.0 
    19  * @author Justin Tadlock <justin@justintadlock.com> 
    20  * @copyright Copyright (c) 2012, Justin Tadlock 
    21  * @link http://justintadlock.com/archives/2012/02/06/custom-classes-wordpress-plugin 
    22  * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html 
     17 * @package   CustomClasses 
     18 * @version   1.0.0 
     19 * @author    Justin Tadlock <justintadlock@gmail.com> 
     20 * @copyright Copyright (c) 2012-2017, Justin Tadlock 
     21 * @link      https://themehybrid.com/plugins/custom-classes 
     22 * @license   http://www.gnu.org/licenses/old-licenses/gpl-1.0.html 
    2323 */ 
    2424 
    25 /* Set up the plugin on the 'plugins_loaded' hook. */ 
    26 add_action( 'plugins_loaded', 'custom_classes_setup' ); 
     25namespace Custom_Classes; 
    2726 
    2827/** 
    29  * Plugin setup function.  Loads the translation files and adds each action and filter to their appropriate hook. 
     28 * Singleton class for setting up the plugin. 
    3029 * 
    31  * @since 0.1.0 
     30 * @since  1.0.0 
     31 * @access public 
    3232 */ 
    33 function custom_classes_setup() { 
     33final class Plugin { 
    3434 
    35     /* Loads the plugin translation files. */ 
    36     load_plugin_textdomain( 'custom-classes', false, 'custom-classes/languages' ); 
     35    /** 
     36     * Plugin directory path. 
     37     * 
     38     * @since  1.0.0 
     39     * @access public 
     40     * @var    string 
     41     */ 
     42    public $dir = ''; 
    3743 
    38     /* Register post type support for custom classes. */ 
    39     add_action( 'init', 'custom_classes_post_type_support' ); 
     44    /** 
     45     * Plugin directory URI. 
     46     * 
     47     * @since  1.0.0 
     48     * @access public 
     49     * @var    string 
     50     */ 
     51    public $uri = ''; 
    4052 
    41     /* Register metadata. */ 
    42     add_action( 'init', 'custom_classes_register_meta' ); 
     53    /** 
     54     * Returns the instance. 
     55     * 
     56     * @since  1.0.0 
     57     * @access public 
     58     * @return object 
     59     */ 
     60    public static function get_instance() { 
    4361 
    44     /* Filter the <body> class. */ 
    45     add_filter( 'body_class', 'custom_classes_body_class' ); 
     62        static $instance = null; 
    4663 
    47     /* Filter the post class. */ 
    48     add_filter( 'post_class', 'custom_classes_post_class', 10, 3 ); 
     64        if ( is_null( $instance ) ) { 
     65            $instance = new self; 
     66            $instance->setup(); 
     67            $instance->includes(); 
     68            $instance->setup_actions(); 
     69        } 
    4970 
    50     /* Register the meta box. */ 
    51     add_action( 'add_meta_boxes', 'custom_classes_add_meta_boxes', 10, 2 ); 
    52  
    53     /* Save the post meta. */ 
    54     add_action( 'save_post', 'custom_classes_save_meta', 10, 2 ); 
    55 } 
    56  
    57 /** 
    58  * Adds post type support for 'custom-classes' to all 'public' post types registered.  To unregister support  
    59  * for custom classes for a specific post type, use the remove_post_type_support() function. 
    60  * 
    61  * @since 0.1.0 
    62  */ 
    63 function custom_classes_post_type_support() { 
    64  
    65     /* Get all available 'public' post types. */ 
    66     $post_types = get_post_types( array( 'public' => true ) ); 
    67  
    68     /* Loop through each of the public post types and add support for custom classes. */ 
    69     foreach ( $post_types as $type ) 
    70         add_post_type_support( $type, 'custom-classes' ); 
    71 } 
    72  
    73 /** 
    74  * Registers the '_custom_body_class' and '_custom_post_class' meta keys for posts. 
    75  * 
    76  * @since 0.1.0 
    77  */ 
    78 function custom_classes_register_meta() { 
    79  
    80     /* Register custom body class meta. */ 
    81     register_meta( 
    82         'post', 
    83         '_custom_body_class', 
    84         'custom_classes_sanitize_meta', 
    85         'custom_classes_auth_post_meta' 
    86     ); 
    87  
    88     /* Register custom post class meta. */ 
    89     register_meta( 
    90         'post', 
    91         '_custom_post_class', 
    92         'custom_classes_sanitize_meta', 
    93         'custom_classes_auth_post_meta' 
    94     ); 
    95 } 
    96  
    97 /** 
    98  * Checks if viewing a single post and if the post has a custom body class registered.  If so, it adds the class  
    99  * to the array of body classes. 
    100  * 
    101  * @since 0.1.0 
    102  * @param array $classes Array of HTML classes used in the <body> tag. 
    103  * @return array $classes 
    104  */ 
    105 function custom_classes_body_class( $classes ) { 
    106  
    107     if ( is_singular() ) { 
    108         $custom_class = get_post_meta( get_queried_object_id(), '_custom_body_class', true ); 
    109  
    110         if ( !empty( $custom_class ) ) 
    111             $classes[] = sanitize_html_class( $custom_class ); 
     71        return $instance; 
    11272    } 
    11373 
    114     return $classes; 
    115 } 
     74    /** 
     75     * Constructor method. 
     76     * 
     77     * @since  1.0.0 
     78     * @access private 
     79     * @return void 
     80     */ 
     81    private function __construct() {} 
    11682 
    117 /** 
    118  * Checks if a post has a custom post class and adds it to the post wrapper element's classes. 
    119  * 
    120  * @since 0.1.0 
    121  * @param array $classes Array of HTML classes used in the post wrapper. 
    122  * @param string $class Custom class added by the user. 
    123  * @param int $post_id The ID of the post currently being shown. 
    124  * @return array $classes 
    125  */ 
    126 function custom_classes_post_class( $classes, $class, $post_id ) { 
     83    /** 
     84     * Sets up globals. 
     85     * 
     86     * @since  1.0.0 
     87     * @access private 
     88     * @return void 
     89     */ 
     90    private function setup() { 
    12791 
    128     $custom_class = get_post_meta( $post_id, '_custom_post_class', true ); 
    129  
    130     if ( !empty( $custom_class ) ) 
    131         $classes[] = sanitize_html_class( $custom_class ); 
    132  
    133     return $classes; 
    134 } 
    135  
    136 /** 
    137  * Callback function for sanitizing the meta value when add_post_meta() or update_post_meta() are called. 
    138  * 
    139  * @since 0.1.0 
    140  * @param string $meta_value The custom body or post class. 
    141  * @param string $meta_key The current meta key ('_custom_body_class' or '_custom_post_class'). 
    142  * @param string $meta_type The type of meta (post, comment, user, etc.). 
    143  * @return string $meta_value 
    144  */ 
    145 function custom_classes_sanitize_meta( $meta_value, $meta_key, $meta_type ) { 
    146     return sanitize_html_class( $meta_value ); 
    147 } 
    148  
    149 /** 
    150  * Checks if the user can edit the post meta.  By default, any user who can edit the post can also edit its meta.  To 
    151  * change this, add a filter on the "auth_post_meta_{$meta_key}" hook. 
    152  * 
    153  * @since 0.1.0 
    154  * @param bool $allowed Whether the user can edit the meta. True | False. 
    155  * @param string $meta_key The current meta key being checked against. 
    156  * @param string $post_id The current post being edited. 
    157  * @param string $cap The capability being checked (edit_post_meta, add_post_meta, delete_post_meta). 
    158  * @param array $caps An array of capabilities the user must have to edit the meta. 
    159  * @return bool $allowed 
    160  */ 
    161 function custom_classes_auth_post_meta( $allowed, $meta_key, $post_id, $user_id, $cap, $caps ) { 
    162  
    163     if ( '_custom_body_class' == $meta_key || '_custom_post_class' == $meta_key ) { 
    164  
    165         $post_type = get_post_type_object( get_post_type( $post_id ) ); 
    166  
    167         if ( user_can( $user_id, $post_type->cap->edit_post, $post_id ) ) 
    168             $allowed = true; 
     92        // Main plugin directory path and URI. 
     93        $this->dir = trailingslashit( plugin_dir_path( __FILE__ ) ); 
     94        $this->uri = trailingslashit( plugin_dir_url(  __FILE__ ) ); 
    16995    } 
    17096 
    171     return $allowed; 
    172 } 
     97    /** 
     98     * Loads files needed by the plugin. 
     99     * 
     100     * @since  1.0.0 
     101     * @access private 
     102     * @return void 
     103     */ 
     104    private function includes() { 
    173105 
    174 /** 
    175  * Adds the custom classes meta box to the edit post screen for any post type that supports it and if the user can  
    176  * edit the post meta. 
    177  * 
    178  * @since 0.1.0 
    179  * @param string $post_type The post type of the current post. 
    180  * @param object $post The current post object. 
    181  */ 
    182 function custom_classes_add_meta_boxes( $post_type, $post ) { 
     106        // Load filters and functions. 
     107        require_once( $this->dir . 'inc/functions-filters.php' ); 
    183108 
    184     if ( post_type_supports( $post_type, 'custom-classes' ) && ( current_user_can( 'edit_post_meta', $post->ID, '_custom_body_class' ) || current_user_can( 'edit_post_meta', $post->ID, '_custom_post_class' ) ) ) 
    185         add_meta_box( 'custom-classes', __( 'Classes', 'custom-classes' ), 'custom_classes_meta_box', $post_type, 'side', 'default' ); 
    186 } 
     109        // Load admin files. 
     110        if ( is_admin() ) { 
    187111 
    188 /** 
    189  * Displays the custom classes meta box with input fields for the body and post classes. 
    190  * 
    191  * @since 0.1.0 
    192  * @param object $post The current post object. 
    193  * @param array $metabox An array of arguments for the metabox. 
    194  */ 
    195 function custom_classes_meta_box( $post, $metabox ) { ?> 
     112            // Admin classes. 
     113            require_once( $this->dir . 'admin/class-post-edit.php' ); 
     114            require_once( $this->dir . 'admin/class-term-edit.php' ); 
    196115 
    197     <?php wp_nonce_field( basename( __FILE__ ), 'custom-classes-nonce' ); ?> 
     116            Post_Edit::get_instance(); 
     117            Term_Edit::get_instance(); 
     118        } 
     119    } 
    198120 
    199     <?php if ( current_user_can( 'edit_post_meta', $post->ID, '_custom_body_class' ) ) { ?> 
    200         <p> 
    201             <label for="custom-body-class"><?php _e( 'Body Class', 'custom-classes' ); ?></label> 
    202             <input type="text" name="custom-body-class" id="custom-body-class" value="<?php echo esc_attr( get_post_meta( $post->ID, '_custom_body_class', true ) ); ?>" size="30" tabindex="30" style="width: 99%;" /> 
    203         </p> 
    204     <?php } ?> 
     121    /** 
     122     * Sets up main plugin actions and filters. 
     123     * 
     124     * @since  1.0.0 
     125     * @access private 
     126     * @return void 
     127     */ 
     128    private function setup_actions() { 
    205129 
    206     <?php if ( current_user_can( 'edit_post_meta', $post->ID, '_custom_post_class' ) ) { ?> 
    207         <p> 
    208             <label for="custom-post-class"><?php _e( 'Post Class', 'custom-classes' ); ?></label> 
    209             <input type="text" name="custom-post-class" id="custom-post-class" value="<?php echo esc_attr( get_post_meta( $post->ID, '_custom_post_class', true ) ); ?>" size="30" tabindex="30" style="width: 99%;" /> 
    210         </p> 
    211     <?php } 
    212 } 
     130        // Internationalize the text strings used. 
     131        add_action( 'plugins_loaded', array( $this, 'i18n' ), 2 ); 
     132    } 
    213133 
    214 /** 
    215  * Saves the custom classes post meta when the 'save_post' hook is fired. 
    216  * 
    217  * @since 0.1.0 
    218  * @param int $post_id The ID of the post being saved. 
    219  * @param array $post An array of the post object. 
    220  */ 
    221 function custom_classes_save_meta( $post_id, $post ) { 
     134    /** 
     135     * Loads the translation files. 
     136    * 
     137     * @since  1.0.0 
     138     * @access public 
     139     * @return void 
     140    */ 
     141    public function i18n() { 
    222142 
    223     /* Verify the nonce before proceeding. */ 
    224     if ( !isset( $_POST['custom-classes-nonce'] ) || !wp_verify_nonce( $_POST['custom-classes-nonce'], basename( __FILE__ ) ) ) 
    225         return $post_id; 
    226  
    227     /* Add the posted metadata to an array. */ 
    228     $metadata = array( 
    229         '_custom_body_class' => $_POST['custom-body-class'], 
    230         '_custom_post_class' => $_POST['custom-post-class'] 
    231     ); 
    232  
    233     /* Loop through the posted meta and add, update, or delete. */ 
    234     foreach ( $metadata as $meta_key => $new_meta_value ) { 
    235  
    236         /* Get the meta value of the custom field key. */ 
    237         $meta_value = get_post_meta( $post_id, $meta_key, true ); 
    238  
    239         /* If there is no new meta value but an old value exists, delete it. */ 
    240         if ( current_user_can( 'delete_post_meta', $post_id, $meta_key ) && '' == $new_meta_value && $meta_value ) 
    241             delete_post_meta( $post_id, $meta_key, $meta_value ); 
    242  
    243         /* If a new meta value was added and there was no previous value, add it. */ 
    244         elseif ( current_user_can( 'add_post_meta', $post_id, $meta_key ) && $new_meta_value && '' == $meta_value ) 
    245             add_post_meta( $post_id, $meta_key, $new_meta_value, true ); 
    246  
    247         /* If the old layout doesn't match the new layout, update the post meta. */ 
    248         elseif ( current_user_can( 'edit_post_meta', $post_id, $meta_key ) && $meta_value !== $new_meta_value ) 
    249             update_post_meta( $post_id, $meta_key, $new_meta_value ); 
     143        load_plugin_textdomain( 'custom-classes', false, trailingslashit( dirname( plugin_basename( __FILE__ ) ) ) . 'lang' ); 
    250144    } 
    251145} 
    252146 
    253 ?> 
     147Plugin::get_instance(); 
  • custom-classes/trunk/readme.txt

    r501186 r1737208  
    11=== Custom Classes === 
     2 
    23Contributors: greenshady 
    3 Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=3687060 
    4 Tags: admin, password 
    5 Requires at least: 3.3 
    6 Tested up to: 3.3.1 
    7 Stable tag: 0.1 
     4Donate link: https://themehybrid.com/donate 
     5Tags: classes 
     6Requires at least: 4.8 
     7Tested up to: 4.8.2 
     8Requires PHP: 5.3 
     9Stable tag: 1.0.0 
     10License: GPLv2 or later 
     11License URI: http://www.gnu.org/licenses/gpl-2.0.html 
    812 
    9 Allows users to create custom post and body classes on a per-post basis. 
     13Allows users to create custom classes on a per-post/term basis. 
    1014 
    1115== Description == 
    1216 
    13 The Custom Classes plugin allows you to create custom classes for posts (when `post_class()` is called) and for the `<body>` class (when `body_class()` is called).  It creates a meta box on the edit post screen in the admin with two input boxes for adding your custom classes. 
     17The Custom Classes plugin allows you to create custom classes for posts (when `post_class()` is called) and for the `<body>` class (when `body_class()` is called).  It creates a meta box on the edit post and edit term screens in the admin with input boxes for adding your custom classes. 
    1418 
    1519Custom post classes are added whenever your theme calls the `post_class()` function, which is generally whenever a post is shown.  Custom body classes are added on the single view of the post whenever your theme calls the `body_class()` function. 
    1620 
    17 == Installation == 
     21### Like this plugin? 
    1822 
    19 1. Upload `custom-classes` to the `/wp-content/plugins/` directory. 
    20 1. Activate the plugin through the 'Plugins' menu in WordPress. 
    21 1. Go to your user profile in the admin to select whether to prevent password resetting. 
     23Please consider helping the cause by: 
     24 
     25* [Making a donation](https://themehybrid.com/donate). 
     26* [Signing up at my site](https://themehybrid.com/club). 
     27* [Rating the plugin](https://wordpress.org/support/view/plugin-reviews/custom-classes?rate=5#postform). 
     28 
     29### Professional Support 
     30 
     31If you need professional plugin support from me, the plugin author, you can access the support forums at [Theme Hybrid](https://themehybrid.com/board/topics), which is a professional WordPress help/support site where I handle support for all my plugins and themes for a community of 75,000+ users (and growing). 
     32 
     33### Plugin Development 
     34 
     35If you're a theme author, plugin author, or just a code hobbyist, you can follow the development of this plugin on it's [GitHub repository](https://github.com/justintadlock/custom-classes). 
    2236 
    2337== Frequently Asked Questions == 
    2438 
    25 = Why was this plugin created? = 
     39### Why was this plugin created? 
    2640 
    2741I wanted a quick and easy way to add styles to my posts without having to make a tag, create a category, or use post formats.  Those solutions are great, but they're not ideal for every situation.  By using a custom class, I have complete control over post styles. 
    2842 
    29 = How do I use it? = 
     43### How do I use it? 
    3044 
    31 A meta box named "Classes" gets added to the edit post screen in the admin.  From there, you can input a custom post and/or body class for individual posts. 
     45A meta box named "Classes" gets added to the edit post screen in the admin.  From there, you can input a custom post and/or body class for individual posts.  On the edit/new term (category, tag, etc.) screen, a new field labeled "Body Class" is also available. 
    3246 
    3347Of course, you actually have to style the class in your theme's `style.css` file for anything to actually change on the front end.  This plugin just outputs the classes for you.  It's up to you to decide how to use them. 
    3448 
    35 = Will this work with other post types? = 
     49## Will this work with custom post types? 
    3650 
    3751Certainly.  This plugin works with any public post type on your site.  It's not just limited to the "post" post type. 
    3852 
    39 = It's not working! = 
     53### It's not working! 
    4054 
    41 Most likely, this means your theme isn't using the appropriate functions (`post_class()` and `body_class()`).  You'll need to talk to your theme author to get them to fix this.  Or, better yet, use a correctly-coded theme from <a href="http://themehybrid.com">Theme Hybrid</a>! 
     55Most likely, this means your theme isn't using the appropriate functions (`post_class()` and `body_class()`).  You'll need to talk to your theme author to get them to fix this.  Or, better yet, use a correctly-coded theme from <a href="https://themehybrid.com/themes">Theme Hybrid</a>! 
    4256 
    4357== Screenshots == 
     
    4862== Changelog == 
    4963 
    50 **Version 0.1** 
    51  
    52 * Plugin launch.  Everything's new! 
     64The change log is located in the `changelog.md` file in the plugin folder.  You may also [view the change log](https://github.com/justintadlock/custom-classes/blob/master/changelog.md) online. 
Note: See TracChangeset for help on using the changeset viewer.