Advertisement
danfolt

comment-meta-demo

Jun 7th, 2013
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.89 KB | None | 0 0
  1. <?php # -*- coding: utf-8 -*-
  2. namespace WPSE;
  3. /**
  4.  * Plugin Name: Comment Meta Demo
  5.  * Description: Create, save and display a comment meta field. Here, a commentator can select a role.
  6.  * Plugin URI:  http://wordpress.stackexchange.com/q/101579/73
  7.  * Version:     2013.06.06
  8.  * Author:      Thomas Scholz
  9.  * Author URI:  http://toscho.de
  10.  * Licence:     MIT
  11.  * License URI: http://opensource.org/licenses/MIT
  12.  */
  13.  
  14. \add_action(
  15.     'wp_loaded',
  16.     array( __NAMESPACE__ . '\Comment_Meta_Controller', 'init' )
  17. );
  18.  
  19. /**
  20.  * Controller
  21.  *
  22.  * Assigns Views and models to actions and filters
  23.  */
  24. class Comment_Meta_Controller
  25. {
  26.     /**
  27.      * Callback for add_action(). Creates a new instance.
  28.      *
  29.      * @wp-hook login_init
  30.      */
  31.     public function init()
  32.     {
  33.         return new self;
  34.     }
  35.  
  36.     /**
  37.      * Set up objects, register footer action callback.
  38.      *
  39.      * @wp-hook login_init
  40.      */
  41.     protected function __construct()
  42.     {
  43.  
  44.        
  45.         // Use this for custom roles instead
  46.         $data   = new Comment_Meta_Custom_Roles( '_comment_role' );
  47.         $input  = new Comment_Meta_Role_Selector( $data );
  48.         $output = new Comment_Meta_Role_Display( $data );
  49.  
  50.         // remove this if you want to show the select field with
  51.         // do_action( 'comment_role_selector' );
  52.         \add_filter( 'comment_form_field_comment', array ( $input, 'show' ), 10, 2 );
  53.  
  54.         \add_action( 'comment_role_selector', array ( $input, 'print_select' ) );
  55.  
  56.         // remove this if you want to show the select field with
  57.         // do_action( 'comment_role_selector' );
  58.         \add_filter( 'comment_text', array ( $output, 'show' ), 10, 2 );
  59.  
  60.         \add_action( 'comment_role_value', array ( $output, 'show_action' ), 10, 2 );
  61.  
  62.         if ( 'POST' === $_SERVER[ 'REQUEST_METHOD' ] )
  63.             \add_action( 'comment_post', array ( $data, 'save' ) );
  64.     }
  65. }
  66.  
  67. /**
  68.  * Base class for handling comment meta data.
  69.  */
  70. abstract class Comment_Meta_Data_Model
  71. {
  72.     /**
  73.      * Meta key
  74.      *
  75.      * @type string
  76.      */
  77.     protected $key;
  78.  
  79.     /**
  80.      * Constructor
  81.      *
  82.      * @param string $key
  83.      */
  84.     public function __construct( $key )
  85.     {
  86.         $this->key = $key;
  87.     }
  88.  
  89.     /**
  90.      * Get current key
  91.      *
  92.      * @return string
  93.      */
  94.     public function get_key()
  95.     {
  96.         return $this->key;
  97.     }
  98.  
  99.     /**
  100.      * Wrapper for the native get_comment_meta()
  101.      *
  102.      * @param  int    $comment_ID
  103.      * @return string
  104.      */
  105.     public function get_comment_meta( $comment_ID )
  106.     {
  107.         $meta    = \get_comment_meta( $comment_ID, $this->key, TRUE );
  108.         $allowed = $this->get_allowed_values();
  109.  
  110.         // get real display value
  111.         if ( isset ( $allowed[ $meta ] ) )
  112.             return $allowed[ $meta ];
  113.  
  114.         return '';
  115.     }
  116.  
  117.     /**
  118.      * Save comment mate data.
  119.      *
  120.      * @param  int  $comment_ID
  121.      * @return bool
  122.      */
  123.     public function save( $comment_ID )
  124.     {
  125.         $role_keys = array_keys( $this->get_allowed_values() );
  126.  
  127.         if ( ! isset ( $_POST[ $this->key ] ) )
  128.             return;
  129.  
  130.         if ( ! in_array( $_POST[ $this->key ], $role_keys ) )
  131.             return;
  132.  
  133.         return \update_comment_meta( $comment_ID, $this->key, $_POST[ $this->key ] );
  134.     }
  135.  
  136.     /**
  137.      * Get user role.
  138.      */
  139.     public function get_current_value()
  140.     {
  141.         $user = \wp_get_current_user();
  142.  
  143.         if ( empty ( $user->roles ) )
  144.             return array ();
  145.  
  146.         return $user->roles;
  147.     }
  148.  
  149.     /**
  150.      * @return array
  151.      */
  152.     abstract public function get_allowed_values();
  153. }
  154. /**
  155.  * User roles as comment meta.
  156.  */
  157. class Comment_Meta_Builtin_Roles extends Comment_Meta_Data_Model
  158. {
  159.     /**
  160.      * (non-PHPdoc)
  161.      * @see WPSE.Comment_Meta_Data_Model::get_allowed_values()
  162.      */
  163.     public function get_allowed_values()
  164.     {
  165.         global $wp_roles;
  166.  
  167.         if ( empty ( $wp_roles ) )
  168.             $wp_roles = new \WP_Roles;
  169.  
  170.         $output = array();
  171.  
  172.         foreach ( $wp_roles->roles as $identifier => $role )
  173.             $output[ $identifier ] = $role['name'];
  174.  
  175.         return $output;
  176.     }
  177. }
  178. /**
  179. /**
  180.  * Custom roles for comment meta.
  181.  */
  182. class Comment_Meta_Custom_Roles extends Comment_Meta_Data_Model
  183. {
  184.     /**
  185.      * (non-PHPdoc)
  186.      * @see WPSE.Comment_Meta_Data_Model::get_allowed_values()
  187.      */
  188.     public function get_allowed_values()
  189.     {
  190.         return array (
  191.           'job_applicant' => 'Job Applicant',
  192.             'agency' => 'Agency',
  193.             'club' => 'Club'
  194.         );
  195.     }
  196. }
  197.  
  198. /**
  199.  * Base class to show comment meta data.
  200.  */
  201. class Comment_Meta_View
  202. {
  203.     /**
  204.      * Model
  205.      *
  206.      * @type Comment_Meta_Data_Model
  207.      */
  208.     protected $data;
  209.  
  210.     /**
  211.      * Constructor.
  212.      *
  213.      * @param Comment_Meta_Data_Model $data
  214.      */
  215.     public function __construct( Comment_Meta_Data_Model $data )
  216.     {
  217.         $this->data = $data;
  218.     }
  219. }
  220. /**
  221.  * Show role selector from comment meta
  222.  */
  223. class Comment_Meta_Role_Selector extends Comment_Meta_View
  224. {
  225.     /**
  226.      * Add 'select' field before textarea.
  227.      *
  228.      * @param  string $text_field
  229.      * @return string
  230.      */
  231.     public function show( $text_field )
  232.     {
  233.         return $this->get_select() . $text_field;
  234.     }
  235.  
  236.     /**
  237.      * Select element.
  238.      *
  239.      * @return string
  240.      */
  241.     public function get_select()
  242.     {
  243.         $allowed = $this->data->get_allowed_values();
  244.         $current = $this->data->get_current_value();
  245.         $key     = $this->data->get_key();
  246.  
  247.         // is the current value part of the allowed values?
  248.         if ( ! empty ( $current ) && array() !== array_intersect( $allowed, $current ) )
  249.             return $this->get_hidden_field( $key, $current[0] );
  250.  
  251.         $select = '<p>';
  252.         $select .= sprintf( '<label for="%1$s_id">Your role:</label>
  253.            <select name="%1$s" id="%1$s_id">',
  254.             $key
  255.         );
  256.         $select .= '<option value="">Select a role</option>';
  257.  
  258.         foreach ( $allowed as $internal => $display )
  259.             $select .= sprintf(
  260.                 '<option value="%1$s">%2$s</option>',
  261.                 \esc_attr( $internal ),
  262.                 \esc_html( $display )
  263.             );
  264.  
  265.         return $select . '</select></p>';
  266.     }
  267.  
  268.     /**
  269.      * Print preselcted role as hidden input field.
  270.      *
  271.      * @param  string $name Field name
  272.      * @param  string $role Internal role name
  273.      * @return string
  274.      */
  275.     protected function get_hidden_field( $name, $role )
  276.     {
  277.         return sprintf(
  278.             '<input type="hidden" name="%1$s" value="%2$s">',
  279.             $name,
  280.             esc_attr( $role )
  281.         );
  282.     }
  283.  
  284.     /**
  285.      * Callback for do_action.
  286.      *
  287.      * @wp-hook comment_role_selector
  288.      * @return  void
  289.      */
  290.     public function print_select()
  291.     {
  292.         print $this->get_select();
  293.     }
  294. }
  295.  
  296. /**
  297.  * Show current comment role.
  298.  */
  299. class Comment_Meta_Role_Display extends Comment_Meta_View
  300. {
  301.     /**
  302.      * Add role to comment text.
  303.      *
  304.      * @wp-hook comment_text
  305.      * @param   string $text
  306.      * @param   object $comment
  307.      * @return  string
  308.      */
  309.     public function show( $text, $comment )
  310.     {
  311.         $role = $this->data->get_comment_meta( $comment->comment_ID );
  312.  
  313.         if ( '' !== $role )
  314.             $text = "Role: $role<br> $text";
  315.  
  316.         return $text;
  317.     }
  318.  
  319.     /**
  320.      * Print the comment meta value into a template.
  321.      *
  322.      * Usage: <code>do_action( 'comment_role_value', 'Role: %s<br>', $comment );
  323.      *
  324.      * @wp-hook comment_role_value
  325.      * @param   string $template
  326.      * @param   object $comment
  327.      * @return  void
  328.      */
  329.     public function show_action( $template, $comment )
  330.     {
  331.         $role = $this->data->get_comment_meta( $comment->comment_ID );
  332.  
  333.         if ( '' !== $role )
  334.             printf( $template, $role );
  335.     }
  336. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement