Advertisement
Guest User

Buddypress Group Content

a guest
May 13th, 2016
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.53 KB | None | 0 0
  1. <?php
  2. // Check that BP_Group_Extension Class Exists
  3. if( class_exists( 'BP_Group_Extension' ) ) {
  4.     class Buddypress_Groups_Custom_Meta extends BP_Group_Extension {
  5.  
  6.         public function __construct() {
  7.             $this->setup_hooks();
  8.         }
  9.  
  10.         private function setup_hooks() {
  11.             // Add new Metabox for XLSuite Fields (ID & Updated At)
  12.             add_action( 'bp_groups_admin_meta_boxes', array( $this, 'bp_groups_admin_meta_boxes' ) );
  13.  
  14.             // Store Group Meta after Admin Save
  15.             add_action( 'bp_group_admin_edit_after',  array( $this, 'bp_group_admin_content_edit_after'), 10, 1 );
  16.         }
  17.  
  18.         /**
  19.          * Register a new Metabox in Group Admin UI's Edit Group Panel
  20.          * Create Meta Box for Content Editor
  21.          * Create Meta Box for XLSuite ID and XLSuite Updated At
  22.          * @uses add_meta_box() to register our meta box
  23.          */
  24.         public function bp_groups_admin_meta_boxes() {
  25.             add_meta_box(
  26.                 'bp_groups_metabox_content',
  27.                 __( 'Group Information', 'wplms_customizer' ),
  28.                 array( &$this, 'bp_groups_admin_info_metabox'),
  29.                 get_current_screen()->id,
  30.                 'normal',
  31.                 'high'
  32.             );
  33.         }
  34.  
  35.         /**
  36.          * Display the Content Metabox and Field
  37.          * @param  BP_Groups_Group $item the group being edited
  38.          * @uses   groups_get_groupmeta() to get the featured attribute of the group
  39.          * @uses   checked() to eventually add a checked attribute if the group is featured
  40.          * @uses   wp_nonce_field() for security reasons
  41.          */
  42.         public function bp_groups_admin_info_metabox( $group = false ) {
  43.             if( empty( $group ) )
  44.                 return;
  45.             $prefix = 'bp_group_';
  46.             // Set-up Custom Fields to Create
  47.             $custom_fields = array();
  48.             $custom_fields[] = array(
  49.                 'label'     => __('Group Content','wplms-customizer'),
  50.                 'desc'      => __('The Group Content Editor.','wplms-customizer'),
  51.                 'id'        => $prefix.'content_editor',
  52.                 'type'      => 'editor'
  53.             );
  54.             ?>
  55.             <table class="form-table meta_box">
  56.                 <tbody>
  57.                     <?php
  58.                         foreach ( $custom_fields as $custom_field ) {
  59.                             $custom_field_value = groups_get_groupmeta( $group->id, $custom_field['id'] );
  60.                             if ( $custom_field['type'] == 'editor' ) { ?>
  61.                                 <tr>
  62.                                     <th>
  63.                                         <label for="<?php echo $custom_field['id']; ?>"><?php echo $custom_field['label']; ?></label>
  64.                                     </th>
  65.                                     <td>
  66.                                         <?php wp_editor( $custom_field_value, $custom_field['id'] , array(
  67.                                             'wpautop'       => true,
  68.                                             'media_buttons' => true,
  69.                                             'teeny'         => true,
  70.                                             'textarea_rows' => '4',
  71.                                             'textarea_cols' => '30',
  72.                                             'tinymce'       => array(
  73.                                                 'theme_advanced_buttons1'        => 'save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,cut,copy,paste,pastetext,pasteword,',
  74.                                                 'theme_advanced_buttons2'       => "styleselect,formatselect,fontselect,fontsizeselect,",
  75.                                                 'theme_advanced_buttons3'       => ",bullist,numlist,|,outdent,indent,blockquote,|,link,anchor,image,|,insertdate,forecolor,backcolor,|,tablecontrols,|,hr,|,fullscreen",
  76.                                                 'theme_advanced_buttons4'       => "",
  77.                                                 'theme_advanced_text_colors'    => '0f3156,636466,0486d3',
  78.                                             ),
  79.                                             'quicktags'     => array(
  80.                                                 'buttons'   => 'strong,em,link,block,del,ins,img,code,ul,ol,li,close'
  81.                                             )
  82.                                         ) ); ?>
  83.                                         <span class="description"><?php echo $custom_field['desc']; ?></span>
  84.                                     </td>
  85.                                 </tr>
  86.                             <?php
  87.                             } else {
  88.                             ?>
  89.                                 <tr>
  90.                                     <th>
  91.                                         <label for="<?php echo $custom_field['id']; ?>"><?php echo $custom_field['label']; ?></label>
  92.                                     </th>
  93.                                     <td>
  94.                                         <input id="<?php echo $custom_field['id']; ?>" type="<?php echo $custom_field['type']; ?>" name="<?php echo $custom_field['id']; ?>" value="<?php echo $custom_field_value; ?>" size="20" />
  95.                                         <span class="description"><?php echo $custom_field['desc']; ?></span>
  96.                                     </td>
  97.                                 </tr>
  98.                             <?php
  99.                             }
  100.                         }
  101.                     ?>
  102.                 </tbody>
  103.             </table>
  104.             <?php
  105.             wp_nonce_field( 'bp_group_content_save_' . $group->id, 'bp_group_content_admin' );
  106.         }
  107.  
  108.         /**
  109.          * Saves the Content Field
  110.          * @param  integer $group_id the group id for the group being edited
  111.          * @uses   check_admin_referer() for security reasons
  112.          * @uses   groups_get_groupmeta() to get the existing group meta
  113.          * @uses   groups_update_groupmeta() to update the group meta
  114.          * @uses   groups_delete_groupmeta() to remove the group meta if empty
  115.          */
  116.         function bp_group_admin_content_edit_after( $group_id = 0 ) {
  117.             if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) || empty( $group_id ) )
  118.                 return false;
  119.  
  120.             check_admin_referer('bp_group_content_save_' . $group_id, 'bp_group_content_admin')
  121.  
  122.             $content = groups_get_groupmeta( $group_id, 'bp_group_content_editor' );
  123.             $new_content = '';
  124.             if ( array_key_exists( 'bp_group_content_editor', $_POST ) && isset( $_POST['bp_group_content_editor'] ) && ! empty( trim( $_POST['bp_group_content_editor'] ) ) ) {
  125.                 $new_content = $_POST['bp_group_content_editor'];
  126.             }
  127.             if( ! empty( $new_content ) ) {
  128.                 groups_update_groupmeta($group_id, 'bp_group_content_editor', $new_content);
  129.             } elseif ( ! empty( $content ) ) {
  130.                 groups_delete_groupmeta($group_id, 'bp_group_content_editor');
  131.             }
  132.  
  133.         }
  134.  
  135.     }
  136.  
  137.     /**
  138.      * Initalize the Buddypress_Groups_Custom_Meta Class
  139.      *
  140.      * @since     1.0.0
  141.      * @uses bp_is_active([component]) to check the group component is active
  142.      */
  143.     function buddypress_groups_custom_meta() {
  144.         if(bp_is_active('groups')) {
  145.             return new Buddypress_Groups_Custom_Meta();
  146.         }
  147.     }
  148.  
  149.     add_action('bp_init', 'buddypress_groups_custom_meta');
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement