PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/codestar-framework/classes/options.class.php

https://gitlab.com/wowthemes/wmailchimp
PHP | 333 lines | 223 code | 99 blank | 11 comment | 64 complexity | 40c2ac3327e57410c7f4f0ac428b1c21 MD5 | raw file
  1. <?php if ( ! defined( 'ABSPATH' ) ) { die; } // Cannot access pages directly.
  2. /**
  3. *
  4. * Options Class
  5. *
  6. * @since 1.0.0
  7. * @version 1.0.0
  8. *
  9. */
  10. abstract class CSFramework_Options extends CSFramework_Abstract {
  11. public function __construct( $field = array(), $value = '', $unique = '' ) {
  12. $this->field = $field;
  13. $this->value = $value;
  14. $this->org_value = $value;
  15. $this->unique = $unique;
  16. $this->multilang = $this->element_multilang();
  17. }
  18. public function element_value( $value = '' ) {
  19. $value = $this->value;
  20. if ( is_array( $this->multilang ) && is_array( $value ) ) {
  21. $current = $this->multilang['current'];
  22. if( isset( $value[$current] ) ) {
  23. $value = $value[$current];
  24. } else if( $this->multilang['current'] == $this->multilang['default'] ) {
  25. $value = $this->value;
  26. } else {
  27. $value = '';
  28. }
  29. } else if ( ! is_array( $this->multilang ) && isset( $this->value['multilang'] ) && is_array( $this->value ) ) {
  30. $value = array_values( $this->value );
  31. $value = $value[0];
  32. } else if ( is_array( $this->multilang ) && ! is_array( $value ) && ( $this->multilang['current'] != $this->multilang['default'] ) ) {
  33. $value = '';
  34. }
  35. return $value;
  36. }
  37. public function element_name( $extra_name = '', $multilang = false ) {
  38. $element_id = ( isset( $this->field['id'] ) ) ? $this->field['id'] : '';
  39. $extra_multilang = ( ! $multilang && is_array( $this->multilang ) ) ? '['. $this->multilang['current'] .']' : '';
  40. return ( isset( $this->field['name'] ) ) ? $this->field['name'] . $extra_name : $this->unique .'['. $element_id .']'. $extra_multilang . $extra_name;
  41. }
  42. public function element_type() {
  43. $type = ( isset( $this->field['attributes']['type'] ) ) ? $this->field['attributes']['type'] : $this->field['type'];
  44. return $type;
  45. }
  46. public function element_class( $el_class = '' ) {
  47. $field_class = ( isset( $this->field['class'] ) ) ? ' ' . $this->field['class'] : '';
  48. return ( $field_class || $el_class ) ? ' class="'. $el_class . $field_class .'"' : '';
  49. }
  50. public function element_attributes( $el_attributes = array() ) {
  51. $attributes = ( isset( $this->field['attributes'] ) ) ? $this->field['attributes'] : array();
  52. $element_id = ( isset( $this->field['id'] ) ) ? $this->field['id'] : '';
  53. if( $el_attributes !== false ) {
  54. $sub_elemenet = ( isset( $this->field['sub'] ) ) ? 'sub-': '';
  55. $el_attributes = ( is_string( $el_attributes ) || is_numeric( $el_attributes ) ) ? array('data-'. $sub_elemenet .'depend-id' => $element_id . '_' . $el_attributes ) : $el_attributes;
  56. $el_attributes = ( empty( $el_attributes ) && isset( $element_id ) ) ? array('data-'. $sub_elemenet .'depend-id' => $element_id ) : $el_attributes;
  57. }
  58. $attributes = wp_parse_args( $attributes, $el_attributes );
  59. $atts = '';
  60. if( ! empty( $attributes ) ) {
  61. foreach ( $attributes as $key => $value ) {
  62. if( $value === 'only-key' ) {
  63. $atts .= ' '. $key;
  64. } else {
  65. $atts .= ' '. $key . '="'. $value .'"';
  66. }
  67. }
  68. }
  69. return $atts;
  70. }
  71. public function element_before() {
  72. return ( isset( $this->field['before'] ) ) ? $this->field['before'] : '';
  73. }
  74. public function element_after() {
  75. $out = ( isset( $this->field['info'] ) ) ? '<p class="cs-text-desc">'. $this->field['info'] .'</p>' : '';
  76. $out .= ( isset( $this->field['after'] ) ) ? $this->field['after'] : '';
  77. $out .= $this->element_after_multilang();
  78. $out .= $this->element_get_error();
  79. $out .= $this->element_help();
  80. $out .= $this->element_debug();
  81. return $out;
  82. }
  83. public function element_debug() {
  84. $out = '';
  85. if( ( isset( $this->field['debug'] ) && $this->field['debug'] === true ) || ( defined( 'CS_OPTIONS_DEBUG' ) && CS_OPTIONS_DEBUG ) ) {
  86. $value = $this->element_value();
  87. $out .= "<pre>";
  88. $out .= "<strong>". __( 'CONFIG', 'cs-framework' ) .":</strong>";
  89. $out .= "\n";
  90. ob_start();
  91. var_export( $this->field );
  92. $out .= htmlspecialchars( ob_get_clean() );
  93. $out .= "\n\n";
  94. $out .= "<strong>". __( 'USAGE', 'cs-framework' ) .":</strong>";
  95. $out .= "\n";
  96. $out .= ( isset( $this->field['id'] ) ) ? "cs_get_option( '". $this->field['id'] ."' );" : '';
  97. if( ! empty( $value ) ) {
  98. $out .= "\n\n";
  99. $out .= "<strong>". __( 'VALUE', 'cs-framework' ) .":</strong>";
  100. $out .= "\n";
  101. ob_start();
  102. var_export( $value );
  103. $out .= htmlspecialchars( ob_get_clean() );
  104. }
  105. $out .= "</pre>";
  106. }
  107. if( ( isset( $this->field['debug_light'] ) && $this->field['debug_light'] === true ) || ( defined( 'CS_OPTIONS_DEBUG_LIGHT' ) && CS_OPTIONS_DEBUG_LIGHT ) ) {
  108. $out .= "<pre>";
  109. $out .= "<strong>". __( 'USAGE', 'cs-framework' ) .":</strong>";
  110. $out .= "\n";
  111. $out .= ( isset( $this->field['id'] ) ) ? "cs_get_option( '". $this->field['id'] ."' );" : '';
  112. $out .= "\n";
  113. $out .= "<strong>". __( 'ID', 'cs-framework' ) .":</strong>";
  114. $out .= "\n";
  115. $out .= ( isset( $this->field['id'] ) ) ? $this->field['id'] : '';
  116. $out .= "</pre>";
  117. }
  118. return $out;
  119. }
  120. public function element_get_error() {
  121. global $cs_errors;
  122. $out = '';
  123. if( ! empty( $cs_errors ) ) {
  124. foreach ( $cs_errors as $key => $value ) {
  125. if( isset( $this->field['id'] ) && $value['code'] == $this->field['id'] ) {
  126. $out .= '<p class="cs-text-warning">'. $value['message'] .'</p>';
  127. }
  128. }
  129. }
  130. return $out;
  131. }
  132. public function element_help() {
  133. return ( isset( $this->field['help'] ) ) ? '<span class="cs-help" data-title="'. $this->field['help'] .'"><span class="fa fa-question-circle"></span></span>' : '';
  134. }
  135. public function element_after_multilang() {
  136. $out = '';
  137. if ( is_array( $this->multilang ) ) {
  138. $out .= '<fieldset class="hidden">';
  139. foreach ( $this->multilang['languages'] as $key => $val ) {
  140. // ignore current language for hidden element
  141. if( $key != $this->multilang['current'] ) {
  142. // set default value
  143. if( isset( $this->org_value[$key] ) ) {
  144. $value = $this->org_value[$key];
  145. } else if ( ! isset( $this->org_value[$key] ) && ( $key == $this->multilang['default'] ) ) {
  146. $value = $this->org_value;
  147. } else {
  148. $value = '';
  149. }
  150. $cache_field = $this->field;
  151. unset( $cache_field['multilang'] );
  152. $cache_field['name'] = $this->element_name( '['. $key .']', true );
  153. $class = 'CSFramework_Option_' . $this->field['type'];
  154. $element = new $class( $cache_field, $value, $this->unique );
  155. ob_start();
  156. $element->output();
  157. $out .= ob_get_clean();
  158. }
  159. }
  160. $out .= '<input type="hidden" name="'. $this->element_name( '[multilang]', true ) .'" value="true" />';
  161. $out .= '</fieldset>';
  162. $out .= '<p class="cs-text-desc">'. sprintf( __( 'You are editing language: ( <strong>%s</strong> )', 'cs-framework' ), $this->multilang['current'] ) .'</p>';
  163. }
  164. return $out;
  165. }
  166. public function element_data( $type = '' ) {
  167. $options = array();
  168. $query_args = ( isset( $this->field['query_args'] ) ) ? $this->field['query_args'] : array();
  169. switch( $type ) {
  170. case 'pages':
  171. case 'page':
  172. $pages = get_pages( $query_args );
  173. if ( ! is_wp_error( $pages ) && ! empty( $pages ) ) {
  174. foreach ( $pages as $page ) {
  175. $options[$page->ID] = $page->post_title;
  176. }
  177. }
  178. break;
  179. case 'posts':
  180. case 'post':
  181. $posts = get_posts( $query_args );
  182. if ( ! is_wp_error( $posts ) && ! empty( $posts ) ) {
  183. foreach ( $posts as $post ) {
  184. $options[$post->ID] = $post->post_title;
  185. }
  186. }
  187. break;
  188. case 'categories':
  189. case 'category':
  190. $categories = get_categories( $query_args );
  191. if ( ! is_wp_error( $categories ) && ! empty( $categories ) && ! isset( $categories['errors'] ) ) {
  192. foreach ( $categories as $category ) {
  193. $options[$category->term_id] = $category->name;
  194. }
  195. }
  196. break;
  197. case 'tags':
  198. case 'tag':
  199. $taxonomies = ( isset( $query_args['taxonomies'] ) ) ? $query_args['taxonomies'] : 'post_tag';
  200. $tags = get_terms( $taxonomies, $query_args );
  201. if ( ! is_wp_error( $tags ) && ! empty( $tags ) ) {
  202. foreach ( $tags as $tag ) {
  203. $options[$tag->term_id] = $tag->name;
  204. }
  205. }
  206. break;
  207. case 'custom':
  208. case 'callback':
  209. if( is_callable( $query_args['function'] ) ) {
  210. $options = call_user_func( $query_args['function'], $query_args['args'] );
  211. }
  212. break;
  213. }
  214. return $options;
  215. }
  216. public function checked( $helper = '', $current = '', $type = 'checked', $echo = false ) {
  217. if ( is_array( $helper ) && in_array( $current, $helper ) ) {
  218. $result = ' '. $type .'="'. $type .'"';
  219. } else if ( $helper == $current ) {
  220. $result = ' '. $type .'="'. $type .'"';
  221. } else {
  222. $result = '';
  223. }
  224. if ( $echo ) {
  225. echo $result;
  226. }
  227. return $result;
  228. }
  229. public function element_multilang() {
  230. return ( isset( $this->field['multilang'] ) ) ? cs_language_defaults() : false;
  231. }
  232. }
  233. // load all of fields
  234. cs_load_option_fields();