PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-content/plugins/cbxdynamicsidebar/admin/class-cbxdynamicsidebar-admin.php

https://gitlab.com/vanafroo/landingpage
PHP | 406 lines | 206 code | 91 blank | 109 comment | 15 complexity | 67f3a14e12cc39e0e0134affec3dc1e0 MD5 | raw file
  1. <?php
  2. /**
  3. * The admin-specific functionality of the plugin.
  4. *
  5. * @link http://wpboxr.com
  6. * @since 1.0.0
  7. *
  8. * @package Cbxdynamicsidebar
  9. * @subpackage Cbxdynamicsidebar/admin
  10. */
  11. /**
  12. * The admin-specific functionality of the plugin.
  13. *
  14. * Defines the plugin name, version, and two examples hooks for how to
  15. * enqueue the admin-specific stylesheet and JavaScript.
  16. *
  17. * @package Cbxdynamicsidebar
  18. * @subpackage Cbxdynamicsidebar/admin
  19. * @author WPBoxr <info@wpboxr.com>
  20. */
  21. class Cbxdynamicsidebar_Admin {
  22. /**
  23. * The ID of this plugin.
  24. *
  25. * @since 1.0.0
  26. * @access private
  27. * @var string $plugin_name The ID of this plugin.
  28. */
  29. private $plugin_name;
  30. /**
  31. * The version of this plugin.
  32. *
  33. * @since 1.0.0
  34. * @access private
  35. * @var string $version The current version of this plugin.
  36. */
  37. private $version;
  38. public $loader;
  39. /**
  40. * Initialize the class and set its properties.
  41. *
  42. * @since 1.0.0
  43. * @param string $plugin_name The name of this plugin.
  44. * @param string $version The version of this plugin.
  45. */
  46. public function __construct( $plugin_name, $version ) {
  47. $this->plugin_name = $plugin_name;
  48. $this->version = $version;
  49. }
  50. public function admin_init(){
  51. //$plugin_admin = new Cbxdynamicsidebar_Admin( $this->get_plugin_name(), $this->get_version() );
  52. //var_dump($this->loader);
  53. add_filter( 'manage_cbxsidebar_posts_columns', array($this, 'cbxsidebar_columns'), 10, 1);
  54. add_action('manage_cbxsidebar_posts_custom_column', array($this, 'cbxsidebar_column'), 10, 2);
  55. //manage_{$post_type}_posts_columns
  56. $this->loader->add_filter( 'manage_cbxsidebar_posts_columns', $this, 'cbxsidebar_columns', 10);
  57. }
  58. /**
  59. * Register the stylesheets for the admin area.
  60. *
  61. * @since 1.0.0
  62. */
  63. public function enqueue_styles() {
  64. /**
  65. * This function is provided for demonstration purposes only.
  66. *
  67. * An instance of this class should be passed to the run() function
  68. * defined in Cbxdynamicsidebar_Loader as all of the hooks are defined
  69. * in that particular class.
  70. *
  71. * The Cbxdynamicsidebar_Loader will then create the relationship
  72. * between the defined hooks and the functions defined in this
  73. * class.
  74. */
  75. wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/cbxdynamicsidebar-admin.css', array(), $this->version, 'all' );
  76. }
  77. /**
  78. * Register the JavaScript for the admin area.
  79. *
  80. * @since 1.0.0
  81. */
  82. public function enqueue_scripts() {
  83. /**
  84. * This function is provided for demonstration purposes only.
  85. *
  86. * An instance of this class should be passed to the run() function
  87. * defined in Cbxdynamicsidebar_Loader as all of the hooks are defined
  88. * in that particular class.
  89. *
  90. * The Cbxdynamicsidebar_Loader will then create the relationship
  91. * between the defined hooks and the functions defined in this
  92. * class.
  93. */
  94. wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/cbxdynamicsidebar-admin.js', array( 'jquery' ), $this->version, false );
  95. }
  96. // Register Custom Post Type
  97. public function create_sidebar() {
  98. $labels = array(
  99. 'name' => _x( 'Sidebars', 'Post Type General Name', $this->plugin_name ),
  100. 'singular_name' => _x( 'Sidebar', 'Post Type Singular Name', $this->plugin_name ),
  101. 'menu_name' => __( 'CBX Sidebars', $this->plugin_name ),
  102. 'parent_item_colon' => __( 'Parent Sidebar:', $this->plugin_name ),
  103. 'all_items' => __( 'All Sidebars', $this->plugin_name ),
  104. 'view_item' => __( 'View Sidebar', $this->plugin_name ),
  105. 'add_new_item' => __( 'Add New Sidebar', $this->plugin_name ),
  106. 'add_new' => __( 'Add New', $this->plugin_name ),
  107. 'edit_item' => __( 'Edit Sidebar', $this->plugin_name ),
  108. 'update_item' => __( 'Update Sidebar', $this->plugin_name ),
  109. 'search_items' => __( 'Search Sidebar', $this->plugin_name ),
  110. 'not_found' => __( 'Not found', $this->plugin_name ),
  111. 'not_found_in_trash' => __( 'Not found in Trash', $this->plugin_name ),
  112. );
  113. $args = array(
  114. 'label' => __( 'cbxsidebar', $this->plugin_name ),
  115. 'description' => __( 'Dynamic Sidebars', $this->plugin_name ),
  116. 'labels' => $labels,
  117. 'supports' => array( 'title' ),
  118. 'hierarchical' => false,
  119. 'public' => false,
  120. 'show_ui' => true,
  121. 'show_in_menu' => true,
  122. 'show_in_nav_menus' => true,
  123. 'show_in_admin_bar' => true,
  124. //'menu_position' => 5,
  125. 'menu_icon' => 'dashicons-list-view',
  126. 'can_export' => true,
  127. 'has_archive' => true,
  128. 'exclude_from_search' => true,
  129. 'publicly_queryable' => false,
  130. 'capability_type' => 'post',
  131. );
  132. register_post_type( 'cbxsidebar', $args );
  133. }
  134. /**
  135. * Register all custom dynamic sidebars
  136. */
  137. public function cbxdynamicsidebar_register_sidebars(){
  138. //get all post types(aka cbxdynamicsidebar type posts )
  139. $query_arg = array(
  140. 'post_type' => 'cbxsidebar',
  141. 'post_status' => 'publish',
  142. 'posts_per_page' => -1
  143. );
  144. $the_query = new WP_Query( $query_arg );
  145. /*echo '<pre>';
  146. print_r($the_query);
  147. echo '</pre>';*/
  148. while ( $the_query->have_posts() ) : $the_query->the_post();
  149. global $post;
  150. $id = $post->ID;
  151. //var_dump($id);
  152. $post_title = $post->post_title;
  153. $sidebar_custom = get_post_meta( $id, '_cbxdynamicsidebar', true);
  154. /*echo '<pre>';
  155. print_r($sidebar_custom);
  156. echo '</pre>';*/
  157. //exit();
  158. if(is_array($sidebar_custom) && sizeof($sidebar_custom) == 0) continue;
  159. //$sidebar_custom = $sidebar_custom[0];
  160. $active = intval($sidebar_custom['active']);
  161. if(!$active) continue;
  162. $description = html_entity_decode($sidebar_custom['description']);
  163. $before_widget = html_entity_decode($sidebar_custom['before_widget']);
  164. $after_widget = html_entity_decode($sidebar_custom['after_widget']);
  165. $before_title = html_entity_decode($sidebar_custom['before_title']);
  166. $after_title = html_entity_decode($sidebar_custom['after_title']);
  167. register_sidebar( array(
  168. 'name' => $post_title,
  169. 'id' => 'cbxdynamicsidebar-'.$id,
  170. 'description' => $description,
  171. 'before_widget' => $before_widget,
  172. 'after_widget' => $after_widget,
  173. 'before_title' => $before_title,
  174. 'after_title' => $after_title
  175. ) );
  176. endwhile;
  177. // Reset Post Data
  178. wp_reset_postdata();
  179. }
  180. public function sidebar_action_row($actions, $post){
  181. if ($post->post_type =="cbxsidebar"){
  182. unset( $actions['inline hide-if-no-js'] );
  183. unset( $actions['view'] );
  184. }
  185. return $actions;
  186. }
  187. function cbxsidebar_column( $column_name, $post_id ) {
  188. if ($column_name == 'shortcode') {
  189. echo '[cbxdynamicsidebar id="'.$post_id.'"]';
  190. }
  191. }
  192. public function cbxsidebar_columns($columns ){
  193. unset($columns['date']);
  194. $columns['shortcode'] = __('Shortcode', $this->plugin_name);
  195. return $columns;
  196. }
  197. public function add_meta_boxes(){
  198. //photo meta box
  199. add_meta_box(
  200. 'cbxdynamicsidebarmetabox', __('Sidebar Definitions', $this->plugin_name), array($this, 'cbxdynamicsidebarmetabox_display'), 'cbxsidebar', 'normal','high'
  201. );
  202. }
  203. public function cbxdynamicsidebarmetabox_display($post){
  204. $fieldValues = get_post_meta($post->ID, '_cbxdynamicsidebar', true);
  205. wp_nonce_field( 'cbxdynamicsidebarmetabox', 'cbxdynamicsidebarmetabox[nonce]' );
  206. //<input type="hidden" id="name_of_nonce_field" name="name_of_nonce_field" value="a62a76f53d">
  207. echo '<h2>'.__('Shortcode Usages:', $this->plugin_name).'</h2>';
  208. echo '<code>[cbxdynamicsidebar id="'.$post->ID.'" float="auto" wid="" wclass="" /]</code>';
  209. echo '<div id="cbxdynamicsidebarmetabox_wrapper">';
  210. $active = isset($fieldValues['active']) ? intval($fieldValues['active']): 1;
  211. $description = isset($fieldValues['description']) ? html_entity_decode($fieldValues['description']): __('Yet another Dynamic Sidebar', $this->plugin_name);
  212. $class = isset($fieldValues['class']) ? html_entity_decode($fieldValues['class']): 'cbxdynamicsidebar';
  213. $before_widget = isset($fieldValues['before_widget'])? html_entity_decode($fieldValues['before_widget']): '<div id="%1$s" class="widget widget-cbxdynamicsidebar %2$s">';
  214. $after_widget = isset($fieldValues['after_widget'])? html_entity_decode($fieldValues['after_widget']): '</div>';
  215. $before_title = isset($fieldValues['before_title'])? html_entity_decode($fieldValues['before_title']): '<h2 class="widget-title widget-title-cbxdynamicsidebar">';
  216. $after_title = isset($fieldValues['after_title'])? html_entity_decode($fieldValues['after_title']): '</h2>';
  217. //var_dump($active);
  218. ?>
  219. <table class="form-table"><tbody>
  220. <tr valign="top">
  221. <th scope="row"><label for="cbxdynamicsidebarmetabox_fields_class"><?php echo __('Sidebar Enabe/Disable', $this->plugin_name) ?></label></th>
  222. <td>
  223. <legend class="screen-reader-text"><span>input type="radio"</span></legend>
  224. <label title='g:i a'>
  225. <input type="radio" name="cbxdynamicsidebarmetabox[active]" value="0" <?php checked( $active, '0', TRUE ); ?> />
  226. <span><?php esc_attr_e( 'No', $this->plugin_name ); ?></span>
  227. </label><br>
  228. <label title='g:i a'>
  229. <input type="radio" name="cbxdynamicsidebarmetabox[active]" value="1" <?php checked( $active, '1', TRUE ); ?> />
  230. <span><?php esc_attr_e( 'Yes', $this->plugin_name ); ?></span>
  231. </label>
  232. </td>
  233. </tr>
  234. <tr valign="top">
  235. <th scope="row"><label for="cbxdynamicsidebarmetabox_fields_description"><?php echo __('Description', $this->plugin_name) ?></label></th>
  236. <td>
  237. <textarea id="cbxdynamicsidebarmetabox_fields_description" class="regular-text" name="cbxdynamicsidebarmetabox[description]" placeholder="<?php echo $description; ?>"><?php echo htmlentities($description); ?></textarea>
  238. </td>
  239. </tr>
  240. <tr valign="top">
  241. <th scope="row"><label for="cbxdynamicsidebarmetabox_fields_class"><?php echo __('CSS Class', $this->plugin_name) ?></label></th>
  242. <td>
  243. <input id="cbxdynamicsidebarmetabox_fields_class" class="regular-text" type="text" name="cbxdynamicsidebarmetabox[class]" placeholder="cbxdynamicsidebar" value="<?php echo htmlentities($class); ?>" />
  244. </td>
  245. </tr>
  246. <tr valign="top">
  247. <th scope="row"><label for="cbxdynamicsidebarmetabox_fields_before_widget"><?php echo __('Before Widget Html Wrapping', $this->plugin_name) ?></label></th>
  248. <td>
  249. <input id="cbxdynamicsidebarmetabox_fields_before_widget" class="regular-text" type="text" name="cbxdynamicsidebarmetabox[before_widget]" placeholder="<?php echo htmlentities($before_widget); ?>" value="<?php echo htmlentities($before_widget); ?>" />
  250. </td>
  251. </tr>
  252. <tr valign="top">
  253. <th scope="row"><label for="cbxdynamicsidebarmetabox_fields_after_widget"><?php echo __('After Widget Html Wrapping', $this->plugin_name) ?></label></th>
  254. <td>
  255. <input id="cbxdynamicsidebarmetabox_fields_after_widget" class="regular-text" type="text" name="cbxdynamicsidebarmetabox[after_widget]" placeholder="<?php echo htmlentities($after_widget); ?>" value="<?php echo htmlentities($after_widget); ?>" />
  256. </td>
  257. </tr>
  258. <tr valign="top">
  259. <th scope="row"><label for="cbxdynamicsidebarmetabox_fields_before_title"><?php echo __('Before Title Html Wrapping', $this->plugin_name) ?></label></th>
  260. <td>
  261. <input id="cbxdynamicsidebarmetabox_fields_before_title" class="regular-text" type="text" name="cbxdynamicsidebarmetabox[before_title]" placeholder="<?php echo htmlentities($before_title); ?>" value="<?php echo htmlentities($before_title); ?>" />
  262. </td>
  263. </tr>
  264. <tr valign="top">
  265. <th scope="row"><label for="cbxdynamicsidebarmetabox_fields_after_title"><?php echo __('After Title Html Wrapping', $this->plugin_name) ?></label></th>
  266. <td>
  267. <input id="cbxdynamicsidebarmetabox_fields_after_title" class="regular-text" type="text" name="cbxdynamicsidebarmetabox[after_title]" placeholder="<?php echo htmlentities($after_title); ?>" value="<?php echo htmlentities($after_title); ?>" />
  268. </td>
  269. </tr>
  270. </tbody></table>
  271. <?php
  272. echo '</div>';
  273. }//end display metabox
  274. /**
  275. * Determines whether or not the current user has the ability to save meta data associated with this post.
  276. *
  277. * @param int $post_id The ID of the post being save
  278. * @param bool Whether or not the user has the ability to save this post.
  279. */
  280. public function save_post($post_id, $post) {
  281. $post_type = 'cbxsidebar';
  282. // If this isn't a 'book' post, don't update it.
  283. if ( $post_type != $post->post_type ) {
  284. return;
  285. }
  286. if(!empty($_POST['cbxdynamicsidebarmetabox'])) {
  287. $postData = $_POST['cbxdynamicsidebarmetabox'];
  288. $saveableData = array();
  289. //if(!empty($postData['bdnewsphotobox'])) {
  290. if ($this->user_can_save($post_id, 'cbxdynamicsidebarmetabox', $postData['nonce'])) {
  291. $saveableData['active'] = intval($postData['active']);
  292. $saveableData['description'] = esc_attr($postData['description']);
  293. $saveableData['class'] = esc_attr($postData['class']);
  294. $saveableData['before_widget'] = esc_attr($postData['before_widget']);
  295. $saveableData['after_widget'] = esc_attr($postData['after_widget']);
  296. $saveableData['before_title'] = esc_attr($postData['before_title']);
  297. $saveableData['after_title'] = esc_attr($postData['after_title']);
  298. update_post_meta($post_id, '_cbxdynamicsidebar', $saveableData);
  299. }
  300. }
  301. }
  302. /**
  303. * Determines whether or not the current user has the ability to save meta data associated with this post.
  304. *
  305. * @param int $post_id The ID of the post being save
  306. * @param bool Whether or not the user has the ability to save this post.
  307. */
  308. function user_can_save($post_id, $action, $nonce) {
  309. $is_autosave = wp_is_post_autosave($post_id);
  310. $is_revision = wp_is_post_revision($post_id);
  311. $is_valid_nonce = ( isset($nonce) && wp_verify_nonce($nonce, $action) );
  312. // Return true if the user is able to save; otherwise, false.
  313. return !( $is_autosave || $is_revision ) && $is_valid_nonce;
  314. }// end user_can_save
  315. }