← All changes
|
frameworks/codestar-framework/classes/widget-options.class.php
+138
-138
2.3.4
→
2.4.0
View file →
| @@ -1,138 +1,138 @@ | ||
| 1 | -<?php if ( ! defined( 'ABSPATH' ) ) { die; } // Cannot access directly. | |
| 2 | -/** | |
| 3 | - * | |
| 4 | - * Widgets Class | |
| 5 | - * | |
| 6 | - * @since 1.0.0 | |
| 7 | - * @version 1.0.0 | |
| 8 | - * | |
| 9 | - */ | |
| 10 | -if ( ! class_exists( 'CSF_Widget' ) ) { | |
| 11 | - class CSF_Widget extends WP_Widget { | |
| 12 | - | |
| 13 | - // constans | |
| 14 | - public $unique = ''; | |
| 15 | - public $args = array( | |
| 16 | - 'title' => '', | |
| 17 | - 'classname' => '', | |
| 18 | - 'description' => '', | |
| 19 | - 'width' => '', | |
| 20 | - 'class' => '', | |
| 21 | - 'fields' => array(), | |
| 22 | - 'defaults' => array(), | |
| 23 | - ); | |
| 24 | - | |
| 25 | - public function __construct( $key, $params ) { | |
| 26 | - | |
| 27 | - $widget_ops = array(); | |
| 28 | - $control_ops = array(); | |
| 29 | - | |
| 30 | - $this->unique = $key; | |
| 31 | - $this->args = apply_filters( "csf_{$this->unique}_args", wp_parse_args( $params, $this->args ), $this ); | |
| 32 | - | |
| 33 | - // Set control options | |
| 34 | - if ( ! empty( $this->args['width'] ) ) { | |
| 35 | - $control_ops['width'] = esc_attr( $this->args['width'] ); | |
| 36 | - } | |
| 37 | - | |
| 38 | - // Set widget options | |
| 39 | - if ( ! empty( $this->args['description'] ) ) { | |
| 40 | - $widget_ops['description'] = esc_attr( $this->args['description'] ); | |
| 41 | - } | |
| 42 | - | |
| 43 | - if ( ! empty( $this->args['classname'] ) ) { | |
| 44 | - $widget_ops['classname'] = esc_attr( $this->args['classname'] ); | |
| 45 | - } | |
| 46 | - | |
| 47 | - // Set filters | |
| 48 | - $widget_ops = apply_filters( "csf_{$this->unique}_widget_ops", $widget_ops, $this ); | |
| 49 | - $control_ops = apply_filters( "csf_{$this->unique}_control_ops", $control_ops, $this ); | |
| 50 | - | |
| 51 | - parent::__construct( $this->unique, esc_attr( $this->args['title'] ), $widget_ops, $control_ops ); | |
| 52 | - | |
| 53 | - } | |
| 54 | - | |
| 55 | - // Register widget with WordPress | |
| 56 | - public static function instance( $key, $params = array() ) { | |
| 57 | - return new self( $key, $params ); | |
| 58 | - } | |
| 59 | - | |
| 60 | - // Front-end display of widget. | |
| 61 | - public function widget( $args, $instance ) { | |
| 62 | - call_user_func( $this->unique, $args, $instance ); | |
| 63 | - } | |
| 64 | - | |
| 65 | - // get default value | |
| 66 | - public function get_default( $field ) { | |
| 67 | - | |
| 68 | - $default = ( isset( $field['default'] ) ) ? $field['default'] : ''; | |
| 69 | - $default = ( isset( $this->args['defaults'][$field['id']] ) ) ? $this->args['defaults'][$field['id']] : $default; | |
| 70 | - | |
| 71 | - return $default; | |
| 72 | - | |
| 73 | - } | |
| 74 | - | |
| 75 | - // get widget value | |
| 76 | - public function get_widget_value( $instance, $field ) { | |
| 77 | - | |
| 78 | - $default = ( isset( $field['id'] ) ) ? $this->get_default( $field ) : ''; | |
| 79 | - $value = ( isset( $field['id'] ) && isset( $instance[$field['id']] ) ) ? $instance[$field['id']] : $default; | |
| 80 | - | |
| 81 | - return $value; | |
| 82 | - | |
| 83 | - } | |
| 84 | - | |
| 85 | - // Back-end widget form. | |
| 86 | - public function form( $instance ) { | |
| 87 | - | |
| 88 | - if ( ! empty( $this->args['fields'] ) ) { | |
| 89 | - | |
| 90 | - $class = ( $this->args['class'] ) ? ' '. $this->args['class'] : ''; | |
| 91 | - | |
| 92 | - echo '<div class="csf csf-widgets csf-fields'. esc_attr( $class ) .'">'; | |
| 93 | - | |
| 94 | - foreach ( $this->args['fields'] as $field ) { | |
| 95 | - | |
| 96 | - $field_unique = ''; | |
| 97 | - | |
| 98 | - if ( ! empty( $field['id'] ) ) { | |
| 99 | - | |
| 100 | - $field_unique = 'widget-' . $this->unique . '[' . $this->number . ']'; | |
| 101 | - | |
| 102 | - if ( $field['id'] === 'title' ) { | |
| 103 | - $field['attributes']['id'] = 'widget-'. $this->unique .'-'. $this->number .'-title'; | |
| 104 | - } | |
| 105 | - | |
| 106 | - $field['default'] = $this->get_default( $field ); | |
| 107 | - | |
| 108 | - } | |
| 109 | - | |
| 110 | - CSF::field( $field, $this->get_widget_value( $instance, $field ), $field_unique ); | |
| 111 | - | |
| 112 | - } | |
| 113 | - | |
| 114 | - echo '</div>'; | |
| 115 | - | |
| 116 | - } | |
| 117 | - | |
| 118 | - } | |
| 119 | - | |
| 120 | - // Sanitize widget form values as they are saved. | |
| 121 | - public function update( $new_instance, $old_instance ) { | |
| 122 | - | |
| 123 | - // auto sanitize | |
| 124 | - foreach ( $this->args['fields'] as $field ) { | |
| 125 | - if ( ! empty( $field['id'] ) && ( ! isset( $new_instance[$field['id']] ) || is_null( $new_instance[$field['id']] ) ) ) { | |
| 126 | - $new_instance[$field['id']] = ''; | |
| 127 | - } | |
| 128 | - } | |
| 129 | - | |
| 130 | - $new_instance = apply_filters( "csf_{$this->unique}_save", $new_instance, $this->args, $this ); | |
| 131 | - | |
| 132 | - do_action( "csf_{$this->unique}_save_before", $new_instance, $this->args, $this ); | |
| 133 | - | |
| 134 | - return $new_instance; | |
| 135 | - | |
| 136 | - } | |
| 137 | - } | |
| 138 | -} | |
| 1 | +<?php if ( ! defined( 'ABSPATH' ) ) { die; } // Cannot access directly. | |
| 2 | +/** | |
| 3 | + * | |
| 4 | + * Widgets Class | |
| 5 | + * | |
| 6 | + * @since 1.0.0 | |
| 7 | + * @version 1.0.0 | |
| 8 | + * | |
| 9 | + */ | |
| 10 | +if ( ! class_exists( 'CSF_Widget' ) ) { | |
| 11 | + class CSF_Widget extends WP_Widget { | |
| 12 | + | |
| 13 | + // constans | |
| 14 | + public $unique = ''; | |
| 15 | + public $args = array( | |
| 16 | + 'title' => '', | |
| 17 | + 'classname' => '', | |
| 18 | + 'description' => '', | |
| 19 | + 'width' => '', | |
| 20 | + 'class' => '', | |
| 21 | + 'fields' => array(), | |
| 22 | + 'defaults' => array(), | |
| 23 | + ); | |
| 24 | + | |
| 25 | + public function __construct( $key, $params ) { | |
| 26 | + | |
| 27 | + $widget_ops = array(); | |
| 28 | + $control_ops = array(); | |
| 29 | + | |
| 30 | + $this->unique = $key; | |
| 31 | + $this->args = apply_filters( "csf_{$this->unique}_args", wp_parse_args( $params, $this->args ), $this ); | |
| 32 | + | |
| 33 | + // Set control options | |
| 34 | + if ( ! empty( $this->args['width'] ) ) { | |
| 35 | + $control_ops['width'] = esc_attr( $this->args['width'] ); | |
| 36 | + } | |
| 37 | + | |
| 38 | + // Set widget options | |
| 39 | + if ( ! empty( $this->args['description'] ) ) { | |
| 40 | + $widget_ops['description'] = esc_attr( $this->args['description'] ); | |
| 41 | + } | |
| 42 | + | |
| 43 | + if ( ! empty( $this->args['classname'] ) ) { | |
| 44 | + $widget_ops['classname'] = esc_attr( $this->args['classname'] ); | |
| 45 | + } | |
| 46 | + | |
| 47 | + // Set filters | |
| 48 | + $widget_ops = apply_filters( "csf_{$this->unique}_widget_ops", $widget_ops, $this ); | |
| 49 | + $control_ops = apply_filters( "csf_{$this->unique}_control_ops", $control_ops, $this ); | |
| 50 | + | |
| 51 | + parent::__construct( $this->unique, esc_attr( $this->args['title'] ), $widget_ops, $control_ops ); | |
| 52 | + | |
| 53 | + } | |
| 54 | + | |
| 55 | + // Register widget with WordPress | |
| 56 | + public static function instance( $key, $params = array() ) { | |
| 57 | + return new self( $key, $params ); | |
| 58 | + } | |
| 59 | + | |
| 60 | + // Front-end display of widget. | |
| 61 | + public function widget( $args, $instance ) { | |
| 62 | + call_user_func( $this->unique, $args, $instance ); | |
| 63 | + } | |
| 64 | + | |
| 65 | + // get default value | |
| 66 | + public function get_default( $field ) { | |
| 67 | + | |
| 68 | + $default = ( isset( $field['default'] ) ) ? $field['default'] : ''; | |
| 69 | + $default = ( isset( $this->args['defaults'][$field['id']] ) ) ? $this->args['defaults'][$field['id']] : $default; | |
| 70 | + | |
| 71 | + return $default; | |
| 72 | + | |
| 73 | + } | |
| 74 | + | |
| 75 | + // get widget value | |
| 76 | + public function get_widget_value( $instance, $field ) { | |
| 77 | + | |
| 78 | + $default = ( isset( $field['id'] ) ) ? $this->get_default( $field ) : ''; | |
| 79 | + $value = ( isset( $field['id'] ) && isset( $instance[$field['id']] ) ) ? $instance[$field['id']] : $default; | |
| 80 | + | |
| 81 | + return $value; | |
| 82 | + | |
| 83 | + } | |
| 84 | + | |
| 85 | + // Back-end widget form. | |
| 86 | + public function form( $instance ) { | |
| 87 | + | |
| 88 | + if ( ! empty( $this->args['fields'] ) ) { | |
| 89 | + | |
| 90 | + $class = ( $this->args['class'] ) ? ' '. $this->args['class'] : ''; | |
| 91 | + | |
| 92 | + echo '<div class="csf csf-widgets csf-fields'. esc_attr( $class ) .'">'; | |
| 93 | + | |
| 94 | + foreach ( $this->args['fields'] as $field ) { | |
| 95 | + | |
| 96 | + $field_unique = ''; | |
| 97 | + | |
| 98 | + if ( ! empty( $field['id'] ) ) { | |
| 99 | + | |
| 100 | + $field_unique = 'widget-' . $this->unique . '[' . $this->number . ']'; | |
| 101 | + | |
| 102 | + if ( $field['id'] === 'title' ) { | |
| 103 | + $field['attributes']['id'] = 'widget-'. $this->unique .'-'. $this->number .'-title'; | |
| 104 | + } | |
| 105 | + | |
| 106 | + $field['default'] = $this->get_default( $field ); | |
| 107 | + | |
| 108 | + } | |
| 109 | + | |
| 110 | + CSF::field( $field, $this->get_widget_value( $instance, $field ), $field_unique ); | |
| 111 | + | |
| 112 | + } | |
| 113 | + | |
| 114 | + echo '</div>'; | |
| 115 | + | |
| 116 | + } | |
| 117 | + | |
| 118 | + } | |
| 119 | + | |
| 120 | + // Sanitize widget form values as they are saved. | |
| 121 | + public function update( $new_instance, $old_instance ) { | |
| 122 | + | |
| 123 | + // auto sanitize | |
| 124 | + foreach ( $this->args['fields'] as $field ) { | |
| 125 | + if ( ! empty( $field['id'] ) && ( ! isset( $new_instance[$field['id']] ) || is_null( $new_instance[$field['id']] ) ) ) { | |
| 126 | + $new_instance[$field['id']] = ''; | |
| 127 | + } | |
| 128 | + } | |
| 129 | + | |
| 130 | + $new_instance = apply_filters( "csf_{$this->unique}_save", $new_instance, $this->args, $this ); | |
| 131 | + | |
| 132 | + do_action( "csf_{$this->unique}_save_before", $new_instance, $this->args, $this ); | |
| 133 | + | |
| 134 | + return $new_instance; | |
| 135 | + | |
| 136 | + } | |
| 137 | + } | |
| 138 | +} | |