field = $field; $this->value = $value; $this->org_value = $value; $this->unique = $unique; $this->multilang = $this->element_multilang(); } public function element_value( $value = '' ) { $value = $this->value; if ( is_array( $this->multilang ) && is_array( $value ) ) { $current = $this->multilang['current']; if( isset( $value[$current] ) ) { $value = $value[$current]; } else if( $this->multilang['current'] == $this->multilang['default'] ) { $value = $this->value; } else { $value = ''; } } else if ( ! is_array( $this->multilang ) && isset( $this->value['multilang'] ) && is_array( $this->value ) ) { $value = array_values( $this->value ); $value = $value[0]; } else if ( is_array( $this->multilang ) && ! is_array( $value ) && ( $this->multilang['current'] != $this->multilang['default'] ) ) { $value = ''; } return $value; } public function element_name( $extra_name = '', $multilang = false ) { $element_id = ( isset( $this->field['id'] ) ) ? $this->field['id'] : ''; $extra_multilang = ( ! $multilang && is_array( $this->multilang ) ) ? '['. $this->multilang['current'] .']' : ''; return ( isset( $this->field['name'] ) ) ? $this->field['name'] . $extra_name : $this->unique .'['. $element_id .']'. $extra_multilang . $extra_name; } public function element_type() { $type = ( isset( $this->field['attributes']['type'] ) ) ? $this->field['attributes']['type'] : $this->field['type']; return $type; } public function element_class( $el_class = '' ) { $field_class = ( isset( $this->field['class'] ) ) ? ' ' . $this->field['class'] : ''; return ( $field_class || $el_class ) ? ' class="'. $el_class . $field_class .'"' : ''; } public function element_attributes( $el_attributes = array() ) { $attributes = ( isset( $this->field['attributes'] ) ) ? $this->field['attributes'] : array(); $element_id = ( isset( $this->field['id'] ) ) ? $this->field['id'] : ''; if( $el_attributes !== false ) { $sub_elemenet = ( isset( $this->field['sub'] ) ) ? 'sub-': ''; $el_attributes = ( is_string( $el_attributes ) || is_numeric( $el_attributes ) ) ? array('data-'. $sub_elemenet .'depend-id' => $element_id . '_' . $el_attributes ) : $el_attributes; $el_attributes = ( empty( $el_attributes ) && isset( $element_id ) ) ? array('data-'. $sub_elemenet .'depend-id' => $element_id ) : $el_attributes; } $attributes = wp_parse_args( $attributes, $el_attributes ); $atts = ''; if( ! empty( $attributes ) ) { foreach ( $attributes as $key => $value ) { if( $value === 'only-key' ) { $atts .= ' '. $key; } else { $atts .= ' '. $key . '="'. $value .'"'; } } } return $atts; } public function element_before() { return ( isset( $this->field['before'] ) ) ? $this->field['before'] : ''; } public function element_after() { $out = ( isset( $this->field['info'] ) ) ? '

'. $this->field['info'] .'

' : ''; $out .= ( isset( $this->field['after'] ) ) ? $this->field['after'] : ''; $out .= $this->element_after_multilang(); $out .= $this->element_get_error(); $out .= $this->element_help(); $out .= $this->element_debug(); return $out; } public function element_debug() { $out = ''; if( ( isset( $this->field['debug'] ) && $this->field['debug'] === true ) || ( defined( 'CS_OPTIONS_DEBUG' ) && CS_OPTIONS_DEBUG ) ) { $value = $this->element_value(); $out .= "
";
      $out .= "". __( 'CONFIG', 'cs-framework' ) .":";
      $out .= "\n";
      ob_start();
      var_export( $this->field );
      $out .= htmlspecialchars( ob_get_clean() );
      $out .= "\n\n";
      $out .= "". __( 'USAGE', 'cs-framework' ) .":";
      $out .= "\n";
      $out .= ( isset( $this->field['id'] ) ) ? "cs_get_option( '". $this->field['id'] ."' );" : '';

      if( ! empty( $value ) ) {
        $out .= "\n\n";
        $out .= "". __( 'VALUE', 'cs-framework' ) .":";
        $out .= "\n";
        ob_start();
        var_export( $value );
        $out .= htmlspecialchars( ob_get_clean() );
      }

      $out .= "
"; } return $out; } public function element_get_error() { global $cs_errors; $out = ''; if( ! empty( $cs_errors ) ) { foreach ( $cs_errors as $key => $value ) { if( isset( $this->field['id'] ) && $value['code'] == $this->field['id'] ) { $out .= '

'. $value['message'] .'

'; } } } return $out; } public function element_help() { return ( isset( $this->field['help'] ) ) ? '' : ''; } public function element_after_multilang() { $out = ''; if ( is_array( $this->multilang ) ) { $out .= ''; $out .= '

'. sprintf( __( 'You are editing language: ( %s )', 'cs-framework' ), $this->multilang['current'] ) .'

'; } return $out; } public function element_data( $type = '' ) { $options = array(); $query_args = ( isset( $this->field['query_args'] ) ) ? $this->field['query_args'] : array(); switch( $type ) { case 'pages': case 'page': $pages = get_pages( $query_args ); if ( ! is_wp_error( $pages ) && ! empty( $pages ) ) { foreach ( $pages as $page ) { $options[$page->ID] = $page->post_title; } } break; case 'posts': case 'post': $posts = get_posts( $query_args ); if ( ! is_wp_error( $posts ) && ! empty( $posts ) ) { foreach ( $posts as $post ) { $options[$post->ID] = $post->post_title; } } break; case 'categories': case 'category': $categories = get_categories( $query_args ); if ( ! is_wp_error( $categories ) && ! empty( $categories ) && ! isset( $categories['errors'] ) ) { foreach ( $categories as $category ) { $options[$category->term_id] = $category->name; } } break; case 'tags': case 'tag': $taxonomies = ( isset( $query_args['taxonomies'] ) ) ? $query_args['taxonomies'] : 'post_tag'; $tags = get_terms( $taxonomies, $query_args ); if ( ! is_wp_error( $tags ) && ! empty( $tags ) ) { foreach ( $tags as $tag ) { $options[$tag->term_id] = $tag->name; } } break; case 'custom': case 'callback': if( is_callable( $query_args['function'] ) ) { $options = call_user_func( $query_args['function'], $query_args['args'] ); } break; } return $options; } public function checked( $helper = '', $current = '', $type = 'checked', $echo = false ) { if ( is_array( $helper ) && in_array( $current, $helper ) ) { $result = ' '. $type .'="'. $type .'"'; } else if ( $helper == $current ) { $result = ' '. $type .'="'. $type .'"'; } else { $result = ''; } if ( $echo ) { echo $result; } return $result; } public function element_multilang() { return ( isset( $this->field['multilang'] ) ) ? cs_language_defaults() : false; } } // load all of fields cs_load_option_fields();