admin-config.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <?php
  2. /**
  3. * Custom CSS and JS
  4. *
  5. */
  6. if ( ! defined( 'ABSPATH' ) ) {
  7. exit; // Exit if accessed directly
  8. }
  9. /**
  10. * CustomCSSandJS_AdminConfig
  11. */
  12. class CustomCSSandJS_AdminConfig {
  13. var $settings_default;
  14. var $settings;
  15. /**
  16. * Constructor
  17. */
  18. public function __construct() {
  19. // Get the "default settings"
  20. $settings_default = apply_filters( 'ccj_settings_default', array() );
  21. // Get the saved settings
  22. $settings = get_option('ccj_settings');
  23. if ( $settings == false ) {
  24. $settings = $settings_default;
  25. } else {
  26. foreach( $settings_default as $_key => $_value ) {
  27. if ( ! isset($settings[$_key] ) ) {
  28. $settings[$_key] = $_value;
  29. }
  30. }
  31. }
  32. $this->settings = $settings;
  33. $this->settings_default = $settings_default;
  34. //Add actions and filters
  35. add_action( 'admin_menu', array( $this, 'admin_menu' ) );
  36. add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
  37. add_action( 'ccj_settings_form', array( $this, 'general_extra_form' ), 11 );
  38. add_filter( 'ccj_settings_default', array( $this, 'general_extra_default' ) );
  39. add_filter( 'ccj_settings_save', array( $this, 'general_extra_save' ) );
  40. }
  41. /**
  42. * Add submenu pages
  43. */
  44. function admin_menu() {
  45. $menu_slug = 'edit.php?post_type=custom-css-js';
  46. add_submenu_page( $menu_slug, __('Settings', 'custom-css-js'), __('Settings', 'custom-css-js'), 'manage_options', 'custom-css-js-config', array( $this, 'config_page' ) );
  47. }
  48. /**
  49. * Enqueue the scripts and styles
  50. */
  51. public function admin_enqueue_scripts( $hook ) {
  52. $screen = get_current_screen();
  53. // Only for custom-css-js post type
  54. if ( $screen->post_type != 'custom-css-js' )
  55. return false;
  56. if ( $hook != 'custom-css-js_page_custom-css-js-config' )
  57. return false;
  58. // Some handy variables
  59. $a = plugins_url( '/', CCJ_PLUGIN_FILE). 'assets';
  60. $v = CCJ_VERSION;
  61. wp_enqueue_script( 'tipsy', $a . '/jquery.tipsy.js', array('jquery'), $v, false );
  62. wp_enqueue_style( 'tipsy', $a . '/tipsy.css', array(), $v );
  63. }
  64. /**
  65. * Template for the config page
  66. */
  67. function config_page() {
  68. if ( isset( $_POST['ccj_settings-nonce'] ) ) {
  69. check_admin_referer('ccj_settings', 'ccj_settings-nonce');
  70. $data = apply_filters( 'ccj_settings_save', array() );
  71. $settings = get_option('ccj_settings');
  72. if ( !isset($settings['add_role'] ) ) $settings['add_role'] = false;
  73. if ( !isset($settings['remove_comments'] ) ) $settings['remove_comments'] = false;
  74. // If the "add role" option changed
  75. if ( $data['add_role'] !== $settings['add_role'] && current_user_can('update_plugins')) {
  76. // Add the 'css_js_designer' role
  77. if ( $data['add_role'] ) {
  78. CustomCSSandJS_Install::create_roles();
  79. }
  80. // Remove the 'css_js_designer' role
  81. if ( !$data['add_role'] ) {
  82. remove_role('css_js_designer');
  83. }
  84. flush_rewrite_rules();
  85. }
  86. update_option( 'ccj_settings', $data );
  87. } else {
  88. $data = $this->settings;
  89. }
  90. ?>
  91. <div class="wrap">
  92. <?php $this->config_page_header('editor'); ?>
  93. <form action="<?php echo admin_url('edit.php'); ?>?post_type=custom-css-js&page=custom-css-js-config" id="ccj_settings" method="post">
  94. <?php do_action( 'ccj_settings_form' ); ?>
  95. </form>
  96. </div>
  97. <?php
  98. }
  99. /**
  100. * Template for config page header
  101. */
  102. function config_page_header( $tab = 'editor' ) {
  103. $url = '?post_type=custom-css-js&page=custom-css-js-config';
  104. $active = array('editor' => '', 'general' => '', 'debug' => '');
  105. $active[$tab] = 'nav-tab-active';
  106. ?>
  107. <style type="text/css">
  108. .custom-css-js_page_custom-css-js-config h1 { margin-bottom: 40px; }
  109. .form-table { margin-left: 2%; width: 98%;}
  110. .form-table th { width: 500px; }
  111. </style>
  112. <h1><?php _e('Custom CSS & JS Settings'); ?></h1>
  113. <?php
  114. }
  115. /**
  116. * Add the defaults for the `General Settings` form
  117. */
  118. function general_extra_default( $defaults = array() ) {
  119. return array_merge( $defaults, array(
  120. 'ccj_htmlentities' => false,
  121. 'ccj_htmlentities2' => false,
  122. 'add_role' => false,
  123. 'remove_comments' => false,
  124. ) );
  125. }
  126. /**
  127. * Add the `General Settings` form values to the $_POST for the Settings page
  128. */
  129. function general_extra_save( $data = array() ) {
  130. $values = $this->general_extra_default();
  131. foreach($values as $_key => $_value ) {
  132. $values[$_key] = isset($_POST[$_key]) ? true : false;
  133. }
  134. return array_merge( $data, $values );
  135. }
  136. /**
  137. * Extra fields for the `General Settings` Form
  138. */
  139. function general_extra_form() {
  140. // Get the setting
  141. $settings = get_option('ccj_settings');
  142. $defaults = $this->general_extra_default();
  143. foreach( $defaults as $_key => $_value ) {
  144. if ( !isset($settings[$_key] ) ) {
  145. $settings[$_key] = $_value;
  146. }
  147. }
  148. if ( !get_role('css_js_designer') && $settings['add_role'] ) {
  149. $settings['add_role'] = false;
  150. update_option( 'ccj_settings', $settings );
  151. }
  152. if ( get_role('css_js_designer') && !$settings['add_role']) {
  153. $settings['add_role'] = true;
  154. update_option( 'ccj_settings', $settings );
  155. }
  156. $ccj_htmlentities_help = __('If you want to use an HTML entity in your code (for example '. htmlentities('&gt; or &quot;').'), but the editor keeps on changing them to its equivalent character (&gt; and &quot; for the previous example), then you might want to enable this option.', 'custom-css-js');
  157. $ccj_htmlentities2_help = __('If you use HTML tags in your code (for example '.htmlentities('<input> or <textarea>').') and you notice that they disappear and the editor looks weird, then you need to enable this option.', 'custom-css-js');
  158. $remove_comments_help = __('In your page\'s HTML there is a comment added before and after the internal CSS or JS in order to help you locate your custom code. Enable this option in order to remove that comment.', 'custom-css-js');
  159. ?>
  160. <h2><?php echo __('Editor Settings', 'custom-css-js'); ?></h2>
  161. <table class="form-table">
  162. <tr>
  163. <th scope="row"><label for="ccj_htmlentities"><?php _e('Keep the HTML entities, don\'t convert to its character', 'custom-css-js') ?> <span class="dashicons dashicons-editor-help" rel="tipsy" title="<?php echo $ccj_htmlentities_help; ?>"></span>
  164. </label></th>
  165. <td><input type="checkbox" name="ccj_htmlentities" id = "ccj_htmlentities" value="1" <?php checked($settings['ccj_htmlentities'], true); ?> />
  166. </td>
  167. </tr>
  168. <tr>
  169. <th scope="row"><label for="ccj_htmlentities2"><?php _e('Encode the HTML entities', 'custom-css-js') ?> <span class="dashicons dashicons-editor-help" rel="tipsy" title="<?php echo $ccj_htmlentities2_help; ?>"></span></label></th>
  170. <td><input type="checkbox" name="ccj_htmlentities2" id = "ccj_htmlentities2" value="1" <?php checked($settings['ccj_htmlentities2'], true); ?> />
  171. </td>
  172. </tr>
  173. </table>
  174. <?php if ( current_user_can('update_plugins') ) : ?>
  175. <?php $add_role_help = esc_html__('By default only the Administrator will be able to publish/edit/delete Custom Codes. By enabling this option there is also a "Web Designer" role created which can be assigned to a non-admin user in order to publish/edit/delete Custom Codes.', 'custom-css-js'); ?>
  176. <h2><?php echo __('General Settings', 'custom-css-js'); ?></h2>
  177. <table class="form-table">
  178. <tr>
  179. <th scope="row"><label for="add_role"><?php _e('Add the "Web Designer" role', 'custom-css-js') ?> <span class="dashicons dashicons-editor-help" rel="tipsy" title="<?php echo $add_role_help; ?>"></span></label></th>
  180. <td><input type="checkbox" name="add_role" id = "add_role" value="1" <?php checked($settings['add_role'], true); ?> />
  181. </td>
  182. </tr>
  183. </table>
  184. <?php endif; ?>
  185. <table class="form-table">
  186. <tr>
  187. <th scope="row"><label for="remove_comments"><?php _e('Remove the comments from HTML', 'custom-css-js') ?> <span class="dashicons dashicons-editor-help" rel="tipsy" title="<?php echo $remove_comments_help; ?>"></span></label></th>
  188. <td><input type="checkbox" name="remove_comments" id = "remove_comments" value="1" <?php checked($settings['remove_comments'], true); ?> />
  189. </td>
  190. </tr>
  191. </table>
  192. <table class="form-table">
  193. <tr>
  194. <th>&nbsp;</th>
  195. <td>
  196. <input type="submit" name="Submit" class="button-primary" value="<?php _e('Save'); ?>" />
  197. <?php wp_nonce_field('ccj_settings', 'ccj_settings-nonce', false); ?>
  198. </td>
  199. </tr>
  200. </table>
  201. <?php
  202. }
  203. }
  204. return new CustomCSSandJS_AdminConfig();