/wp-content/plugins/wp-editor/classes/WPEditorSetting.php

https://gitlab.com/code26/selah · PHP · 304 lines · 206 code · 49 blank · 49 comment · 21 complexity · 606eae005c300b9527fa393b4a42bc20 MD5 · raw file

  1. <?php
  2. class WPEditorSetting {
  3. /**
  4. * Get Settings
  5. *
  6. * Retrieves all plugin settings
  7. *
  8. * @since 1.0
  9. * @return array WP Editor settings
  10. */
  11. public static function get_settings() {
  12. $settings = get_option( 'wpe_settings' );
  13. if( empty( $settings ) ) {
  14. // Update old settings with new single option
  15. $general_settings = is_array( get_option( 'wpe_settings_general' ) ) ? get_option( 'wpe_settings_general' ) : array();
  16. $theme_editor_settings = is_array( get_option( 'wpe_settings_theme_editor' ) ) ? get_option( 'wpe_settings_theme_editor' ) : array();
  17. $plugin_editor_settings = is_array( get_option( 'wpe_settings_plugin_editor' ) ) ? get_option( 'wpe_settings_plugin_editor' ) : array();
  18. $post_editor_settings = is_array( get_option( 'wpe_settings_post_editor' ) ) ? get_option( 'wpe_settings_post_editor' ) : array();
  19. $license_settings = is_array( get_option( 'wpe_settings_license' ) ) ? get_option( 'wpe_settings_license' ) : array();
  20. $settings = array_merge( $general_settings, $theme_editor_settings, $plugin_editor_settings, $post_editor_settings, $license_settings );
  21. update_option( 'wpe_settings', $settings );
  22. }
  23. return apply_filters( 'wpe_get_settings', $settings );
  24. }
  25. /**
  26. * Add all settings sections and fields
  27. *
  28. * @since 1.0
  29. * @return void
  30. */
  31. public static function register_settings() {
  32. if ( false == get_option( 'wpe_settings' ) ) {
  33. add_option( 'wpe_settings' );
  34. }
  35. foreach ( self::get_registered_settings() as $tab => $sections ) {
  36. foreach ( $sections as $section => $settings) {
  37. // Check for backwards compatibility
  38. $section_tabs = self::get_settings_tab_sections( $tab );
  39. if ( ! is_array( $section_tabs ) || ! array_key_exists( $section, $section_tabs ) ) {
  40. $section = 'main';
  41. $settings = $sections;
  42. }
  43. add_settings_section(
  44. 'wpe_settings_' . $tab . '_' . $section,
  45. __return_null(),
  46. '__return_false',
  47. 'wpe_settings_' . $tab . '_' . $section
  48. );
  49. foreach ( $settings as $option ) {
  50. // For backwards compatibility
  51. if ( empty( $option['id'] ) ) {
  52. continue;
  53. }
  54. $name = isset( $option['name'] ) ? $option['name'] : '';
  55. add_settings_field(
  56. 'wpe_settings[' . $option['id'] . ']',
  57. $name,
  58. method_exists( __CLASS__, 'wpe_' . $option['type'] . '_callback' ) ? array( __CLASS__, 'wpe_' . $option['type'] . '_callback' ) : array( __CLASS__, 'missing_callback' ),
  59. 'wpe_settings_' . $tab . '_' . $section,
  60. 'wpe_settings_' . $tab . '_' . $section,
  61. array(
  62. 'section' => $section,
  63. 'id' => isset( $option['id'] ) ? $option['id'] : null,
  64. 'desc' => ! empty( $option['desc'] ) ? $option['desc'] : '',
  65. 'name' => isset( $option['name'] ) ? $option['name'] : null,
  66. 'size' => isset( $option['size'] ) ? $option['size'] : null,
  67. 'options' => isset( $option['options'] ) ? $option['options'] : '',
  68. 'std' => isset( $option['std'] ) ? $option['std'] : '',
  69. 'min' => isset( $option['min'] ) ? $option['min'] : null,
  70. 'max' => isset( $option['max'] ) ? $option['max'] : null,
  71. 'step' => isset( $option['step'] ) ? $option['step'] : null,
  72. 'chosen' => isset( $option['chosen'] ) ? $option['chosen'] : null,
  73. 'placeholder' => isset( $option['placeholder'] ) ? $option['placeholder'] : null,
  74. 'allow_blank' => isset( $option['allow_blank'] ) ? $option['allow_blank'] : true,
  75. 'readonly' => isset( $option['readonly'] ) ? $option['readonly'] : false,
  76. 'faux' => isset( $option['faux'] ) ? $option['faux'] : false,
  77. )
  78. );
  79. }
  80. }
  81. }
  82. // Creates our settings in the options table
  83. register_setting( 'wpe_settings', 'wpe_settings', 'wpe_settings_sanitize' );
  84. }
  85. /**
  86. * Retrieve the array of plugin settings
  87. *
  88. * @since 1.8
  89. * @return array
  90. */
  91. public static function get_registered_settings() {
  92. /**
  93. * 'Whitelisted' WP Editor settings, filters are provided for each settings
  94. * section to allow extensions and other plugins to add their own settings
  95. */
  96. $wpe_settings = array(
  97. /** General Settings */
  98. 'general' => apply_filters( 'wpe_settings_general',
  99. array(
  100. 'main' => array(
  101. 'allowed_extensions' => array(
  102. 'id' => 'allowed_extensions',
  103. 'name' => __( 'Allowed Extensions', 'wp-editor' ),
  104. 'desc' => __( 'Select the extensions you want to enable for the Theme and Plugin editors.', 'wp-editor' ),
  105. 'type' => 'multiselect',
  106. 'optons' => apply_filters( 'allowed_extensions', array(
  107. 'php' => '.php',
  108. 'js' => '.js',
  109. 'css' => '.css',
  110. 'scss' => '.scss',
  111. 'txt' => '.txt',
  112. 'htm' => '.htm',
  113. 'html' => '.html',
  114. 'jpg' => '.jpg',
  115. 'jpeg' => '.jpeg',
  116. 'png' => '.png',
  117. 'gif' => '.gif',
  118. 'sql' => '.sql',
  119. 'po' => '.po',
  120. 'pot' => '.pot',
  121. 'less' => '.less',
  122. 'xml' => '.xml'
  123. ) )
  124. ),
  125. ),
  126. 'codemirror' => array(
  127. 'test' => array(
  128. 'id' => 'test_codemirror',
  129. 'name' => '<h3>' . __( 'Test Codemirror', 'wp-editor' ) . '</h3>',
  130. 'desc' => '',
  131. 'type' => 'header',
  132. ),
  133. )
  134. )
  135. ),
  136. /** Theme Editor Settings */
  137. );
  138. return apply_filters( 'wpe_registered_settings', $wpe_settings );
  139. }
  140. /**
  141. * Retrieve settings tabs
  142. *
  143. * @since 1.8
  144. * @return array $tabs
  145. */
  146. public static function get_settings_tabs() {
  147. $settings = self::get_registered_settings();
  148. $tabs = array(
  149. 'general' => __( 'General', 'wp-editor' ),
  150. 'theme_editor' => __( 'Theme Editor', 'wp-editor' ),
  151. 'plugin_editor' => __( 'Plugin Editor', 'wp-editor' ),
  152. 'post_editor' => __( 'Page/Post Editor', 'wp-editor' ),
  153. 'license' => __( 'License', 'wp-editor' )
  154. );
  155. return apply_filters( 'wpe_settings_tabs', $tabs );
  156. }
  157. /**
  158. * Retrieve settings tabs
  159. *
  160. * @since 2.5
  161. * @return array $section
  162. */
  163. public static function get_settings_tab_sections( $tab = false ) {
  164. $tabs = false;
  165. $sections = WPEditorSetting::get_registered_settings_sections();
  166. if( $tab && ! empty( $sections[ $tab ] ) ) {
  167. $tabs = $sections[ $tab ];
  168. } else if ( $tab ) {
  169. $tabs = false;
  170. }
  171. return $tabs;
  172. }
  173. /**
  174. * Get the settings sections for each tab
  175. * Uses a static to avoid running the filters on every request to this function
  176. *
  177. * @since 2.5
  178. * @return array Array of tabs and sections
  179. */
  180. public static function get_registered_settings_sections() {
  181. static $sections = false;
  182. if ( false !== $sections ) {
  183. return $sections;
  184. }
  185. $sections = array(
  186. 'general' => apply_filters( 'wpe_settings_sections_general', array(
  187. 'main' => __( 'Main', 'wp-editor' ),
  188. 'codemirror' => __( 'Codemirror', 'wp-editor' ),
  189. ) ),
  190. 'theme_editor' => apply_filters( 'wpe_settings_sections_theme_editor', array(
  191. 'main' => __( 'Theme Editor Settings', 'wp-editor' ),
  192. ) ),
  193. 'plugin_editor' => apply_filters( 'wpe_settings_sections_plugin_editor', array(
  194. 'main' => __( 'Plugin Editor Settings', 'wp-editor' ),
  195. ) ),
  196. 'post_editor' => apply_filters( 'wpe_settings_sections_post_editor', array(
  197. 'main' => __( 'Page/Post Editor Settings', 'wp-editor' ),
  198. ) ),
  199. 'license' => apply_filters( 'wpe_settings_sections_license', array(
  200. 'main' => __( 'WP Editor License', 'wp-editor' ),
  201. ) ),
  202. );
  203. $sections = apply_filters( 'wpe_settings_sections', $sections );
  204. return $sections;
  205. }
  206. public static function missing_callback( $args ) {
  207. printf(
  208. __( 'The callback function used for the %s setting is missing.', 'easy-digital-downloads' ),
  209. '<strong>' . $args['id'] . '</strong>'
  210. );
  211. }
  212. public static function wpe_multiselect_callback( $args ) {
  213. global $wpe_options; //need to set this up
  214. ob_start(); ?>
  215. <?php echo ob_get_clean();
  216. }
  217. public static function set_value( $key, $value ) {
  218. global $wpdb;
  219. $settings_table = WPEditor::get_table_name( 'settings' );
  220. if ( ! empty( $key ) ) {
  221. $db_key = $wpdb->get_var( "SELECT `key` from $settings_table where `key`='$key'" );
  222. if ( $db_key ) {
  223. if ( ! empty( $value ) || $value !== 0 ) {
  224. $wpdb->update( $settings_table,
  225. array( 'key'=>$key, 'value'=>$value ),
  226. array( 'key'=>$key ),
  227. array( '%s', '%s' ),
  228. array( '%s' )
  229. );
  230. }
  231. else {
  232. $wpdb->query( "DELETE from $settings_table where `key`='$key'" );
  233. }
  234. }
  235. else {
  236. if ( !empty( $value ) || $value !== 0 ) {
  237. $wpdb->insert( $settings_table,
  238. array( 'key'=>$key, 'value'=>$value ),
  239. array( '%s', '%s' )
  240. );
  241. }
  242. }
  243. }
  244. }
  245. public static function get_value( $key, $entities=false ) {
  246. $value = false;
  247. global $wpdb;
  248. $settings_table = WPEditor::get_table_name( 'settings' );
  249. $value = $wpdb->get_var( "SELECT `value` from $settings_table where `key`='$key'" );
  250. if(!empty( $value ) && $entities ) {
  251. $value = htmlentities( $value );
  252. }
  253. return $value;
  254. }
  255. }