first commit

This commit is contained in:
2026-03-05 13:07:40 +01:00
commit 64ba0721ee
25709 changed files with 4691006 additions and 0 deletions

View File

@@ -0,0 +1,131 @@
<?php if ( ! defined( 'ABSPATH' ) ) { die; } // Cannot access directly.
/**
*
* Abstract Class
*
* @since 1.0.0
* @version 1.0.0
*
*/
if ( ! class_exists( 'CSF_Abstract' ) ) {
abstract class CSF_Abstract {
public $abstract = '';
public $output_css = '';
public function __construct() {
// Collect output css and typography
if ( ! empty( $this->args['output_css'] ) || ! empty( $this->args['enqueue_webfont'] ) ) {
add_action( 'wp_enqueue_scripts', array( &$this, 'collect_output_css_and_typography' ), 10 );
}
}
public function collect_output_css_and_typography() {
$this->recursive_output_css( $this->pre_fields );
}
public function recursive_output_css( $fields = array(), $combine_field = array() ) {
if ( ! empty( $fields ) ) {
foreach ( $fields as $field ) {
$field_id = ( ! empty( $field['id'] ) ) ? $field['id'] : '';
$field_type = ( ! empty( $field['type'] ) ) ? $field['type'] : '';
$field_output = ( ! empty( $field['output'] ) ) ? $field['output'] : '';
$field_check = ( $field_type === 'typography' || $field_output ) ? true : false;
$field_class = 'CSF_Field_' . $field_type;
if ( $field_type && $field_id ) {
if( $field_type === 'fieldset' ) {
if ( ! empty( $field['fields'] ) ) {
$this->recursive_output_css( $field['fields'], $field );
}
}
if( $field_type === 'accordion' ) {
if ( ! empty( $field['accordions'] ) ) {
foreach ( $field['accordions'] as $accordion ) {
$this->recursive_output_css( $accordion['fields'], $field );
}
}
}
if( $field_type === 'tabbed' ) {
if ( ! empty( $field['tabs'] ) ) {
foreach ( $field['tabs'] as $accordion ) {
$this->recursive_output_css( $accordion['fields'], $field );
}
}
}
if ( class_exists( $field_class ) ) {
if ( method_exists( $field_class, 'output' ) || method_exists( $field_class, 'enqueue_google_fonts' ) ) {
$field_value = '';
if ( $field_check && ( $this->abstract === 'options' || $this->abstract === 'customize' ) ) {
if( ! empty( $combine_field ) ) {
$field_value = ( isset( $this->options[$combine_field['id']][$field_id] ) ) ? $this->options[$combine_field['id']][$field_id] : '';
} else {
$field_value = ( isset( $this->options[$field_id] ) ) ? $this->options[$field_id] : '';
}
} else if ( $field_check && ( $this->abstract === 'metabox' && is_singular() || $this->abstract === 'taxonomy' && is_archive() ) ) {
if( ! empty( $combine_field ) ) {
$meta_value = $this->get_meta_value( $combine_field );
$field_value = ( isset( $meta_value[$field_id] ) ) ? $meta_value[$field_id] : '';
} else {
$meta_value = $this->get_meta_value( $field );
$field_value = ( isset( $meta_value ) ) ? $meta_value : '';
}
}
$instance = new $field_class( $field, $field_value, $this->unique, 'wp/enqueue', $this );
// typography enqueue and embed google web fonts
if ( $field_type === 'typography' && $this->args['enqueue_webfont'] && ! empty( $field_value['font-family'] ) ) {
$method = ( ! empty( $this->args['async_webfont'] ) ) ? 'async' : 'enqueue';
$instance->enqueue_google_fonts( $method );
}
// output css
if ( $field_output && $this->args['output_css'] ) {
CSF::$css .= $instance->output();
}
unset( $instance );
}
}
}
}
}
}
}
}

View File

@@ -0,0 +1,721 @@
<?php if ( ! defined( 'ABSPATH' ) ) { die; } // Cannot access directly.
/**
*
* Options Class
*
* @since 1.0.0
* @version 1.0.0
*
*/
if ( ! class_exists( 'CSF_Options' ) ) {
class CSF_Options extends CSF_Abstract {
// constans
public $unique = '';
public $notice = '';
public $abstract = 'options';
public $sections = array();
public $options = array();
public $errors = array();
public $pre_tabs = array();
public $pre_fields = array();
public $pre_sections = array();
public $args = array(
// framework title
'framework_title' => 'Codestar Framework <small>by Codestar</small>',
'framework_class' => '',
// menu settings
'menu_title' => '',
'menu_slug' => '',
'menu_type' => 'menu',
'menu_capability' => 'manage_options',
'menu_icon' => null,
'menu_position' => null,
'menu_hidden' => false,
'menu_parent' => '',
'sub_menu_title' => '',
// menu extras
'show_bar_menu' => true,
'show_sub_menu' => true,
'show_in_network' => true,
'show_in_customizer' => false,
'show_search' => true,
'show_reset_all' => true,
'show_reset_section' => true,
'show_footer' => true,
'show_all_options' => true,
'show_form_warning' => true,
'sticky_header' => true,
'save_defaults' => true,
'ajax_save' => true,
'form_action' => '',
// admin bar menu settings
'admin_bar_menu_icon' => '',
'admin_bar_menu_priority' => 50,
// footer
'footer_text' => '',
'footer_after' => '',
'footer_credit' => '',
// database model
'database' => '', // options, transient, theme_mod, network
'transient_time' => 0,
// contextual help
'contextual_help' => array(),
'contextual_help_sidebar' => '',
// typography options
'enqueue_webfont' => true,
'async_webfont' => false,
// others
'output_css' => true,
// theme
'nav' => 'normal',
'theme' => 'dark',
'class' => '',
// external default values
'defaults' => array(),
);
// run framework construct
public function __construct( $key, $params = array() ) {
$this->unique = $key;
$this->args = apply_filters( "csf_{$this->unique}_args", wp_parse_args( $params['args'], $this->args ), $this );
$this->sections = apply_filters( "csf_{$this->unique}_sections", $params['sections'], $this );
// run only is admin panel options, avoid performance loss
$this->pre_tabs = $this->pre_tabs( $this->sections );
$this->pre_fields = $this->pre_fields( $this->sections );
$this->pre_sections = $this->pre_sections( $this->sections );
$this->get_options();
$this->set_options();
$this->save_defaults();
add_action( 'admin_menu', array( &$this, 'add_admin_menu' ) );
add_action( 'admin_bar_menu', array( &$this, 'add_admin_bar_menu' ), $this->args['admin_bar_menu_priority'] );
add_action( 'wp_ajax_csf_'. $this->unique .'_ajax_save', array( &$this, 'ajax_save' ) );
if ( $this->args['database'] === 'network' && ! empty( $this->args['show_in_network'] ) ) {
add_action( 'network_admin_menu', array( &$this, 'add_admin_menu' ) );
}
// wp enqeueu for typography and output css
parent::__construct();
}
// instance
public static function instance( $key, $params = array() ) {
return new self( $key, $params );
}
public function pre_tabs( $sections ) {
$result = array();
$parents = array();
$count = 100;
foreach ( $sections as $key => $section ) {
if ( ! empty( $section['parent'] ) ) {
$section['priority'] = ( isset( $section['priority'] ) ) ? $section['priority'] : $count;
$parents[$section['parent']][] = $section;
unset( $sections[$key] );
}
$count++;
}
foreach ( $sections as $key => $section ) {
$section['priority'] = ( isset( $section['priority'] ) ) ? $section['priority'] : $count;
if ( ! empty( $section['id'] ) && ! empty( $parents[$section['id']] ) ) {
$section['subs'] = wp_list_sort( $parents[$section['id']], array( 'priority' => 'ASC' ), 'ASC', true );
}
$result[] = $section;
$count++;
}
return wp_list_sort( $result, array( 'priority' => 'ASC' ), 'ASC', true );
}
public function pre_fields( $sections ) {
$result = array();
foreach ( $sections as $key => $section ) {
if ( ! empty( $section['fields'] ) ) {
foreach ( $section['fields'] as $field ) {
$result[] = $field;
}
}
}
return $result;
}
public function pre_sections( $sections ) {
$result = array();
foreach ( $this->pre_tabs as $tab ) {
if ( ! empty( $tab['subs'] ) ) {
foreach ( $tab['subs'] as $sub ) {
$sub['ptitle'] = $tab['title'];
$result[] = $sub;
}
}
if ( empty( $tab['subs'] ) ) {
$result[] = $tab;
}
}
return $result;
}
// add admin bar menu
public function add_admin_bar_menu( $wp_admin_bar ) {
if( is_network_admin() && ( $this->args['database'] !== 'network' || $this->args['show_in_network'] !== true ) ) {
return;
}
if ( ! empty( $this->args['show_bar_menu'] ) && empty( $this->args['menu_hidden'] ) ) {
global $submenu;
$menu_slug = $this->args['menu_slug'];
$menu_icon = ( ! empty( $this->args['admin_bar_menu_icon'] ) ) ? '<span class="csf-ab-icon ab-icon '. esc_attr( $this->args['admin_bar_menu_icon'] ) .'"></span>' : '';
$wp_admin_bar->add_node( array(
'id' => $menu_slug,
'title' => $menu_icon . esc_attr( $this->args['menu_title'] ),
'href' => esc_url( ( is_network_admin() ) ? network_admin_url( 'admin.php?page='. $menu_slug ) : admin_url( 'admin.php?page='. $menu_slug ) ),
) );
if ( ! empty( $submenu[$menu_slug] ) ) {
foreach ( $submenu[$menu_slug] as $menu_key => $menu_value ) {
$wp_admin_bar->add_node( array(
'parent' => $menu_slug,
'id' => $menu_slug .'-'. $menu_key,
'title' => $menu_value[0],
'href' => esc_url( ( is_network_admin() ) ? network_admin_url( 'admin.php?page='. $menu_value[2] ) : admin_url( 'admin.php?page='. $menu_value[2] ) ),
) );
}
}
}
}
public function ajax_save() {
$result = $this->set_options( true );
if ( ! $result ) {
wp_send_json_error( array( 'error' => esc_html__( 'Error while saving the changes.', 'csf' ) ) );
} else {
wp_send_json_success( array( 'notice' => $this->notice, 'errors' => $this->errors ) );
}
}
// get default value
public function get_default( $field ) {
$default = ( isset( $field['default'] ) ) ? $field['default'] : '';
$default = ( isset( $this->args['defaults'][$field['id']] ) ) ? $this->args['defaults'][$field['id']] : $default;
return $default;
}
// save defaults and set new fields value to main options
public function save_defaults() {
$tmp_options = $this->options;
foreach ( $this->pre_fields as $field ) {
if ( ! empty( $field['id'] ) ) {
$this->options[$field['id']] = ( isset( $this->options[$field['id']] ) ) ? $this->options[$field['id']] : $this->get_default( $field );
}
}
if ( $this->args['save_defaults'] && empty( $tmp_options ) ) {
$this->save_options( $this->options );
}
}
// set options
public function set_options( $ajax = false ) {
// XSS ok.
// No worries, This "POST" requests is sanitizing in the below foreach. see #L337 - #L341
$response = ( $ajax && ! empty( $_POST['data'] ) ) ? json_decode( wp_unslash( trim( $_POST['data'] ) ), true ) : $_POST;
// Set variables.
$data = array();
$noncekey = 'csf_options_nonce'. $this->unique;
$nonce = ( ! empty( $response[$noncekey] ) ) ? $response[$noncekey] : '';
$options = ( ! empty( $response[$this->unique] ) ) ? $response[$this->unique] : array();
$transient = ( ! empty( $response['csf_transient'] ) ) ? $response['csf_transient'] : array();
if ( wp_verify_nonce( $nonce, 'csf_options_nonce' ) ) {
$importing = false;
$section_id = ( ! empty( $transient['section'] ) ) ? $transient['section'] : '';
if ( ! $ajax && ! empty( $response[ 'csf_import_data' ] ) ) {
// XSS ok.
// No worries, This "POST" requests is sanitizing in the below foreach. see #L337 - #L341
$import_data = json_decode( wp_unslash( trim( $response[ 'csf_import_data' ] ) ), true );
$options = ( is_array( $import_data ) && ! empty( $import_data ) ) ? $import_data : array();
$importing = true;
$this->notice = esc_html__( 'Settings successfully imported.', 'csf' );
}
if ( ! empty( $transient['reset'] ) ) {
foreach ( $this->pre_fields as $field ) {
if ( ! empty( $field['id'] ) ) {
$data[$field['id']] = $this->get_default( $field );
}
}
$this->notice = esc_html__( 'Default settings restored.', 'csf' );
} else if ( ! empty( $transient['reset_section'] ) && ! empty( $section_id ) ) {
if ( ! empty( $this->pre_sections[$section_id-1]['fields'] ) ) {
foreach ( $this->pre_sections[$section_id-1]['fields'] as $field ) {
if ( ! empty( $field['id'] ) ) {
$data[$field['id']] = $this->get_default( $field );
}
}
}
$data = wp_parse_args( $data, $this->options );
$this->notice = esc_html__( 'Default settings restored.', 'csf' );
} else {
// sanitize and validate
foreach ( $this->pre_fields as $field ) {
if ( ! empty( $field['id'] ) ) {
$field_id = $field['id'];
$field_value = isset( $options[$field_id] ) ? $options[$field_id] : '';
// Ajax and Importing doing wp_unslash already.
if ( ! $ajax && ! $importing ) {
$field_value = wp_unslash( $field_value );
}
// Sanitize "post" request of field.
if ( ! isset( $field['sanitize'] ) ) {
if( is_array( $field_value ) ) {
$data[$field_id] = wp_kses_post_deep( $field_value );
} else {
$data[$field_id] = wp_kses_post( $field_value );
}
} else if( isset( $field['sanitize'] ) && is_callable( $field['sanitize'] ) ) {
$data[$field_id] = call_user_func( $field['sanitize'], $field_value );
} else {
$data[$field_id] = $field_value;
}
// Validate "post" request of field.
if ( isset( $field['validate'] ) && is_callable( $field['validate'] ) ) {
$has_validated = call_user_func( $field['validate'], $field_value );
if ( ! empty( $has_validated ) ) {
$data[$field_id] = ( isset( $this->options[$field_id] ) ) ? $this->options[$field_id] : '';
$this->errors[$field_id] = $has_validated;
}
}
}
}
}
$data = apply_filters( "csf_{$this->unique}_save", $data, $this );
do_action( "csf_{$this->unique}_save_before", $data, $this );
$this->options = $data;
$this->save_options( $data );
do_action( "csf_{$this->unique}_save_after", $data, $this );
if ( empty( $this->notice ) ) {
$this->notice = esc_html__( 'Settings saved.', 'csf' );
}
return true;
}
return false;
}
// save options database
public function save_options( $data ) {
if ( $this->args['database'] === 'transient' ) {
set_transient( $this->unique, $data, $this->args['transient_time'] );
} else if ( $this->args['database'] === 'theme_mod' ) {
set_theme_mod( $this->unique, $data );
} else if ( $this->args['database'] === 'network' ) {
update_site_option( $this->unique, $data );
} else {
update_option( $this->unique, $data );
}
do_action( "csf_{$this->unique}_saved", $data, $this );
}
// get options from database
public function get_options() {
if ( $this->args['database'] === 'transient' ) {
$this->options = get_transient( $this->unique );
} else if ( $this->args['database'] === 'theme_mod' ) {
$this->options = get_theme_mod( $this->unique );
} else if ( $this->args['database'] === 'network' ) {
$this->options = get_site_option( $this->unique );
} else {
$this->options = get_option( $this->unique );
}
if ( empty( $this->options ) ) {
$this->options = array();
}
return $this->options;
}
// admin menu
public function add_admin_menu() {
extract( $this->args );
if ( $menu_type === 'submenu' ) {
$menu_page = call_user_func( 'add_submenu_page', $menu_parent, esc_attr( $menu_title ), esc_attr( $menu_title ), $menu_capability, $menu_slug, array( &$this, 'add_options_html' ) );
} else {
$menu_page = call_user_func( 'add_menu_page', esc_attr( $menu_title ), esc_attr( $menu_title ), $menu_capability, $menu_slug, array( &$this, 'add_options_html' ), $menu_icon, $menu_position );
if ( ! empty( $sub_menu_title ) ) {
call_user_func( 'add_submenu_page', $menu_slug, esc_attr( $sub_menu_title ), esc_attr( $sub_menu_title ), $menu_capability, $menu_slug, array( &$this, 'add_options_html' ) );
}
if ( ! empty( $this->args['show_sub_menu'] ) && count( $this->pre_tabs ) > 1 ) {
// create submenus
foreach ( $this->pre_tabs as $section ) {
call_user_func( 'add_submenu_page', $menu_slug, esc_attr( $section['title'] ), esc_attr( $section['title'] ), $menu_capability, $menu_slug .'#tab='. sanitize_title( $section['title'] ), '__return_null' );
}
remove_submenu_page( $menu_slug, $menu_slug );
}
if ( ! empty( $menu_hidden ) ) {
remove_menu_page( $menu_slug );
}
}
add_action( 'load-'. $menu_page, array( &$this, 'add_page_on_load' ) );
}
public function add_page_on_load() {
if ( ! empty( $this->args['contextual_help'] ) ) {
$screen = get_current_screen();
foreach ( $this->args['contextual_help'] as $tab ) {
$screen->add_help_tab( $tab );
}
if ( ! empty( $this->args['contextual_help_sidebar'] ) ) {
$screen->set_help_sidebar( $this->args['contextual_help_sidebar'] );
}
}
add_filter( 'admin_footer_text', array( &$this, 'add_admin_footer_text' ) );
}
public function add_admin_footer_text() {
$default = 'Thank you for creating with <a href="http://codestarframework.com/" target="_blank">Codestar Framework</a>';
echo ( ! empty( $this->args['footer_credit'] ) ) ? $this->args['footer_credit'] : $default;
}
public function error_check( $sections, $err = '' ) {
if ( ! $this->args['ajax_save'] ) {
if ( ! empty( $sections['fields'] ) ) {
foreach ( $sections['fields'] as $field ) {
if ( ! empty( $field['id'] ) ) {
if ( array_key_exists( $field['id'], $this->errors ) ) {
$err = '<span class="csf-label-error">!</span>';
}
}
}
}
if ( ! empty( $sections['subs'] ) ) {
foreach ( $sections['subs'] as $sub ) {
$err = $this->error_check( $sub, $err );
}
}
if ( ! empty( $sections['id'] ) && array_key_exists( $sections['id'], $this->errors ) ) {
$err = $this->errors[$sections['id']];
}
}
return $err;
}
// option page html output
public function add_options_html() {
$has_nav = ( count( $this->pre_tabs ) > 1 ) ? true : false;
$show_all = ( ! $has_nav ) ? ' csf-show-all' : '';
$ajax_class = ( $this->args['ajax_save'] ) ? ' csf-save-ajax' : '';
$sticky_class = ( $this->args['sticky_header'] ) ? ' csf-sticky-header' : '';
$wrapper_class = ( $this->args['framework_class'] ) ? ' '. $this->args['framework_class'] : '';
$theme = ( $this->args['theme'] ) ? ' csf-theme-'. $this->args['theme'] : '';
$class = ( $this->args['class'] ) ? ' '. $this->args['class'] : '';
$nav_type = ( $this->args['nav'] === 'inline' ) ? 'inline' : 'normal';
$form_action = ( $this->args['form_action'] ) ? $this->args['form_action'] : '';
do_action( 'csf_options_before' );
echo '<div class="csf csf-options'. esc_attr( $theme . $class . $wrapper_class ) .'" data-slug="'. esc_attr( $this->args['menu_slug'] ) .'" data-unique="'. esc_attr( $this->unique ) .'">';
echo '<div class="csf-container">';
echo '<form method="post" action="'. esc_attr( $form_action ) .'" enctype="multipart/form-data" id="csf-form" autocomplete="off" novalidate="novalidate">';
echo '<input type="hidden" class="csf-section-id" name="csf_transient[section]" value="1">';
wp_nonce_field( 'csf_options_nonce', 'csf_options_nonce'. $this->unique );
echo '<div class="csf-header'. esc_attr( $sticky_class ) .'">';
echo '<div class="csf-header-inner">';
echo '<div class="csf-header-left">';
echo '<h1>'. $this->args['framework_title'] .'</h1>';
echo '</div>';
echo '<div class="csf-header-right">';
$notice_class = ( ! empty( $this->notice ) ) ? 'csf-form-show' : '';
$notice_text = ( ! empty( $this->notice ) ) ? $this->notice : '';
echo '<div class="csf-form-result csf-form-success '. esc_attr( $notice_class ) .'">'. $notice_text .'</div>';
echo ( $this->args['show_form_warning'] ) ? '<div class="csf-form-result csf-form-warning">'. esc_html__( 'You have unsaved changes, save your changes!', 'csf' ) .'</div>' : '';
echo ( $has_nav && $this->args['show_all_options'] ) ? '<div class="csf-expand-all" title="'. esc_html__( 'show all settings', 'csf' ) .'"><i class="fas fa-outdent"></i></div>' : '';
echo ( $this->args['show_search'] ) ? '<div class="csf-search"><input type="text" name="csf-search" placeholder="'. esc_html__( 'Search...', 'csf' ) .'" autocomplete="off" /></div>' : '';
echo '<div class="csf-buttons">';
echo '<input type="submit" name="'. esc_attr( $this->unique ) .'[_nonce][save]" class="button button-primary csf-top-save csf-save'. esc_attr( $ajax_class ) .'" value="'. esc_html__( 'Save', 'csf' ) .'" data-save="'. esc_html__( 'Saving...', 'csf' ) .'">';
echo ( $this->args['show_reset_section'] ) ? '<input type="submit" name="csf_transient[reset_section]" class="button button-secondary csf-reset-section csf-confirm" value="'. esc_html__( 'Reset Section', 'csf' ) .'" data-confirm="'. esc_html__( 'Are you sure to reset this section options?', 'csf' ) .'">' : '';
echo ( $this->args['show_reset_all'] ) ? '<input type="submit" name="csf_transient[reset]" class="button csf-warning-primary csf-reset-all csf-confirm" value="'. ( ( $this->args['show_reset_section'] ) ? esc_html__( 'Reset All', 'csf' ) : esc_html__( 'Reset', 'csf' ) ) .'" data-confirm="'. esc_html__( 'Are you sure you want to reset all settings to default values?', 'csf' ) .'">' : '';
echo '</div>';
echo '</div>';
echo '<div class="clear"></div>';
echo '</div>';
echo '</div>';
echo '<div class="csf-wrapper'. esc_attr( $show_all ) .'">';
if ( $has_nav ) {
echo '<div class="csf-nav csf-nav-'. esc_attr( $nav_type ) .' csf-nav-options">';
echo '<ul>';
foreach ( $this->pre_tabs as $tab ) {
$tab_id = sanitize_title( $tab['title'] );
$tab_error = $this->error_check( $tab );
$tab_icon = ( ! empty( $tab['icon'] ) ) ? '<i class="csf-tab-icon '. esc_attr( $tab['icon'] ) .'"></i>' : '';
if ( ! empty( $tab['subs'] ) ) {
echo '<li class="csf-tab-item">';
echo '<a href="#tab='. esc_attr( $tab_id ) .'" data-tab-id="'. esc_attr( $tab_id ) .'" class="csf-arrow">'. $tab_icon . $tab['title'] . $tab_error .'</a>';
echo '<ul>';
foreach ( $tab['subs'] as $sub ) {
$sub_id = $tab_id .'/'. sanitize_title( $sub['title'] );
$sub_error = $this->error_check( $sub );
$sub_icon = ( ! empty( $sub['icon'] ) ) ? '<i class="csf-tab-icon '. esc_attr( $sub['icon'] ) .'"></i>' : '';
echo '<li><a href="#tab='. esc_attr( $sub_id ) .'" data-tab-id="'. esc_attr( $sub_id ) .'">'. $sub_icon . $sub['title'] . $sub_error .'</a></li>';
}
echo '</ul>';
echo '</li>';
} else {
echo '<li class="csf-tab-item"><a href="#tab='. esc_attr( $tab_id ) .'" data-tab-id="'. esc_attr( $tab_id ) .'">'. $tab_icon . $tab['title'] . $tab_error .'</a></li>';
}
}
echo '</ul>';
echo '</div>';
}
echo '<div class="csf-content">';
echo '<div class="csf-sections">';
foreach ( $this->pre_sections as $section ) {
$section_onload = ( ! $has_nav ) ? ' csf-onload' : '';
$section_class = ( ! empty( $section['class'] ) ) ? ' '. $section['class'] : '';
$section_icon = ( ! empty( $section['icon'] ) ) ? '<i class="csf-section-icon '. esc_attr( $section['icon'] ) .'"></i>' : '';
$section_title = ( ! empty( $section['title'] ) ) ? $section['title'] : '';
$section_parent = ( ! empty( $section['ptitle'] ) ) ? sanitize_title( $section['ptitle'] ) .'/' : '';
$section_slug = ( ! empty( $section['title'] ) ) ? sanitize_title( $section_title ) : '';
echo '<div class="csf-section hidden'. esc_attr( $section_onload . $section_class ) .'" data-section-id="'. esc_attr( $section_parent . $section_slug ) .'">';
echo ( $has_nav ) ? '<div class="csf-section-title"><h3>'. $section_icon . $section_title .'</h3></div>' : '';
echo ( ! empty( $section['description'] ) ) ? '<div class="csf-field csf-section-description">'. $section['description'] .'</div>' : '';
if ( ! empty( $section['fields'] ) ) {
foreach ( $section['fields'] as $field ) {
$is_field_error = $this->error_check( $field );
if ( ! empty( $is_field_error ) ) {
$field['_error'] = $is_field_error;
}
if ( ! empty( $field['id'] ) ) {
$field['default'] = $this->get_default( $field );
}
$value = ( ! empty( $field['id'] ) && isset( $this->options[$field['id']] ) ) ? $this->options[$field['id']] : '';
CSF::field( $field, $value, $this->unique, 'options' );
}
} else {
echo '<div class="csf-no-option">'. esc_html__( 'No data available.', 'csf' ) .'</div>';
}
echo '</div>';
}
echo '</div>';
echo '<div class="clear"></div>';
echo '</div>';
echo ( $has_nav && $nav_type === 'normal' ) ? '<div class="csf-nav-background"></div>' : '';
echo '</div>';
if ( ! empty( $this->args['show_footer'] ) ) {
echo '<div class="csf-footer">';
echo '<div class="csf-buttons">';
echo '<input type="submit" name="csf_transient[save]" class="button button-primary csf-save'. esc_attr( $ajax_class ) .'" value="'. esc_html__( 'Save', 'csf' ) .'" data-save="'. esc_html__( 'Saving...', 'csf' ) .'">';
echo ( $this->args['show_reset_section'] ) ? '<input type="submit" name="csf_transient[reset_section]" class="button button-secondary csf-reset-section csf-confirm" value="'. esc_html__( 'Reset Section', 'csf' ) .'" data-confirm="'. esc_html__( 'Are you sure to reset this section options?', 'csf' ) .'">' : '';
echo ( $this->args['show_reset_all'] ) ? '<input type="submit" name="csf_transient[reset]" class="button csf-warning-primary csf-reset-all csf-confirm" value="'. ( ( $this->args['show_reset_section'] ) ? esc_html__( 'Reset All', 'csf' ) : esc_html__( 'Reset', 'csf' ) ) .'" data-confirm="'. esc_html__( 'Are you sure you want to reset all settings to default values?', 'csf' ) .'">' : '';
echo '</div>';
echo ( ! empty( $this->args['footer_text'] ) ) ? '<div class="csf-copyright">'. $this->args['footer_text'] .'</div>' : '';
echo '<div class="clear"></div>';
echo '</div>';
}
echo '</form>';
echo '</div>';
echo '<div class="clear"></div>';
echo ( ! empty( $this->args['footer_after'] ) ) ? $this->args['footer_after'] : '';
echo '</div>';
do_action( 'csf_options_after' );
}
}
}

View File

@@ -0,0 +1,346 @@
<?php if ( ! defined( 'ABSPATH' ) ) { die; } // Cannot access directly.
/**
*
* Comment Metabox Class
*
* @since 1.0.0
* @version 1.0.0
*
*/
if ( ! class_exists( 'CSF_Comment_Metabox' ) ) {
class CSF_Comment_Metabox extends CSF_Abstract{
// constans
public $unique = '';
public $abstract = 'comment_metabox';
public $pre_fields = array();
public $sections = array();
public $args = array(
'title' => '',
'data_type' => 'serialize',
'priority' => 'default',
'show_reset' => false,
'show_restore' => false,
'nav' => 'normal',
'theme' => 'dark',
'class' => '',
'defaults' => array(),
);
// run comment metabox construct
public function __construct( $key, $params = array() ) {
$this->unique = $key;
$this->args = apply_filters( "csf_{$this->unique}_args", wp_parse_args( $params['args'], $this->args ), $this );
$this->sections = apply_filters( "csf_{$this->unique}_sections", $params['sections'], $this );
$this->pre_fields = $this->pre_fields( $this->sections );
add_action( 'add_meta_boxes_comment', array( &$this, 'add_comment_meta_box' ) );
add_action( 'edit_comment', array( &$this, 'save_comment_meta_box' ) );
if ( ! empty( $this->args['class'] ) ) {
add_filter( 'postbox_classes_comment_'. $this->unique, array( &$this, 'add_comment_metabox_classes' ) );
}
}
// instance
public static function instance( $key, $params = array() ) {
return new self( $key, $params );
}
public function pre_fields( $sections ) {
$result = array();
foreach ( $sections as $key => $section ) {
if ( ! empty( $section['fields'] ) ) {
foreach ( $section['fields'] as $field ) {
$result[] = $field;
}
}
}
return $result;
}
public function add_comment_metabox_classes( $classes ) {
if ( ! empty( $this->args['class'] ) ) {
$classes[] = $this->args['class'];
}
return $classes;
}
// add comment metabox
public function add_comment_meta_box( $post_type ) {
add_meta_box( $this->unique, $this->args['title'], array( &$this, 'add_comment_meta_box_content' ), 'comment', 'normal', $this->args['priority'], $this->args );
}
// get default value
public function get_default( $field ) {
$default = ( isset( $field['default'] ) ) ? $field['default'] : '';
$default = ( isset( $this->args['defaults'][$field['id']] ) ) ? $this->args['defaults'][$field['id']] : $default;
return $default;
}
// get meta value
public function get_meta_value( $comment_id, $field ) {
$value = null;
if ( ! empty( $comment_id ) && ! empty( $field['id'] ) ) {
if ( $this->args['data_type'] !== 'serialize' ) {
$meta = get_comment_meta( $comment_id, $field['id'] );
$value = ( isset( $meta[0] ) ) ? $meta[0] : null;
} else {
$meta = get_comment_meta( $comment_id, $this->unique, true );
$value = ( isset( $meta[$field['id']] ) ) ? $meta[$field['id']] : null;
}
}
$default = ( isset( $field['id'] ) ) ? $this->get_default( $field ) : '';
$value = ( isset( $value ) ) ? $value : $default;
return $value;
}
// add comment metabox content
public function add_comment_meta_box_content( $comment, $callback ) {
$has_nav = ( count( $this->sections ) > 1 ) ? true : false;
$show_all = ( ! $has_nav ) ? ' csf-show-all' : '';
$errors = ( is_object ( $comment ) ) ? get_comment_meta( $comment->comment_ID, '_csf_errors_'. $this->unique, true ) : array();
$errors = ( ! empty( $errors ) ) ? $errors : array();
$theme = ( $this->args['theme'] ) ? ' csf-theme-'. $this->args['theme'] : '';
$nav_type = ( $this->args['nav'] === 'inline' ) ? 'inline' : 'normal';
if ( is_object( $comment ) && ! empty( $errors ) ) {
delete_comment_meta( $comment->comment_ID, '_csf_errors_'. $this->unique );
}
wp_nonce_field( 'csf_comment_metabox_nonce', 'csf_comment_metabox_nonce'. $this->unique );
echo '<div class="csf csf-comment-metabox'. esc_attr( $theme ) .'">';
echo '<div class="csf-wrapper'. esc_attr( $show_all ) .'">';
if ( $has_nav ) {
echo '<div class="csf-nav csf-nav-'. esc_attr( $nav_type ) .' csf-nav-metabox">';
echo '<ul>';
$tab_key = 1;
foreach ( $this->sections as $section ) {
$tab_icon = ( ! empty( $section['icon'] ) ) ? '<i class="csf-tab-icon '. esc_attr( $section['icon'] ) .'"></i>' : '';
$tab_error = ( ! empty( $errors['sections'][$tab_key] ) ) ? '<i class="csf-label-error csf-error">!</i>' : '';
echo '<li><a href="#">'. $tab_icon . $section['title'] . $tab_error .'</a></li>';
$tab_key++;
}
echo '</ul>';
echo '</div>';
}
echo '<div class="csf-content">';
echo '<div class="csf-sections">';
$section_key = 1;
foreach ( $this->sections as $section ) {
$section_onload = ( ! $has_nav ) ? ' csf-onload' : '';
$section_class = ( ! empty( $section['class'] ) ) ? ' '. $section['class'] : '';
$section_title = ( ! empty( $section['title'] ) ) ? $section['title'] : '';
$section_icon = ( ! empty( $section['icon'] ) ) ? '<i class="csf-section-icon '. esc_attr( $section['icon'] ) .'"></i>' : '';
echo '<div class="csf-section hidden'. esc_attr( $section_onload . $section_class ) .'">';
echo ( $section_title || $section_icon ) ? '<div class="csf-section-title"><h3>'. $section_icon . $section_title .'</h3></div>' : '';
if ( ! empty( $section['fields'] ) ) {
foreach ( $section['fields'] as $field ) {
if ( ! empty( $field['id'] ) && ! empty( $errors['fields'][$field['id']] ) ) {
$field['_error'] = $errors['fields'][$field['id']];
}
if ( ! empty( $field['id'] ) ) {
$field['default'] = $this->get_default( $field );
}
CSF::field( $field, $this->get_meta_value( $comment->comment_ID, $field ), $this->unique, 'comment_metabox' );
}
} else {
echo '<div class="csf-no-option">'. esc_html__( 'No data available.', 'csf' ) .'</div>';
}
echo '</div>';
$section_key++;
}
echo '</div>';
if ( ! empty( $this->args['show_restore'] ) || ! empty( $this->args['show_reset'] ) ) {
echo '<div class="csf-sections-reset">';
echo '<label>';
echo '<input type="checkbox" name="'. esc_attr( $this->unique ) .'[_reset]" />';
echo '<span class="button csf-button-reset">'. esc_html__( 'Reset', 'csf' ) .'</span>';
echo '<span class="button csf-button-cancel">'. sprintf( '<small>( %s )</small> %s', esc_html__( 'update post', 'csf' ), esc_html__( 'Cancel', 'csf' ) ) .'</span>';
echo '</label>';
echo '</div>';
}
echo '</div>';
echo ( $has_nav && $nav_type === 'normal' ) ? '<div class="csf-nav-background"></div>' : '';
echo '<div class="clear"></div>';
echo '</div>';
echo '</div>';
}
// save comment metabox
public function save_comment_meta_box( $comment_id ) {
$count = 1;
$data = array();
$errors = array();
$noncekey = 'csf_comment_metabox_nonce'. $this->unique;
$nonce = ( ! empty( $_POST[ $noncekey ] ) ) ? sanitize_text_field( wp_unslash( $_POST[ $noncekey ] ) ) : '';
if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || ! wp_verify_nonce( $nonce, 'csf_comment_metabox_nonce' ) ) {
return $comment_id;
}
// XSS ok.
// No worries, This "POST" requests is sanitizing in the below foreach.
$request = ( ! empty( $_POST[ $this->unique ] ) ) ? $_POST[ $this->unique ] : array();
if ( ! empty( $request ) ) {
foreach ( $this->sections as $section ) {
if ( ! empty( $section['fields'] ) ) {
foreach ( $section['fields'] as $field ) {
if ( ! empty( $field['id'] ) ) {
$field_id = $field['id'];
$field_value = isset( $request[$field_id] ) ? $request[$field_id] : '';
// Sanitize "post" request of field.
if ( ! isset( $field['sanitize'] ) ) {
if( is_array( $field_value ) ) {
$data[$field_id] = wp_kses_post_deep( $field_value );
} else {
$data[$field_id] = wp_kses_post( $field_value );
}
} else if( isset( $field['sanitize'] ) && is_callable( $field['sanitize'] ) ) {
$data[$field_id] = call_user_func( $field['sanitize'], $field_value );
} else {
$data[$field_id] = $field_value;
}
// Validate "post" request of field.
if ( isset( $field['validate'] ) && is_callable( $field['validate'] ) ) {
$has_validated = call_user_func( $field['validate'], $field_value );
if ( ! empty( $has_validated ) ) {
$errors['sections'][$count] = true;
$errors['fields'][$field_id] = $has_validated;
$data[$field_id] = $this->get_meta_value( $comment_id, $field );
}
}
}
}
}
$count++;
}
}
$data = apply_filters( "csf_{$this->unique}_save", $data, $comment_id, $this );
do_action( "csf_{$this->unique}_save_before", $data, $comment_id, $this );
if ( empty( $data ) || ! empty( $request['_reset'] ) ) {
if ( $this->args['data_type'] !== 'serialize' ) {
foreach ( $data as $key => $value ) {
delete_comment_meta( $comment_id, $key );
}
} else {
delete_comment_meta( $comment_id, $this->unique );
}
} else {
if ( $this->args['data_type'] !== 'serialize' ) {
foreach ( $data as $key => $value ) {
update_comment_meta( $comment_id, $key, $value );
}
} else {
update_comment_meta( $comment_id, $this->unique, $data );
}
if ( ! empty( $errors ) ) {
update_comment_meta( $comment_id, '_csf_errors_'. $this->unique, $errors );
}
}
do_action( "csf_{$this->unique}_saved", $data, $comment_id, $this );
do_action( "csf_{$this->unique}_save_after", $data, $comment_id, $this );
}
}
}

View File

@@ -0,0 +1,282 @@
<?php if ( ! defined( 'ABSPATH' ) ) { die; } // Cannot access directly.
/**
*
* Customize Options Class
*
* @since 1.0.0
* @version 1.0.0
*
*/
if ( ! class_exists( 'CSF_Customize_Options' ) ) {
class CSF_Customize_Options extends CSF_Abstract {
// constans
public $unique = '';
public $abstract = 'customize';
public $options = array();
public $sections = array();
public $pre_fields = array();
public $pre_tabs = array();
public $priority = 10;
public $args = array(
'database' => 'option',
'transport' => 'refresh',
'capability' => 'manage_options',
'save_defaults' => true,
'enqueue_webfont' => true,
'async_webfont' => false,
'output_css' => true,
'defaults' => array()
);
// run customize construct
public function __construct( $key, $params ) {
$this->unique = $key;
$this->args = apply_filters( "csf_{$this->unique}_args", wp_parse_args( $params['args'], $this->args ), $this );
$this->sections = apply_filters( "csf_{$this->unique}_sections", $params['sections'], $this );
$this->pre_fields = $this->pre_fields( $this->sections );
$this->get_options();
$this->save_defaults();
add_action( 'customize_register', array( &$this, 'add_customize_options' ) );
add_action( 'customize_save_after', array( &$this, 'add_customize_save_after' ) );
// Get options for enqueue actions
if ( is_customize_preview() ) {
add_action( 'wp_enqueue_scripts', array( &$this, 'get_options' ) );
}
// wp enqeueu for typography and output css
parent::__construct();
}
// instance
public static function instance( $key, $params = array() ) {
return new self( $key, $params );
}
public function add_customize_save_after( $wp_customize ) {
do_action( "csf_{$this->unique}_save_before", $this->get_options(), $this, $wp_customize );
do_action( "csf_{$this->unique}_saved", $this->get_options(), $this, $wp_customize );
do_action( "csf_{$this->unique}_save_after", $this->get_options(), $this, $wp_customize );
}
// get default value
public function get_default( $field ) {
$default = ( isset( $field['default'] ) ) ? $field['default'] : '';
$default = ( isset( $this->args['defaults'][$field['id']] ) ) ? $this->args['defaults'][$field['id']] : $default;
return $default;
}
// get option
public function get_options() {
if ( $this->args['database'] === 'theme_mod' ) {
$this->options = get_theme_mod( $this->unique, array() );
} else {
$this->options = get_option( $this->unique, array() );
}
if ( empty( $this->options ) ) {
$this->options = array();
}
return $this->options;
}
// save defaults and set new fields value to main options
public function save_defaults() {
$tmp_options = $this->options;
if ( ! empty( $this->pre_fields ) ) {
foreach ( $this->pre_fields as $field ) {
if ( ! empty( $field['id'] ) ) {
$this->options[$field['id']] = ( isset( $this->options[$field['id']] ) ) ? $this->options[$field['id']] : $this->get_default( $field );
}
}
}
if ( $this->args['save_defaults'] && empty( $this->args['show_in_customizer'] ) && empty( $tmp_options ) ) {
if ( $this->args['database'] === 'theme_mod' ) {
set_theme_mod( $this->unique, $this->options );
} else {
update_option( $this->unique, $this->options );
}
}
}
public function pre_fields( $sections ) {
$result = array();
foreach ( $sections as $key => $section ) {
if ( ! empty( $section['fields'] ) ) {
foreach ( $section['fields'] as $field ) {
$result[] = $field;
}
}
}
return $result;
}
public function pre_tabs( $sections ) {
$result = array();
$parents = array();
foreach ( $sections as $key => $section ) {
if ( ! empty( $section['parent'] ) ) {
$parents[$section['parent']][] = $section;
unset( $sections[$key] );
}
}
foreach ( $sections as $key => $section ) {
if ( ! empty( $section['id'] ) && ! empty( $parents[$section['id']] ) ) {
$section['subs'] = $parents[$section['id']];
}
$result[] = $section;
}
return $result;
}
public function add_customize_options( $wp_customize ) {
if ( ! class_exists( 'WP_Customize_Panel_CSF' ) ) {
CSF::include_plugin_file( 'functions/customize.php' );
}
if ( ! empty( $this->sections ) ) {
$sections = $this->pre_tabs( $this->sections );
foreach ( $sections as $section ) {
if ( ! empty( $section['subs'] ) ) {
$panel_id = ( isset( $section['id'] ) ) ? $section['id'] : $this->unique .'-panel-'. $this->priority;
$wp_customize->add_panel( new WP_Customize_Panel_CSF( $wp_customize, $panel_id, array(
'title' => ( isset( $section['title'] ) ) ? $section['title'] : null,
'description' => ( isset( $section['description'] ) ) ? $section['description'] : null,
'priority' => ( isset( $section['priority'] ) ) ? $section['priority'] : null,
) ) );
$this->priority++;
foreach ( $section['subs'] as $sub_section ) {
$section_id = ( isset( $sub_section['id'] ) ) ? $sub_section['id'] : $this->unique .'-section-'. $this->priority;
$this->add_section( $wp_customize, $section_id, $sub_section, $panel_id );
$this->priority++;
}
} else {
$section_id = ( isset( $section['id'] ) ) ? $section['id'] : $this->unique .'-section-'. $this->priority;
$this->add_section( $wp_customize, $section_id, $section, false );
$this->priority++;
}
}
}
}
// add customize section
public function add_section( $wp_customize, $section_id, $section_args, $panel_id ) {
if ( ! empty( $section_args['assign'] ) ) {
$section_id = $section_args['assign'];
} else {
$wp_customize->add_section( new WP_Customize_Section_CSF( $wp_customize, $section_id, array(
'title' => ( isset( $section_args['title'] ) ) ? $section_args['title'] : '',
'description' => ( isset( $section_args['description'] ) ) ? $section_args['description'] : '',
'priority' => ( isset( $section_args['priority'] ) ) ? $section_args['priority'] : '',
'panel' => ( $panel_id ) ? $panel_id : '',
) ) );
}
if ( ! empty( $section_args['fields'] ) ) {
$field_key = 1;
foreach ( $section_args['fields'] as $field ) {
if ( isset( $field['id'] ) ) {
$field['default'] = $this->get_default( $field );
}
$field_id = ( isset( $field['id'] ) ) ? $field['id'] : '_nonce-'. $section_id .'-'. $field_key;
$setting_args = ( isset( $field['setting_args'] ) ) ? $field['setting_args'] : array();
$control_args = ( isset( $field['control_args'] ) ) ? $field['control_args'] : array();
$field_transport = ( isset( $field['transport'] ) ) ? $field['transport'] : $this->args['transport'];
$field_sanitize = ( isset( $field['sanitize'] ) ) ? $field['sanitize'] : '';
$field_validate = ( isset( $field['validate'] ) ) ? $field['validate'] : '';
$field_default = ( isset( $field['default'] ) ) ? $field['default'] : '';
$field_customize = ( isset( $field['customize'] ) && ! isset( $field['transport'] ) ) ? true : false;
$has_selective = ( isset( $field['selective_refresh'] ) && isset( $wp_customize->selective_refresh ) ) ? true : false;
$setting_id = $this->unique .'['. $field_id .']';
$transport = ( $has_selective || $field_customize ) ? 'postMessage' : $field_transport;
$wp_customize->add_setting( $setting_id,
wp_parse_args( $setting_args, array(
'default' => $field_default,
'type' => $this->args['database'],
'capability' => $this->args['capability'],
'transport' => $transport,
'sanitize_callback' => $field_sanitize,
'validate_callback' => $field_validate
) )
);
$wp_customize->add_control( new WP_Customize_Control_CSF( $wp_customize, $setting_id,
wp_parse_args( $control_args, array(
'unique' => $this->unique,
'field' => $field,
'section' => $section_id,
'settings' => $setting_id
) )
) );
if ( $has_selective ) {
$wp_customize->selective_refresh->add_partial( $setting_id, $field['selective_refresh'] );
}
$field_key++;
}
}
}
}
}

View File

@@ -0,0 +1,405 @@
<?php if ( ! defined( 'ABSPATH' ) ) { die; } // Cannot access directly.
/**
*
* Fields Class
*
* @since 1.0.0
* @version 1.0.0
*
*/
if ( ! class_exists( 'CSF_Fields' ) ) {
abstract class CSF_Fields extends CSF_Abstract {
public function __construct( $field = array(), $value = '', $unique = '', $where = '', $parent = '' ) {
$this->field = $field;
$this->value = $value;
$this->unique = $unique;
$this->where = $where;
$this->parent = $parent;
}
public function field_name( $nested_name = '' ) {
$field_id = ( ! empty( $this->field['id'] ) ) ? $this->field['id'] : '';
$unique_id = ( ! empty( $this->unique ) ) ? $this->unique .'['. $field_id .']' : $field_id;
$field_name = ( ! empty( $this->field['name'] ) ) ? $this->field['name'] : $unique_id;
$tag_prefix = ( ! empty( $this->field['tag_prefix'] ) ) ? $this->field['tag_prefix'] : '';
if ( ! empty( $tag_prefix ) ) {
$nested_name = str_replace( '[', '['. $tag_prefix, $nested_name );
}
return $field_name . $nested_name;
}
public function field_attributes( $custom_atts = array() ) {
$field_id = ( ! empty( $this->field['id'] ) ) ? $this->field['id'] : '';
$attributes = ( ! empty( $this->field['attributes'] ) ) ? $this->field['attributes'] : array();
if ( ! empty( $field_id ) && empty( $attributes['data-depend-id'] ) ) {
$attributes['data-depend-id'] = $field_id;
}
if ( ! empty( $this->field['placeholder'] ) ) {
$attributes['placeholder'] = $this->field['placeholder'];
}
$attributes = wp_parse_args( $attributes, $custom_atts );
$atts = '';
if ( ! empty( $attributes ) ) {
foreach ( $attributes as $key => $value ) {
if ( $value === 'only-key' ) {
$atts .= ' '. esc_attr( $key );
} else {
$atts .= ' '. esc_attr( $key ) . '="'. esc_attr( $value ) .'"';
}
}
}
return $atts;
}
public function field_before() {
return ( ! empty( $this->field['before'] ) ) ? '<div class="csf-before-text">'. $this->field['before'] .'</div>' : '';
}
public function field_after() {
$output = ( ! empty( $this->field['after'] ) ) ? '<div class="csf-after-text">'. $this->field['after'] .'</div>' : '';
$output .= ( ! empty( $this->field['desc'] ) ) ? '<div class="clear"></div><div class="csf-desc-text">'. $this->field['desc'] .'</div>' : '';
$output .= ( ! empty( $this->field['help'] ) ) ? '<div class="csf-help"><span class="csf-help-text">'. $this->field['help'] .'</span><i class="fas fa-question-circle"></i></div>' : '';
$output .= ( ! empty( $this->field['_error'] ) ) ? '<div class="csf-error-text">'. $this->field['_error'] .'</div>' : '';
return $output;
}
public static function field_data( $type = '', $term = false, $query_args = array() ) {
$options = array();
$array_search = false;
// sanitize type name
if ( in_array( $type, array( 'page', 'pages' ) ) ) {
$option = 'page';
} else if ( in_array( $type, array( 'post', 'posts' ) ) ) {
$option = 'post';
} else if ( in_array( $type, array( 'category', 'categories' ) ) ) {
$option = 'category';
} else if ( in_array( $type, array( 'tag', 'tags' ) ) ) {
$option = 'post_tag';
} else if ( in_array( $type, array( 'menu', 'menus' ) ) ) {
$option = 'nav_menu';
} else {
$option = '';
}
// switch type
switch( $type ) {
case 'page':
case 'pages':
case 'post':
case 'posts':
// term query required for ajax select
if ( ! empty( $term ) ) {
$query = new WP_Query( wp_parse_args( $query_args, array(
's' => $term,
'post_type' => $option,
'post_status' => 'publish',
'posts_per_page' => 25,
) ) );
} else {
$query = new WP_Query( wp_parse_args( $query_args, array(
'post_type' => $option,
'post_status' => 'publish',
) ) );
}
if ( ! is_wp_error( $query ) && ! empty( $query->posts ) ) {
foreach ( $query->posts as $item ) {
$options[$item->ID] = $item->post_title;
}
}
break;
case 'category':
case 'categories':
case 'tag':
case 'tags':
case 'menu':
case 'menus':
if ( ! empty( $term ) ) {
$query = new WP_Term_Query( wp_parse_args( $query_args, array(
'search' => $term,
'taxonomy' => $option,
'hide_empty' => false,
'number' => 25,
) ) );
} else {
$query = new WP_Term_Query( wp_parse_args( $query_args, array(
'taxonomy' => $option,
'hide_empty' => false,
) ) );
}
if ( ! is_wp_error( $query ) && ! empty( $query->terms ) ) {
foreach ( $query->terms as $item ) {
$options[$item->term_id] = $item->name;
}
}
break;
case 'user':
case 'users':
if ( ! empty( $term ) ) {
$query = new WP_User_Query( array(
'search' => '*'. $term .'*',
'number' => 25,
'orderby' => 'title',
'order' => 'ASC',
'fields' => array( 'display_name', 'ID' )
) );
} else {
$query = new WP_User_Query( array( 'fields' => array( 'display_name', 'ID' ) ) );
}
if ( ! is_wp_error( $query ) && ! empty( $query->get_results() ) ) {
foreach ( $query->get_results() as $item ) {
$options[$item->ID] = $item->display_name;
}
}
break;
case 'sidebar':
case 'sidebars':
global $wp_registered_sidebars;
if ( ! empty( $wp_registered_sidebars ) ) {
foreach ( $wp_registered_sidebars as $sidebar ) {
$options[$sidebar['id']] = $sidebar['name'];
}
}
$array_search = true;
break;
case 'role':
case 'roles':
global $wp_roles;
if ( ! empty( $wp_roles ) ) {
if ( ! empty( $wp_roles->roles ) ) {
foreach ( $wp_roles->roles as $role_key => $role_value ) {
$options[$role_key] = $role_value['name'];
}
}
}
$array_search = true;
break;
case 'post_type':
case 'post_types':
$post_types = get_post_types( array( 'show_in_nav_menus' => true ), 'objects' );
if ( ! is_wp_error( $post_types ) && ! empty( $post_types ) ) {
foreach ( $post_types as $post_type ) {
$options[$post_type->name] = $post_type->labels->name;
}
}
$array_search = true;
break;
case 'location':
case 'locations':
$nav_menus = get_registered_nav_menus();
if ( ! is_wp_error( $nav_menus ) && ! empty( $nav_menus ) ) {
foreach ( $nav_menus as $nav_menu_key => $nav_menu_name ) {
$options[$nav_menu_key] = $nav_menu_name;
}
}
$array_search = true;
break;
default:
if ( is_callable( $type ) ) {
if ( ! empty( $term ) ) {
$options = call_user_func( $type, $query_args );
} else {
$options = call_user_func( $type, $term, $query_args );
}
}
break;
}
// Array search by "term"
if ( ! empty( $term ) && ! empty( $options ) && ! empty( $array_search ) ) {
$options = preg_grep( '/'. $term .'/i', $options );
}
// Make multidimensional array for ajax search
if ( ! empty( $term ) && ! empty( $options ) ) {
$arr = array();
foreach ( $options as $option_key => $option_value ) {
$arr[] = array( 'value' => $option_key, 'text' => $option_value );
}
$options = $arr;
}
return $options;
}
public function field_wp_query_data_title( $type, $values ) {
$options = array();
if ( ! empty( $values ) && is_array( $values ) ) {
foreach ( $values as $value ) {
$options[$value] = ucfirst( $value );
switch( $type ) {
case 'post':
case 'posts':
case 'page':
case 'pages':
$title = get_the_title( $value );
if ( ! is_wp_error( $title ) && ! empty( $title ) ) {
$options[$value] = $title;
}
break;
case 'category':
case 'categories':
case 'tag':
case 'tags':
case 'menu':
case 'menus':
$term = get_term( $value );
if ( ! is_wp_error( $term ) && ! empty( $term ) ) {
$options[$value] = $term->name;
}
break;
case 'user':
case 'users':
$user = get_user_by( 'id', $value );
if ( ! is_wp_error( $user ) && ! empty( $user ) ) {
$options[$value] = $user->display_name;
}
break;
case 'sidebar':
case 'sidebars':
global $wp_registered_sidebars;
if ( ! empty( $wp_registered_sidebars[$value] ) ) {
$options[$value] = $wp_registered_sidebars[$value]['name'];
}
break;
case 'role':
case 'roles':
global $wp_roles;
if ( ! empty( $wp_roles ) && ! empty( $wp_roles->roles ) && ! empty( $wp_roles->roles[$value] ) ) {
$options[$value] = $wp_roles->roles[$value]['name'];
}
break;
case 'post_type':
case 'post_types':
$post_types = get_post_types( array( 'show_in_nav_menus' => true ) );
if ( ! is_wp_error( $post_types ) && ! empty( $post_types ) && ! empty( $post_types[$value] ) ) {
$options[$value] = ucfirst( $value );
}
break;
case 'location':
case 'locations':
$nav_menus = get_registered_nav_menus();
if ( ! is_wp_error( $nav_menus ) && ! empty( $nav_menus ) && ! empty( $nav_menus[$value] ) ) {
$options[$value] = $nav_menus[$value];
}
break;
default:
if ( is_callable( $type .'_title' ) ) {
$options[$value] = call_user_func( $type .'_title', $value );
}
break;
}
}
}
return $options;
}
}
}

View File

@@ -0,0 +1,424 @@
<?php if ( ! defined( 'ABSPATH' ) ) { die; } // Cannot access directly.
/**
*
* Metabox Class
*
* @since 1.0.0
* @version 1.0.0
*
*/
if ( ! class_exists( 'CSF_Metabox' ) ) {
class CSF_Metabox extends CSF_Abstract{
// constans
public $unique = '';
public $abstract = 'metabox';
public $pre_fields = array();
public $sections = array();
public $post_type = array();
public $args = array(
'title' => '',
'post_type' => 'post',
'data_type' => 'serialize',
'context' => 'advanced',
'priority' => 'default',
'exclude_post_types' => array(),
'page_templates' => '',
'post_formats' => '',
'show_reset' => false,
'show_restore' => false,
'enqueue_webfont' => true,
'async_webfont' => false,
'output_css' => true,
'nav' => 'normal',
'theme' => 'dark',
'class' => '',
'defaults' => array(),
);
// run metabox construct
public function __construct( $key, $params = array() ) {
$this->unique = $key;
$this->args = apply_filters( "csf_{$this->unique}_args", wp_parse_args( $params['args'], $this->args ), $this );
$this->sections = apply_filters( "csf_{$this->unique}_sections", $params['sections'], $this );
$this->post_type = ( is_array( $this->args['post_type'] ) ) ? $this->args['post_type'] : array_filter( (array) $this->args['post_type'] );
$this->post_formats = ( is_array( $this->args['post_formats'] ) ) ? $this->args['post_formats'] : array_filter( (array) $this->args['post_formats'] );
$this->page_templates = ( is_array( $this->args['page_templates'] ) ) ? $this->args['page_templates'] : array_filter( (array) $this->args['page_templates'] );
$this->pre_fields = $this->pre_fields( $this->sections );
add_action( 'add_meta_boxes', array( &$this, 'add_meta_box' ) );
add_action( 'save_post', array( &$this, 'save_meta_box' ) );
add_action( 'edit_attachment', array( &$this, 'save_meta_box' ) );
if ( ! empty( $this->page_templates ) || ! empty( $this->post_formats ) || ! empty( $this->args['class'] ) ) {
foreach ( $this->post_type as $post_type ) {
add_filter( 'postbox_classes_'. $post_type .'_'. $this->unique, array( &$this, 'add_metabox_classes' ) );
}
}
// wp enqeueu for typography and output css
parent::__construct();
}
// instance
public static function instance( $key, $params = array() ) {
return new self( $key, $params );
}
public function pre_fields( $sections ) {
$result = array();
foreach ( $sections as $key => $section ) {
if ( ! empty( $section['fields'] ) ) {
foreach ( $section['fields'] as $field ) {
$result[] = $field;
}
}
}
return $result;
}
public function add_metabox_classes( $classes ) {
global $post;
if ( ! empty( $this->post_formats ) ) {
$saved_post_format = ( is_object( $post ) ) ? get_post_format( $post ) : false;
$saved_post_format = ( ! empty( $saved_post_format ) ) ? $saved_post_format : 'default';
$classes[] = 'csf-post-formats';
// Sanitize post format for standard to default
if ( ( $key = array_search( 'standard', $this->post_formats ) ) !== false ) {
$this->post_formats[$key] = 'default';
}
foreach ( $this->post_formats as $format ) {
$classes[] = 'csf-post-format-'. $format;
}
if ( ! in_array( $saved_post_format, $this->post_formats ) ) {
$classes[] = 'csf-metabox-hide';
} else {
$classes[] = 'csf-metabox-show';
}
}
if ( ! empty( $this->page_templates ) ) {
$saved_template = ( is_object( $post ) && ! empty( $post->page_template ) ) ? $post->page_template : 'default';
$classes[] = 'csf-page-templates';
foreach ( $this->page_templates as $template ) {
$classes[] = 'csf-page-'. preg_replace( '/[^a-zA-Z0-9]+/', '-', strtolower( $template ) );
}
if ( ! in_array( $saved_template, $this->page_templates ) ) {
$classes[] = 'csf-metabox-hide';
} else {
$classes[] = 'csf-metabox-show';
}
}
if ( ! empty( $this->args['class'] ) ) {
$classes[] = $this->args['class'];
}
return $classes;
}
// add metabox
public function add_meta_box( $post_type ) {
if ( ! in_array( $post_type, $this->args['exclude_post_types'] ) ) {
add_meta_box( $this->unique, $this->args['title'], array( &$this, 'add_meta_box_content' ), $this->post_type, $this->args['context'], $this->args['priority'], $this->args );
}
}
// get default value
public function get_default( $field ) {
$default = ( isset( $field['default'] ) ) ? $field['default'] : '';
$default = ( isset( $this->args['defaults'][$field['id']] ) ) ? $this->args['defaults'][$field['id']] : $default;
return $default;
}
// get meta value
public function get_meta_value( $field ) {
global $post;
$value = null;
if ( is_object( $post ) && ! empty( $field['id'] ) ) {
if ( $this->args['data_type'] !== 'serialize' ) {
$meta = get_post_meta( $post->ID, $field['id'] );
$value = ( isset( $meta[0] ) ) ? $meta[0] : null;
} else {
$meta = get_post_meta( $post->ID, $this->unique, true );
$value = ( isset( $meta[$field['id']] ) ) ? $meta[$field['id']] : null;
}
}
$default = ( isset( $field['id'] ) ) ? $this->get_default( $field ) : '';
$value = ( isset( $value ) ) ? $value : $default;
return $value;
}
// add metabox content
public function add_meta_box_content( $post, $callback ) {
global $post;
$has_nav = ( count( $this->sections ) > 1 && $this->args['context'] !== 'side' ) ? true : false;
$show_all = ( ! $has_nav ) ? ' csf-show-all' : '';
$post_type = ( is_object ( $post ) ) ? $post->post_type : '';
$errors = ( is_object ( $post ) ) ? get_post_meta( $post->ID, '_csf_errors_'. $this->unique, true ) : array();
$errors = ( ! empty( $errors ) ) ? $errors : array();
$theme = ( $this->args['theme'] ) ? ' csf-theme-'. $this->args['theme'] : '';
$nav_type = ( $this->args['nav'] === 'inline' ) ? 'inline' : 'normal';
if ( is_object ( $post ) && ! empty( $errors ) ) {
delete_post_meta( $post->ID, '_csf_errors_'. $this->unique );
}
wp_nonce_field( 'csf_metabox_nonce', 'csf_metabox_nonce'. $this->unique );
echo '<div class="csf csf-metabox'. esc_attr( $theme ) .'">';
echo '<div class="csf-wrapper'. esc_attr( $show_all ) .'">';
if ( $has_nav ) {
echo '<div class="csf-nav csf-nav-'. esc_attr( $nav_type ) .' csf-nav-metabox">';
echo '<ul>';
$tab_key = 0;
foreach ( $this->sections as $section ) {
if ( ! empty( $section['post_type'] ) && ! in_array( $post_type, array_filter( (array) $section['post_type'] ) ) ) {
continue;
}
$tab_error = ( ! empty( $errors['sections'][$tab_key] ) ) ? '<i class="csf-label-error csf-error">!</i>' : '';
$tab_icon = ( ! empty( $section['icon'] ) ) ? '<i class="csf-tab-icon '. esc_attr( $section['icon'] ) .'"></i>' : '';
echo '<li><a href="#">'. $tab_icon . $section['title'] . $tab_error .'</a></li>';
$tab_key++;
}
echo '</ul>';
echo '</div>';
}
echo '<div class="csf-content">';
echo '<div class="csf-sections">';
$section_key = 0;
foreach ( $this->sections as $section ) {
if ( ! empty( $section['post_type'] ) && ! in_array( $post_type, array_filter( (array) $section['post_type'] ) ) ) {
continue;
}
$section_onload = ( ! $has_nav ) ? ' csf-onload' : '';
$section_class = ( ! empty( $section['class'] ) ) ? ' '. $section['class'] : '';
$section_title = ( ! empty( $section['title'] ) ) ? $section['title'] : '';
$section_icon = ( ! empty( $section['icon'] ) ) ? '<i class="csf-section-icon '. esc_attr( $section['icon'] ) .'"></i>' : '';
echo '<div class="csf-section hidden'. esc_attr( $section_onload . $section_class ) .'">';
echo ( $section_title || $section_icon ) ? '<div class="csf-section-title"><h3>'. $section_icon . $section_title .'</h3></div>' : '';
if ( ! empty( $section['fields'] ) ) {
foreach ( $section['fields'] as $field ) {
if ( ! empty( $field['id'] ) && ! empty( $errors['fields'][$field['id']] ) ) {
$field['_error'] = $errors['fields'][$field['id']];
}
if ( ! empty( $field['id'] ) ) {
$field['default'] = $this->get_default( $field );
}
CSF::field( $field, $this->get_meta_value( $field ), $this->unique, 'metabox' );
}
} else {
echo '<div class="csf-no-option">'. esc_html__( 'No data available.', 'csf' ) .'</div>';
}
echo '</div>';
$section_key++;
}
echo '</div>';
if ( ! empty( $this->args['show_restore'] ) || ! empty( $this->args['show_reset'] ) ) {
echo '<div class="csf-sections-reset">';
echo '<label>';
echo '<input type="checkbox" name="'. esc_attr( $this->unique ) .'[_reset]" />';
echo '<span class="button csf-button-reset">'. esc_html__( 'Reset', 'csf' ) .'</span>';
echo '<span class="button csf-button-cancel">'. sprintf( '<small>( %s )</small> %s', esc_html__( 'update post', 'csf' ), esc_html__( 'Cancel', 'csf' ) ) .'</span>';
echo '</label>';
echo '</div>';
}
echo '</div>';
echo ( $has_nav && $nav_type === 'normal' ) ? '<div class="csf-nav-background"></div>' : '';
echo '<div class="clear"></div>';
echo '</div>';
echo '</div>';
}
// save metabox
public function save_meta_box( $post_id ) {
$count = 1;
$data = array();
$errors = array();
$noncekey = 'csf_metabox_nonce'. $this->unique;
$nonce = ( ! empty( $_POST[ $noncekey ] ) ) ? sanitize_text_field( wp_unslash( $_POST[ $noncekey ] ) ) : '';
if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || ! wp_verify_nonce( $nonce, 'csf_metabox_nonce' ) ) {
return $post_id;
}
// XSS ok.
// No worries, This "POST" requests is sanitizing in the below foreach.
$request = ( ! empty( $_POST[ $this->unique ] ) ) ? $_POST[ $this->unique ] : array();
if ( ! empty( $request ) ) {
foreach ( $this->sections as $section ) {
if ( ! empty( $section['fields'] ) ) {
foreach ( $section['fields'] as $field ) {
if ( ! empty( $field['id'] ) ) {
$field_id = $field['id'];
$field_value = isset( $request[$field_id] ) ? $request[$field_id] : '';
// Sanitize "post" request of field.
if ( ! isset( $field['sanitize'] ) ) {
if( is_array( $field_value ) ) {
$data[$field_id] = wp_kses_post_deep( $field_value );
} else {
$data[$field_id] = wp_kses_post( $field_value );
}
} else if( isset( $field['sanitize'] ) && is_callable( $field['sanitize'] ) ) {
$data[$field_id] = call_user_func( $field['sanitize'], $field_value );
} else {
$data[$field_id] = $field_value;
}
// Validate "post" request of field.
if ( isset( $field['validate'] ) && is_callable( $field['validate'] ) ) {
$has_validated = call_user_func( $field['validate'], $field_value );
if ( ! empty( $has_validated ) ) {
$errors['sections'][$count] = true;
$errors['fields'][$field_id] = $has_validated;
$data[$field_id] = $this->get_meta_value( $field );
}
}
}
}
}
$count++;
}
}
$data = apply_filters( "csf_{$this->unique}_save", $data, $post_id, $this );
do_action( "csf_{$this->unique}_save_before", $data, $post_id, $this );
if ( empty( $data ) || ! empty( $request['_reset'] ) ) {
if ( $this->args['data_type'] !== 'serialize' ) {
foreach ( $data as $key => $value ) {
delete_post_meta( $post_id, $key );
}
} else {
delete_post_meta( $post_id, $this->unique );
}
} else {
if ( $this->args['data_type'] !== 'serialize' ) {
foreach ( $data as $key => $value ) {
update_post_meta( $post_id, $key, $value );
}
} else {
update_post_meta( $post_id, $this->unique, $data );
}
if ( ! empty( $errors ) ) {
update_post_meta( $post_id, '_csf_errors_'. $this->unique, $errors );
}
}
do_action( "csf_{$this->unique}_saved", $data, $post_id, $this );
do_action( "csf_{$this->unique}_save_after", $data, $post_id, $this );
}
}
}

View File

@@ -0,0 +1,254 @@
<?php if ( ! defined( 'ABSPATH' ) ) { die; } // Cannot access directly.
/**
*
* Nav Menu Options Class
*
* @since 1.0.0
* @version 1.0.0
*
*/
if ( ! class_exists( 'CSF_Nav_Menu_Options' ) ) {
class CSF_Nav_Menu_Options extends CSF_Abstract{
// constans
public $unique = '';
public $abstract = 'menu';
public $sections = array();
public $args = array(
'data_type' => 'serialize',
'class' => '',
'defaults' => array(),
);
// run menu construct
public function __construct( $key, $params ) {
$this->unique = $key;
$this->args = apply_filters( "csf_{$this->unique}_args", wp_parse_args( $params['args'], $this->args ), $this );
$this->sections = apply_filters( "csf_{$this->unique}_sections", $params['sections'], $this );
add_action( 'wp_nav_menu_item_custom_fields', array( &$this, 'wp_nav_menu_item_custom_fields' ), 10, 4 );
add_action( 'wp_update_nav_menu_item', array( &$this, 'wp_update_nav_menu_item' ), 10, 3 );
add_filter( 'wp_edit_nav_menu_walker', array( &$this, 'wp_edit_nav_menu_walker' ), 10, 2 );
}
// instance
public static function instance( $key, $params ) {
return new self( $key, $params );
}
public function wp_edit_nav_menu_walker( $class, $menu_id ) {
global $wp_version;
if( version_compare( $wp_version, '5.4.0', '<' ) ) {
if ( ! class_exists( 'CSF_Walker_Nav_Menu_Edit' ) ) {
CSF::include_plugin_file( 'functions/walker.php' );
}
return 'CSF_Walker_Nav_Menu_Edit';
}
return $class;
}
// get default value
public function get_default( $field ) {
$default = ( isset( $field['default'] ) ) ? $field['default'] : '';
$default = ( isset( $this->args['defaults'][$field['id']] ) ) ? $this->args['defaults'][$field['id']] : $default;
return $default;
}
// get meta value
public function get_meta_value( $menu_item_id, $field ) {
$value = null;
if ( ! empty( $menu_item_id ) && ! empty( $field['id'] ) ) {
if ( $this->args['data_type'] !== 'serialize' ) {
$meta = get_post_meta( $menu_item_id, $field['id'] );
$value = ( isset( $meta[0] ) ) ? $meta[0] : null;
} else {
$meta = get_post_meta( $menu_item_id, $this->unique, true );
$value = ( isset( $meta[$field['id']] ) ) ? $meta[$field['id']] : null;
}
}
$default = ( isset( $field['id'] ) ) ? $this->get_default( $field ) : '';
$value = ( isset( $value ) ) ? $value : $default;
return $value;
}
//
public function wp_nav_menu_item_custom_fields( $menu_item_id, $item, $depth, $args ) {
$errors = ( ! empty( $menu_item_id ) ) ? get_post_meta( $menu_item_id, '_csf_errors_'. $this->unique, true ) : array();
$errors = ( ! empty( $errors ) ) ? $errors : array();
$class = ( $this->args['class'] ) ? ' '. $this->args['class'] : '';
if ( ! empty( $errors ) ) {
delete_post_meta( $menu_item_id, '_csf_errors_'. $this->unique );
}
echo '<div class="csf csf-nav-menu-options'. esc_attr( $class ) .'">';
foreach ( $this->sections as $section ) {
$section_icon = ( ! empty( $section['icon'] ) ) ? '<i class="csf-nav-menu-icon '. esc_attr( $section['icon'] ) .'"></i>' : '';
$section_title = ( ! empty( $section['title'] ) ) ? $section['title'] : '';
echo '<div class="csf-fields">';
echo ( $section_title || $section_icon ) ? '<div class="csf-nav-menu-title"><h4>'. $section_icon . $section_title .'</h4></div>' : '';
if ( ! empty( $section['fields'] ) ) {
foreach ( $section['fields'] as $field ) {
if ( ! empty( $field['id'] ) && ! empty( $errors['fields'][$field['id']] ) ) {
$field['_error'] = $errors['fields'][$field['id']];
}
if ( ! empty( $field['id'] ) ) {
$field['default'] = $this->get_default( $field );
}
CSF::field( $field, $this->get_meta_value( $menu_item_id, $field ), $this->unique .'['. $menu_item_id .']', 'menu' );
}
}
echo '</div>';
}
echo '</div>';
}
public function wp_update_nav_menu_item( $menu_id, $menu_item_db_id, $menu_item_args ) {
$count = 1;
$data = array();
$errors = array();
$noncekey = 'update-nav-menu-nonce';
$nonce = ( ! empty( $_POST[ $noncekey ] ) ) ? sanitize_text_field( wp_unslash( $_POST[ $noncekey ] ) ) : '';
if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || ! wp_verify_nonce( $nonce, 'update-nav_menu' ) ) {
return $menu_item_db_id;
}
// XSS ok.
// No worries, This "POST" requests is sanitizing in the below foreach.
$request = ( ! empty( $_POST[ $this->unique ][ $menu_item_db_id ] ) ) ? $_POST[ $this->unique ][ $menu_item_db_id ] : array();
if ( ! empty( $request ) ) {
foreach ( $this->sections as $section ) {
if ( ! empty( $section['fields'] ) ) {
foreach ( $section['fields'] as $field ) {
if ( ! empty( $field['id'] ) ) {
$field_id = $field['id'];
$field_value = isset( $request[$field_id] ) ? $request[$field_id] : '';
// Sanitize "post" request of field.
if ( ! isset( $field['sanitize'] ) ) {
if( is_array( $field_value ) ) {
$data[$field_id] = wp_kses_post_deep( $field_value );
} else {
$data[$field_id] = wp_kses_post( $field_value );
}
} else if( isset( $field['sanitize'] ) && is_callable( $field['sanitize'] ) ) {
$data[$field_id] = call_user_func( $field['sanitize'], $field_value );
} else {
$data[$field_id] = $field_value;
}
// Validate "post" request of field.
if ( isset( $field['validate'] ) && is_callable( $field['validate'] ) ) {
$has_validated = call_user_func( $field['validate'], $field_value );
if ( ! empty( $has_validated ) ) {
$errors['sections'][$count] = true;
$errors['fields'][$field_id] = $has_validated;
$data[$field_id] = $this->get_meta_value( $menu_item_db_id, $field );
}
}
}
}
}
$count++;
}
}
$data = apply_filters( "csf_{$this->unique}_save", $data, $menu_item_db_id, $this );
do_action( "csf_{$this->unique}_save_before", $data, $menu_item_db_id, $this );
if ( empty( $data ) ) {
if ( $this->args['data_type'] !== 'serialize' ) {
foreach ( $data as $key => $value ) {
delete_post_meta( $menu_item_db_id, $key );
}
} else {
delete_post_meta( $menu_item_db_id, $this->unique );
}
} else {
if ( $this->args['data_type'] !== 'serialize' ) {
foreach ( $data as $key => $value ) {
update_post_meta( $menu_item_db_id, $key, $value );
}
} else {
update_post_meta( $menu_item_db_id, $this->unique, $data );
}
if ( ! empty( $errors ) ) {
update_post_meta( $menu_item_db_id, '_csf_errors_'. $this->unique, $errors );
}
}
do_action( "csf_{$this->unique}_saved", $data, $menu_item_db_id, $this );
do_action( "csf_{$this->unique}_save_after", $data, $menu_item_db_id, $this );
}
}
}

View File

@@ -0,0 +1,244 @@
<?php if ( ! defined( 'ABSPATH' ) ) { die; } // Cannot access directly.
/**
*
* Profile Option Class
*
* @since 1.0.0
* @version 1.0.0
*
*/
if ( ! class_exists( 'CSF_Profile_Options' ) ) {
class CSF_Profile_Options extends CSF_Abstract{
// constans
public $unique = '';
public $abstract = 'profile';
public $sections = array();
public $args = array(
'data_type' => 'serialize',
'class' => '',
'defaults' => array(),
);
// run profile construct
public function __construct( $key, $params ) {
$this->unique = $key;
$this->args = apply_filters( "csf_{$this->unique}_args", wp_parse_args( $params['args'], $this->args ), $this );
$this->sections = apply_filters( "csf_{$this->unique}_sections", $params['sections'], $this );
add_action( 'admin_init', array( &$this, 'add_profile_options' ) );
}
// instance
public static function instance( $key, $params ) {
return new self( $key, $params );
}
// add profile add/edit fields
public function add_profile_options() {
add_action( 'show_user_profile', array( &$this, 'render_profile_form_fields' ) );
add_action( 'edit_user_profile', array( &$this, 'render_profile_form_fields' ) );
add_action( 'personal_options_update', array( &$this, 'save_profile' ) );
add_action( 'edit_user_profile_update', array( &$this, 'save_profile' ) );
}
// get default value
public function get_default( $field ) {
$default = ( isset( $field['default'] ) ) ? $field['default'] : '';
$default = ( isset( $this->args['defaults'][$field['id']] ) ) ? $this->args['defaults'][$field['id']] : $default;
return $default;
}
// get meta value
public function get_meta_value( $user_id, $field ) {
$value = null;
if ( ! empty( $user_id ) && ! empty( $field['id'] ) ) {
if ( $this->args['data_type'] !== 'serialize' ) {
$meta = get_user_meta( $user_id, $field['id'] );
$value = ( isset( $meta[0] ) ) ? $meta[0] : null;
} else {
$meta = get_user_meta( $user_id, $this->unique, true );
$value = ( isset( $meta[$field['id']] ) ) ? $meta[$field['id']] : null;
}
}
$default = ( isset( $field['id'] ) ) ? $this->get_default( $field ) : '';
$value = ( isset( $value ) ) ? $value : $default;
return $value;
}
// render profile add/edit form fields
public function render_profile_form_fields( $profileuser ) {
$is_profile = ( is_object( $profileuser ) && isset( $profileuser->ID ) ) ? true : false;
$profile_id = ( $is_profile ) ? $profileuser->ID : 0;
$errors = ( ! empty( $profile_id ) ) ? get_user_meta( $profile_id, '_csf_errors_'. $this->unique, true ) : array();
$errors = ( ! empty( $errors ) ) ? $errors : array();
$class = ( $this->args['class'] ) ? ''. $this->args['class'] : '';
if ( ! empty( $errors ) ) {
delete_user_meta( $profile_id, '_csf_errors_'. $this->unique );
}
echo '<div class="csf csf-profile-options csf-onload'. esc_attr( $class ) .'">';
wp_nonce_field( 'csf_profile_nonce', 'csf_profile_nonce'. $this->unique );
foreach ( $this->sections as $section ) {
$section_icon = ( ! empty( $section['icon'] ) ) ? '<i class="csf-section-icon '. esc_attr( $section['icon'] ) .'"></i>' : '';
$section_title = ( ! empty( $section['title'] ) ) ? $section['title'] : '';
echo ( $section_title || $section_icon ) ? '<h2>'. $section_icon . $section_title .'</h2>' : '';
if ( ! empty( $section['fields'] ) ) {
foreach ( $section['fields'] as $field ) {
if ( ! empty( $field['id'] ) && ! empty( $errors['fields'][$field['id']] ) ) {
$field['_error'] = $errors['fields'][$field['id']];
}
if ( ! empty( $field['id'] ) ) {
$field['default'] = $this->get_default( $field );
}
CSF::field( $field, $this->get_meta_value( $profile_id, $field ), $this->unique, 'profile' );
}
}
}
echo '</div>';
}
// save profile form fields
public function save_profile( $user_id ) {
$count = 1;
$data = array();
$errors = array();
$noncekey = 'csf_profile_nonce'. $this->unique;
$nonce = ( ! empty( $_POST[ $noncekey ] ) ) ? sanitize_text_field( wp_unslash( $_POST[ $noncekey ] ) ) : '';
if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || ! wp_verify_nonce( $nonce, 'csf_profile_nonce' ) ) {
return $user_id;
}
// XSS ok.
// No worries, This "POST" requests is sanitizing in the below foreach.
$request = ( ! empty( $_POST[ $this->unique ] ) ) ? $_POST[ $this->unique ] : array();
if ( ! empty( $request ) ) {
foreach ( $this->sections as $section ) {
if ( ! empty( $section['fields'] ) ) {
foreach ( $section['fields'] as $field ) {
if ( ! empty( $field['id'] ) ) {
$field_id = $field['id'];
$field_value = isset( $request[$field_id] ) ? $request[$field_id] : '';
// Sanitize "post" request of field.
if ( ! isset( $field['sanitize'] ) ) {
if( is_array( $field_value ) ) {
$data[$field_id] = wp_kses_post_deep( $field_value );
} else {
$data[$field_id] = wp_kses_post( $field_value );
}
} else if( isset( $field['sanitize'] ) && is_callable( $field['sanitize'] ) ) {
$data[$field_id] = call_user_func( $field['sanitize'], $field_value );
} else {
$data[$field_id] = $field_value;
}
// Validate "post" request of field.
if ( isset( $field['validate'] ) && is_callable( $field['validate'] ) ) {
$has_validated = call_user_func( $field['validate'], $field_value );
if ( ! empty( $has_validated ) ) {
$errors['sections'][$count] = true;
$errors['fields'][$field_id] = $has_validated;
$data[$field_id] = $this->get_meta_value( $user_id, $field );
}
}
}
}
}
$count++;
}
}
$data = apply_filters( "csf_{$this->unique}_save", $data, $user_id, $this );
do_action( "csf_{$this->unique}_save_before", $data, $user_id, $this );
if ( empty( $data ) ) {
if ( $this->args['data_type'] !== 'serialize' ) {
foreach ( $data as $key => $value ) {
delete_user_meta( $user_id, $key );
}
} else {
delete_user_meta( $user_id, $this->unique );
}
} else {
if ( $this->args['data_type'] !== 'serialize' ) {
foreach ( $data as $key => $value ) {
update_user_meta( $user_id, $key, $value );
}
} else {
update_user_meta( $user_id, $this->unique, $data );
}
if ( ! empty( $errors ) ) {
update_user_meta( $user_id, '_csf_errors_'. $this->unique, $errors );
}
}
do_action( "csf_{$this->unique}_saved", $data, $user_id, $this );
do_action( "csf_{$this->unique}_save_after", $data, $user_id, $this );
}
}
}

View File

@@ -0,0 +1,785 @@
<?php if ( ! defined( 'ABSPATH' ) ) { die; } // Cannot access directly.
/**
*
* Setup Class
*
* @since 1.0.0
* @version 1.0.0
*
*/
if ( ! class_exists( 'CSF' ) ) {
class CSF {
// Default constants
public static $premium = true;
public static $version = '2.2.2';
public static $dir = '';
public static $url = '';
public static $css = '';
public static $file = '';
public static $enqueue = false;
public static $webfonts = array();
public static $subsets = array();
public static $inited = array();
public static $fields = array();
public static $args = array(
'admin_options' => array(),
'customize_options' => array(),
'metabox_options' => array(),
'nav_menu_options' => array(),
'profile_options' => array(),
'taxonomy_options' => array(),
'widget_options' => array(),
'comment_options' => array(),
'shortcode_options' => array(),
);
// Shortcode instances
public static $shortcode_instances = array();
private static $instance = null;
public static function init( $file = __FILE__ ) {
// Set file constant
self::$file = $file;
// Set constants
self::constants();
// Include files
self::includes();
if ( is_null( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
// Initalize
public function __construct() {
// Init action
do_action( 'csf_init' );
// Setup textdomain
self::textdomain();
add_action( 'after_setup_theme', array( 'CSF', 'setup' ) );
add_action( 'init', array( 'CSF', 'setup' ) );
add_action( 'switch_theme', array( 'CSF', 'setup' ) );
add_action( 'admin_enqueue_scripts', array( 'CSF', 'add_admin_enqueue_scripts' ) );
add_action( 'wp_enqueue_scripts', array( 'CSF', 'add_typography_enqueue_styles' ), 80 );
add_action( 'wp_head', array( 'CSF', 'add_custom_css' ), 80 );
add_filter( 'admin_body_class', array( 'CSF', 'add_admin_body_class' ) );
}
// Setup frameworks
public static function setup() {
// Welcome page
self::include_plugin_file( 'views/welcome.php' );
// Setup admin option framework
$params = array();
if ( class_exists( 'CSF_Options' ) && ! empty( self::$args['admin_options'] ) ) {
foreach ( self::$args['admin_options'] as $key => $value ) {
if ( ! empty( self::$args['sections'][$key] ) && ! isset( self::$inited[$key] ) ) {
$params['args'] = $value;
$params['sections'] = self::$args['sections'][$key];
self::$inited[$key] = true;
CSF_Options::instance( $key, $params );
if ( ! empty( $value['show_in_customizer'] ) ) {
$value['output_css'] = false;
$value['enqueue_webfont'] = false;
self::$args['customize_options'][$key] = $value;
self::$inited[$key] = null;
}
}
}
}
// Setup customize option framework
$params = array();
if ( class_exists( 'CSF_Customize_Options' ) && ! empty( self::$args['customize_options'] ) ) {
foreach ( self::$args['customize_options'] as $key => $value ) {
if ( ! empty( self::$args['sections'][$key] ) && ! isset( self::$inited[$key] ) ) {
$params['args'] = $value;
$params['sections'] = self::$args['sections'][$key];
self::$inited[$key] = true;
CSF_Customize_Options::instance( $key, $params );
}
}
}
// Setup metabox option framework
$params = array();
if ( class_exists( 'CSF_Metabox' ) && ! empty( self::$args['metabox_options'] ) ) {
foreach ( self::$args['metabox_options'] as $key => $value ) {
if ( ! empty( self::$args['sections'][$key] ) && ! isset( self::$inited[$key] ) ) {
$params['args'] = $value;
$params['sections'] = self::$args['sections'][$key];
self::$inited[$key] = true;
CSF_Metabox::instance( $key, $params );
}
}
}
// Setup nav menu option framework
$params = array();
if ( class_exists( 'CSF_Nav_Menu_Options' ) && ! empty( self::$args['nav_menu_options'] ) ) {
foreach ( self::$args['nav_menu_options'] as $key => $value ) {
if ( ! empty( self::$args['sections'][$key] ) && ! isset( self::$inited[$key] ) ) {
$params['args'] = $value;
$params['sections'] = self::$args['sections'][$key];
self::$inited[$key] = true;
CSF_Nav_Menu_Options::instance( $key, $params );
}
}
}
// Setup profile option framework
$params = array();
if ( class_exists( 'CSF_Profile_Options' ) && ! empty( self::$args['profile_options'] ) ) {
foreach ( self::$args['profile_options'] as $key => $value ) {
if ( ! empty( self::$args['sections'][$key] ) && ! isset( self::$inited[$key] ) ) {
$params['args'] = $value;
$params['sections'] = self::$args['sections'][$key];
self::$inited[$key] = true;
CSF_Profile_Options::instance( $key, $params );
}
}
}
// Setup taxonomy option framework
$params = array();
if ( class_exists( 'CSF_Taxonomy_Options' ) && ! empty( self::$args['taxonomy_options'] ) ) {
$taxonomy = ( isset( $_GET['taxonomy'] ) ) ? sanitize_text_field( wp_unslash( $_GET['taxonomy'] ) ) : '';
foreach ( self::$args['taxonomy_options'] as $key => $value ) {
if ( ! empty( self::$args['sections'][$key] ) && ! isset( self::$inited[$key] ) ) {
$params['args'] = $value;
$params['sections'] = self::$args['sections'][$key];
self::$inited[$key] = true;
CSF_Taxonomy_Options::instance( $key, $params );
}
}
}
// Setup widget option framework
if ( class_exists( 'CSF_Widget' ) && class_exists( 'WP_Widget_Factory' ) && ! empty( self::$args['widget_options'] ) ) {
$wp_widget_factory = new WP_Widget_Factory();
foreach ( self::$args['widget_options'] as $key => $value ) {
if ( ! isset( self::$inited[$key] ) ) {
self::$inited[$key] = true;
$wp_widget_factory->register( CSF_Widget::instance( $key, $value ) );
}
}
}
// Setup comment option framework
$params = array();
if ( class_exists( 'CSF_Comment_Metabox' ) && ! empty( self::$args['comment_options'] ) ) {
foreach ( self::$args['comment_options'] as $key => $value ) {
if ( ! empty( self::$args['sections'][$key] ) && ! isset( self::$inited[$key] ) ) {
$params['args'] = $value;
$params['sections'] = self::$args['sections'][$key];
self::$inited[$key] = true;
CSF_Comment_Metabox::instance( $key, $params );
}
}
}
// Setup shortcode option framework
$params = array();
if ( class_exists( 'CSF_Shortcoder' ) && ! empty( self::$args['shortcode_options'] ) ) {
foreach ( self::$args['shortcode_options'] as $key => $value ) {
if ( ! empty( self::$args['sections'][$key] ) && ! isset( self::$inited[$key] ) ) {
$params['args'] = $value;
$params['sections'] = self::$args['sections'][$key];
self::$inited[$key] = true;
CSF_Shortcoder::instance( $key, $params );
}
}
// Once editor setup for gutenberg and media buttons
if ( class_exists( 'CSF_Shortcoder' ) && ! empty( self::$shortcode_instances ) ) {
foreach ( self::$shortcode_instances as $instance ) {
if ( ! empty( $instance['show_in_editor'] ) ) {
CSF_Shortcoder::once_editor_setup();
break;
}
}
}
}
do_action( 'csf_loaded' );
}
// Create options
public static function createOptions( $id, $args = array() ) {
self::$args['admin_options'][$id] = $args;
}
// Create customize options
public static function createCustomizeOptions( $id, $args = array() ) {
self::$args['customize_options'][$id] = $args;
}
// Create metabox options
public static function createMetabox( $id, $args = array() ) {
self::$args['metabox_options'][$id] = $args;
}
// Create menu options
public static function createNavMenuOptions( $id, $args = array() ) {
self::$args['nav_menu_options'][$id] = $args;
}
// Create shortcoder options
public static function createShortcoder( $id, $args = array() ) {
self::$args['shortcode_options'][$id] = $args;
}
// Create taxonomy options
public static function createTaxonomyOptions( $id, $args = array() ) {
self::$args['taxonomy_options'][$id] = $args;
}
// Create profile options
public static function createProfileOptions( $id, $args = array() ) {
self::$args['profile_options'][$id] = $args;
}
// Create widget
public static function createWidget( $id, $args = array() ) {
self::$args['widget_options'][$id] = $args;
self::set_used_fields( $args );
}
// Create comment metabox
public static function createCommentMetabox( $id, $args = array() ) {
self::$args['comment_options'][$id] = $args;
}
// Create section
public static function createSection( $id, $sections ) {
self::$args['sections'][$id][] = $sections;
self::set_used_fields( $sections );
}
// Set directory constants
public static function constants() {
// We need this path-finder code for set URL of framework
$dirname = str_replace( '//', '/', wp_normalize_path( dirname( dirname( self::$file ) ) ) );
$theme_dir = str_replace( '//', '/', wp_normalize_path( get_parent_theme_file_path() ) );
$plugin_dir = str_replace( '//', '/', wp_normalize_path( WP_PLUGIN_DIR ) );
$located_plugin = ( preg_match( '#'. self::sanitize_dirname( $plugin_dir ) .'#', self::sanitize_dirname( $dirname ) ) ) ? true : false;
$directory = ( $located_plugin ) ? $plugin_dir : $theme_dir;
$directory_uri = ( $located_plugin ) ? WP_PLUGIN_URL : get_parent_theme_file_uri();
$foldername = str_replace( $directory, '', $dirname );
$protocol_uri = ( is_ssl() ) ? 'https' : 'http';
$directory_uri = set_url_scheme( $directory_uri, $protocol_uri );
self::$dir = $dirname;
self::$url = $directory_uri . $foldername;
}
// Include file helper
public static function include_plugin_file( $file, $load = true ) {
$path = '';
$file = ltrim( $file, '/' );
$override = apply_filters( 'csf_override', 'csf-override' );
if ( file_exists( get_parent_theme_file_path( $override .'/'. $file ) ) ) {
$path = get_parent_theme_file_path( $override .'/'. $file );
} elseif ( file_exists( get_theme_file_path( $override .'/'. $file ) ) ) {
$path = get_theme_file_path( $override .'/'. $file );
} elseif ( file_exists( self::$dir .'/'. $override .'/'. $file ) ) {
$path = self::$dir .'/'. $override .'/'. $file;
} elseif ( file_exists( self::$dir .'/'. $file ) ) {
$path = self::$dir .'/'. $file;
}
if ( ! empty( $path ) && ! empty( $file ) && $load ) {
global $wp_query;
if ( is_object( $wp_query ) && function_exists( 'load_template' ) ) {
load_template( $path, true );
} else {
require_once( $path );
}
} else {
return self::$dir .'/'. $file;
}
}
// Is active plugin helper
public static function is_active_plugin( $file = '' ) {
return in_array( $file, (array) get_option( 'active_plugins', array() ) );
}
// Sanitize dirname
public static function sanitize_dirname( $dirname ) {
return preg_replace( '/[^A-Za-z]/', '', $dirname );
}
// Set url constant
public static function include_plugin_url( $file ) {
return esc_url( self::$url ) .'/'. ltrim( $file, '/' );
}
// Include files
public static function includes() {
// Helpers
self::include_plugin_file( 'functions/actions.php' );
self::include_plugin_file( 'functions/helpers.php' );
self::include_plugin_file( 'functions/sanitize.php' );
self::include_plugin_file( 'functions/validate.php' );
// Includes free version classes
self::include_plugin_file( 'classes/abstract.class.php' );
self::include_plugin_file( 'classes/fields.class.php' );
self::include_plugin_file( 'classes/admin-options.class.php' );
// Includes premium version classes
if ( self::$premium ) {
self::include_plugin_file( 'classes/customize-options.class.php' );
self::include_plugin_file( 'classes/metabox-options.class.php' );
self::include_plugin_file( 'classes/nav-menu-options.class.php' );
self::include_plugin_file( 'classes/profile-options.class.php' );
self::include_plugin_file( 'classes/shortcode-options.class.php' );
self::include_plugin_file( 'classes/taxonomy-options.class.php' );
self::include_plugin_file( 'classes/widget-options.class.php' );
self::include_plugin_file( 'classes/comment-options.class.php' );
}
// Include all framework fields
$fields = apply_filters( 'csf_fields', array(
'accordion',
'background',
'backup',
'border',
'button_set',
'callback',
'checkbox',
'code_editor',
'color',
'color_group',
'content',
'date',
'dimensions',
'fieldset',
'gallery',
'group',
'heading',
'icon',
'image_select',
'link',
'link_color',
'map',
'media',
'notice',
'number',
'palette',
'radio',
'repeater',
'select',
'slider',
'sortable',
'sorter',
'spacing',
'spinner',
'subheading',
'submessage',
'switcher',
'tabbed',
'text',
'textarea',
'typography',
'upload',
'wp_editor',
) );
if ( ! empty( $fields ) ) {
foreach ( $fields as $field ) {
if ( ! class_exists( 'CSF_Field_'. $field ) && class_exists( 'CSF_Fields' ) ) {
self::include_plugin_file( 'fields/'. $field .'/'. $field .'.php' );
}
}
}
}
// Setup textdomain
public static function textdomain() {
load_textdomain( 'csf', self::$dir .'/languages/'. get_locale() .'.mo' );
}
// Set all of used fields
public static function set_used_fields( $sections ) {
if ( ! empty( $sections['fields'] ) ) {
foreach ( $sections['fields'] as $field ) {
if ( ! empty( $field['fields'] ) ) {
self::set_used_fields( $field );
}
if ( ! empty( $field['tabs'] ) ) {
self::set_used_fields( array( 'fields' => $field['tabs'] ) );
}
if ( ! empty( $field['accordions'] ) ) {
self::set_used_fields( array( 'fields' => $field['accordions'] ) );
}
if ( ! empty( $field['type'] ) ) {
self::$fields[$field['type']] = $field;
}
}
}
}
// Enqueue admin and fields styles and scripts
public static function add_admin_enqueue_scripts() {
// Loads scripts and styles only when needed
$wpscreen = get_current_screen();
if ( ! empty( self::$args['admin_options'] ) ) {
foreach ( self::$args['admin_options'] as $argument ) {
if ( substr( $wpscreen->id, -strlen( $argument['menu_slug'] ) ) === $argument['menu_slug'] ) {
self::$enqueue = true;
}
}
}
if ( ! empty( self::$args['metabox_options'] ) ) {
foreach ( self::$args['metabox_options'] as $argument ) {
if ( in_array( $wpscreen->post_type, (array) $argument['post_type'] ) ) {
self::$enqueue = true;
}
}
}
if ( ! empty( self::$args['taxonomy_options'] ) ) {
foreach ( self::$args['taxonomy_options'] as $argument ) {
if ( in_array( $wpscreen->taxonomy, (array) $argument['taxonomy'] ) ) {
self::$enqueue = true;
}
}
}
if ( ! empty( self::$shortcode_instances ) ) {
foreach ( self::$shortcode_instances as $argument ) {
if ( ( $argument['show_in_editor'] && $wpscreen->base === 'post' ) || $argument['show_in_custom'] ) {
self::$enqueue = true;
}
}
}
if ( ! empty( self::$args['widget_options'] ) && ( $wpscreen->id === 'widgets' || $wpscreen->id === 'customize' ) ) {
self::$enqueue = true;
}
if ( ! empty( self::$args['customize_options'] ) && $wpscreen->id === 'customize' ) {
self::$enqueue = true;
}
if ( ! empty( self::$args['nav_menu_options'] ) && $wpscreen->id === 'nav-menus' ) {
self::$enqueue = true;
}
if ( ! empty( self::$args['profile_options'] ) && ( $wpscreen->id === 'profile' || $wpscreen->id === 'user-edit' ) ) {
self::$enqueue = true;
}
if ( ! empty( self::$args['comment_options'] ) && $wpscreen->id === 'comment' ) {
self::$enqueue = true;
}
if ( $wpscreen->id === 'tools_page_csf-welcome' ) {
self::$enqueue = true;
}
if ( ! apply_filters( 'csf_enqueue_assets', self::$enqueue ) ) {
return;
}
// Check for developer mode
$min = ( self::$premium && SCRIPT_DEBUG ) ? '' : '.min';
// Admin utilities
wp_enqueue_media();
// Wp color picker
wp_enqueue_style( 'wp-color-picker' );
wp_enqueue_script( 'wp-color-picker' );
// Font awesome 4 and 5 loader
if ( apply_filters( 'csf_fa4', false ) ) {
wp_enqueue_style( 'csf-fa', 'https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome'. $min .'.css', array(), '4.7.0', 'all' );
} else {
wp_enqueue_style( 'csf-fa5', 'https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.3/css/all'. $min .'.css', array(), '5.15.3', 'all' );
wp_enqueue_style( 'csf-fa5-v4-shims', 'https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.3/css/v4-shims'. $min .'.css', array(), '5.15.3', 'all' );
}
// Main style
wp_enqueue_style( 'csf', self::include_plugin_url( 'assets/css/style'. $min .'.css' ), array(), self::$version, 'all' );
// Main RTL styles
if ( is_rtl() ) {
wp_enqueue_style( 'csf-rtl', self::include_plugin_url( 'assets/css/style-rtl'. $min .'.css' ), array(), self::$version, 'all' );
}
// Main scripts
wp_enqueue_script( 'csf-plugins', self::include_plugin_url( 'assets/js/plugins'. $min .'.js' ), array(), self::$version, true );
wp_enqueue_script( 'csf', self::include_plugin_url( 'assets/js/main'. $min .'.js' ), array( 'csf-plugins' ), self::$version, true );
// Main variables
wp_localize_script( 'csf', 'csf_vars', array(
'color_palette' => apply_filters( 'csf_color_palette', array() ),
'i18n' => array(
'confirm' => esc_html__( 'Are you sure?', 'csf' ),
'typing_text' => esc_html__( 'Please enter %s or more characters', 'csf' ),
'searching_text' => esc_html__( 'Searching...', 'csf' ),
'no_results_text' => esc_html__( 'No results found.', 'csf' ),
),
) );
// Enqueue fields scripts and styles
$enqueued = array();
if ( ! empty( self::$fields ) ) {
foreach ( self::$fields as $field ) {
if ( ! empty( $field['type'] ) ) {
$classname = 'CSF_Field_' . $field['type'];
if ( class_exists( $classname ) && method_exists( $classname, 'enqueue' ) ) {
$instance = new $classname( $field );
if ( method_exists( $classname, 'enqueue' ) ) {
$instance->enqueue();
}
unset( $instance );
}
}
}
}
do_action( 'csf_enqueue' );
}
// Add typography enqueue styles to front page
public static function add_typography_enqueue_styles() {
if ( ! empty( self::$webfonts ) ) {
if ( ! empty( self::$webfonts['enqueue'] ) ) {
$query = array();
$fonts = array();
foreach ( self::$webfonts['enqueue'] as $family => $styles ) {
$fonts[] = $family . ( ( ! empty( $styles ) ) ? ':'. implode( ',', $styles ) : '' );
}
if ( ! empty( $fonts ) ) {
$query['family'] = implode( '%7C', $fonts );
}
if ( ! empty( self::$subsets ) ) {
$query['subset'] = implode( ',', self::$subsets );
}
$query['display'] = 'swap';
wp_enqueue_style( 'csf-google-web-fonts', esc_url( add_query_arg( $query, '//fonts.googleapis.com/css' ) ), array(), null );
}
if ( ! empty( self::$webfonts['async'] ) ) {
$fonts = array();
foreach ( self::$webfonts['async'] as $family => $styles ) {
$fonts[] = $family . ( ( ! empty( $styles ) ) ? ':'. implode( ',', $styles ) : '' );
}
wp_enqueue_script( 'csf-google-web-fonts', esc_url( '//ajax.googleapis.com/ajax/libs/webfont/1.6.26/webfont.js' ), array(), null );
wp_localize_script( 'csf-google-web-fonts', 'WebFontConfig', array( 'google' => array( 'families' => $fonts ) ) );
}
}
}
// Add admin body class
public static function add_admin_body_class( $classes ) {
if ( apply_filters( 'csf_fa4', false ) ) {
$classes .= 'csf-fa5-shims';
}
return $classes;
}
// Add custom css to front page
public static function add_custom_css() {
if ( ! empty( self::$css ) ) {
echo '<style type="text/css">'. wp_strip_all_tags( self::$css ) .'</style>';
}
}
// Add a new framework field
public static function field( $field = array(), $value = '', $unique = '', $where = '', $parent = '' ) {
// Check for unallow fields
if ( ! empty( $field['_notice'] ) ) {
$field_type = $field['type'];
$field = array();
$field['content'] = esc_html__( 'Oops! Not allowed.', 'csf' ) .' <strong>('. $field_type .')</strong>';
$field['type'] = 'notice';
$field['style'] = 'danger';
}
$depend = '';
$visible = '';
$unique = ( ! empty( $unique ) ) ? $unique : '';
$class = ( ! empty( $field['class'] ) ) ? ' ' . esc_attr( $field['class'] ) : '';
$is_pseudo = ( ! empty( $field['pseudo'] ) ) ? ' csf-pseudo-field' : '';
$field_type = ( ! empty( $field['type'] ) ) ? esc_attr( $field['type'] ) : '';
if ( ! empty( $field['dependency'] ) ) {
$dependency = $field['dependency'];
$depend_visible = '';
$data_controller = '';
$data_condition = '';
$data_value = '';
$data_global = '';
if ( is_array( $dependency[0] ) ) {
$data_controller = implode( '|', array_column( $dependency, 0 ) );
$data_condition = implode( '|', array_column( $dependency, 1 ) );
$data_value = implode( '|', array_column( $dependency, 2 ) );
$data_global = implode( '|', array_column( $dependency, 3 ) );
$depend_visible = implode( '|', array_column( $dependency, 4 ) );
} else {
$data_controller = ( ! empty( $dependency[0] ) ) ? $dependency[0] : '';
$data_condition = ( ! empty( $dependency[1] ) ) ? $dependency[1] : '';
$data_value = ( ! empty( $dependency[2] ) ) ? $dependency[2] : '';
$data_global = ( ! empty( $dependency[3] ) ) ? $dependency[3] : '';
$depend_visible = ( ! empty( $dependency[4] ) ) ? $dependency[4] : '';
}
$depend .= ' data-controller="'. esc_attr( $data_controller ) .'"';
$depend .= ' data-condition="'. esc_attr( $data_condition ) .'"';
$depend .= ' data-value="'. esc_attr( $data_value ) .'"';
$depend .= ( ! empty( $data_global ) ) ? ' data-depend-global="true"' : '';
$visible = ( ! empty( $depend_visible ) ) ? ' csf-depend-visible' : ' csf-depend-hidden';
}
if ( ! empty( $field_type ) ) {
// These attributes has been sanitized above.
echo '<div class="csf-field csf-field-'. $field_type . $is_pseudo . $class . $visible .'"'. $depend .'>';
if ( ! empty( $field['fancy_title'] ) ) {
echo '<div class="csf-fancy-title">' . $field['fancy_title'] .'</div>';
}
if ( ! empty( $field['title'] ) ) {
echo '<div class="csf-title">';
echo '<h4>'. $field['title'] .'</h4>';
echo ( ! empty( $field['subtitle'] ) ) ? '<div class="csf-subtitle-text">'. $field['subtitle'] .'</div>' : '';
echo '</div>';
}
echo ( ! empty( $field['title'] ) || ! empty( $field['fancy_title'] ) ) ? '<div class="csf-fieldset">' : '';
$value = ( ! isset( $value ) && isset( $field['default'] ) ) ? $field['default'] : $value;
$value = ( isset( $field['value'] ) ) ? $field['value'] : $value;
$classname = 'CSF_Field_'. $field_type;
if ( class_exists( $classname ) ) {
$instance = new $classname( $field, $value, $unique, $where, $parent );
$instance->render();
} else {
echo '<p>'. esc_html__( 'Field not found!', 'csf' ) .'</p>';
}
} else {
echo '<p>'. esc_html__( 'Field not found!', 'csf' ) .'</p>';
}
echo ( ! empty( $field['title'] ) || ! empty( $field['fancy_title'] ) ) ? '</div>' : '';
echo '<div class="clear"></div>';
echo '</div>';
}
}
}
CSF::init( __FILE__ );

View File

@@ -0,0 +1,335 @@
<?php if ( ! defined( 'ABSPATH' ) ) { die; } // Cannot access directly.
/**
*
* Shortcoder Class
*
* @since 1.0.0
* @version 1.0.0
*
*/
if ( ! class_exists( 'CSF_Shortcoder' ) ) {
class CSF_Shortcoder extends CSF_Abstract{
// constans
public $unique = '';
public $abstract = 'shortcoder';
public $blocks = array();
public $sections = array();
public $pre_tabs = array();
public $pre_sections = array();
public $args = array(
'button_title' => 'Add Shortcode',
'select_title' => 'Select a shortcode',
'insert_title' => 'Insert Shortcode',
'show_in_editor' => true,
'show_in_custom' => false,
'defaults' => array(),
'class' => '',
'gutenberg' => array(
'title' => 'CSF Shortcodes',
'description' => 'CSF Shortcode Block',
'icon' => 'screenoptions',
'category' => 'widgets',
'keywords' => array( 'shortcode', 'csf', 'insert' ),
'placeholder' => 'Write shortcode here...',
),
);
// run shortcode construct
public function __construct( $key, $params = array() ) {
$this->unique = $key;
$this->args = apply_filters( "csf_{$this->unique}_args", wp_parse_args( $params['args'], $this->args ), $this );
$this->sections = apply_filters( "csf_{$this->unique}_sections", $params['sections'], $this );
$this->pre_tabs = $this->pre_tabs( $this->sections );
$this->pre_sections = $this->pre_sections( $this->sections );
add_action( 'admin_footer', array( &$this, 'add_footer_modal_shortcode' ) );
add_action( 'customize_controls_print_footer_scripts', array( &$this, 'add_footer_modal_shortcode' ) );
add_action( 'wp_ajax_csf-get-shortcode-'. $this->unique, array( &$this, 'get_shortcode' ) );
if ( ! empty( $this->args['show_in_editor'] ) ) {
CSF::$shortcode_instances[$this->unique] = wp_parse_args( array( 'hash' => md5( $key ), 'modal_id' => $this->unique ), $this->args );
// elementor editor support
if ( CSF::is_active_plugin( 'elementor/elementor.php' ) ) {
add_action( 'elementor/editor/before_enqueue_scripts', array( 'CSF', 'add_admin_enqueue_scripts' ) );
add_action( 'elementor/editor/footer', array( 'CSF_Field_icon', 'add_footer_modal_icon' ) );
add_action( 'elementor/editor/footer', array( &$this, 'add_footer_modal_shortcode' ) );
}
}
}
// instance
public static function instance( $key, $params = array() ) {
return new self( $key, $params );
}
public function pre_tabs( $sections ) {
$result = array();
$parents = array();
$count = 100;
foreach ( $sections as $key => $section ) {
if ( ! empty( $section['parent'] ) ) {
$section['priority'] = ( isset( $section['priority'] ) ) ? $section['priority'] : $count;
$parents[$section['parent']][] = $section;
unset( $sections[$key] );
}
$count++;
}
foreach ( $sections as $key => $section ) {
$section['priority'] = ( isset( $section['priority'] ) ) ? $section['priority'] : $count;
if ( ! empty( $section['id'] ) && ! empty( $parents[$section['id']] ) ) {
$section['subs'] = wp_list_sort( $parents[$section['id']], array( 'priority' => 'ASC' ), 'ASC', true );
}
$result[] = $section;
$count++;
}
return wp_list_sort( $result, array( 'priority' => 'ASC' ), 'ASC', true );
}
public function pre_sections( $sections ) {
$result = array();
foreach ( $this->pre_tabs as $tab ) {
if ( ! empty( $tab['subs'] ) ) {
foreach ( $tab['subs'] as $sub ) {
$result[] = $sub;
}
}
if ( empty( $tab['subs'] ) ) {
$result[] = $tab;
}
}
return $result;
}
// get default value
public function get_default( $field ) {
$default = ( isset( $field['default'] ) ) ? $field['default'] : '';
$default = ( isset( $this->args['defaults'][$field['id']] ) ) ? $this->args['defaults'][$field['id']] : $default;
return $default;
}
public function add_footer_modal_shortcode() {
if( ! wp_script_is( 'csf' ) ) {
return;
}
$class = ( $this->args['class'] ) ? ' '. esc_attr( $this->args['class'] ) : '';
$has_select = ( count( $this->pre_tabs ) > 1 ) ? true : false;
$single_usage = ( ! $has_select ) ? ' csf-shortcode-single' : '';
$hide_header = ( ! $has_select ) ? ' hidden' : '';
?>
<div id="csf-modal-<?php echo esc_attr( $this->unique ); ?>" class="wp-core-ui csf-modal csf-shortcode hidden<?php echo esc_attr( $single_usage . $class ); ?>" data-modal-id="<?php echo esc_attr( $this->unique ); ?>" data-nonce="<?php echo esc_attr( wp_create_nonce( 'csf_shortcode_nonce' ) ); ?>">
<div class="csf-modal-table">
<div class="csf-modal-table-cell">
<div class="csf-modal-overlay"></div>
<div class="csf-modal-inner">
<div class="csf-modal-title">
<?php echo $this->args['button_title']; ?>
<div class="csf-modal-close"></div>
</div>
<?php
echo '<div class="csf-modal-header'. esc_attr( $hide_header ) .'">';
echo '<select>';
echo ( $has_select ) ? '<option value="">'. esc_attr( $this->args['select_title'] ) .'</option>' : '';
$tab_key = 1;
foreach ( $this->pre_tabs as $tab ) {
if ( ! empty( $tab['subs'] ) ) {
echo '<optgroup label="'. esc_attr( $tab['title'] ) .'">';
foreach ( $tab['subs'] as $sub ) {
$view = ( ! empty( $sub['view'] ) ) ? ' data-view="'. esc_attr( $sub['view'] ) .'"' : '';
$shortcode = ( ! empty( $sub['shortcode'] ) ) ? ' data-shortcode="'. esc_attr( $sub['shortcode'] ) .'"' : '';
$group = ( ! empty( $sub['group_shortcode'] ) ) ? ' data-group="'. esc_attr( $sub['group_shortcode'] ) .'"' : '';
echo '<option value="'. esc_attr( $tab_key ) .'"'. $view . $shortcode . $group .'>'. esc_attr( $sub['title'] ) .'</option>';
$tab_key++;
}
echo '</optgroup>' ;
} else {
$view = ( ! empty( $tab['view'] ) ) ? ' data-view="'. esc_attr( $tab['view'] ) .'"' : '';
$shortcode = ( ! empty( $tab['shortcode'] ) ) ? ' data-shortcode="'. esc_attr( $tab['shortcode'] ) .'"' : '';
$group = ( ! empty( $tab['group_shortcode'] ) ) ? ' data-group="'. esc_attr( $tab['group_shortcode'] ) .'"' : '';
echo '<option value="'. esc_attr( $tab_key ) .'"'. $view . $shortcode . $group .'>'. esc_attr( $tab['title'] ) .'</option>';
$tab_key++;
}
}
echo '</select>';
echo '</div>';
?>
<div class="csf-modal-content">
<div class="csf-modal-loading"><div class="csf-loading"></div></div>
<div class="csf-modal-load"></div>
</div>
<div class="csf-modal-insert-wrapper hidden"><a href="#" class="button button-primary csf-modal-insert"><?php echo $this->args['insert_title']; ?></a></div>
</div>
</div>
</div>
</div>
<?php
}
public function get_shortcode() {
ob_start();
$nonce = ( ! empty( $_POST[ 'nonce' ] ) ) ? sanitize_text_field( wp_unslash( $_POST[ 'nonce' ] ) ) : '';
$shortcode_key = ( ! empty( $_POST[ 'shortcode_key' ] ) ) ? sanitize_text_field( wp_unslash( $_POST[ 'shortcode_key' ] ) ) : '';
if ( ! empty( $shortcode_key ) && wp_verify_nonce( $nonce, 'csf_shortcode_nonce' ) ) {
$unallows = array( 'group', 'repeater', 'sorter' );
$section = $this->pre_sections[$shortcode_key-1];
$shortcode = ( ! empty( $section['shortcode'] ) ) ? $section['shortcode'] : '';
$view = ( ! empty( $section['view'] ) ) ? $section['view'] : 'normal';
if ( ! empty( $section ) ) {
//
// View: normal
if ( ! empty( $section['fields'] ) && $view !== 'repeater' ) {
echo '<div class="csf-fields">';
foreach ( $section['fields'] as $field ) {
if ( in_array( $field['type'], $unallows ) ) { $field['_notice'] = true; }
// Extra tag improves for spesific fields (border, spacing, dimensions etc...)
$field['tag_prefix'] = ( ! empty( $field['tag_prefix'] ) ) ? $field['tag_prefix'] .'_' : '';
$field_default = ( isset( $field['id'] ) ) ? $this->get_default( $field ) : '';
CSF::field( $field, $field_default, $shortcode, 'shortcode' );
}
echo '</div>';
}
//
// View: group and repeater fields
$repeatable_fields = ( $view === 'repeater' && ! empty( $section['fields'] ) ) ? $section['fields'] : array();
$repeatable_fields = ( $view === 'group' && ! empty( $section['group_fields'] ) ) ? $section['group_fields'] : $repeatable_fields;
if ( ! empty( $repeatable_fields ) ) {
$button_title = ( ! empty( $section['button_title'] ) ) ? ' '. $section['button_title'] : esc_html__( 'Add New', 'csf' );
$inner_shortcode = ( ! empty( $section['group_shortcode'] ) ) ? $section['group_shortcode'] : $shortcode;
echo '<div class="csf--repeatable">';
echo '<div class="csf--repeat-shortcode">';
echo '<div class="csf-repeat-remove fas fa-times"></div>';
echo '<div class="csf-fields">';
foreach ( $repeatable_fields as $field ) {
if ( in_array( $field['type'], $unallows ) ) { $field['_notice'] = true; }
// Extra tag improves for spesific fields (border, spacing, dimensions etc...)
$field['tag_prefix'] = ( ! empty( $field['tag_prefix'] ) ) ? $field['tag_prefix'] .'_' : '';
$field_default = ( isset( $field['id'] ) ) ? $this->get_default( $field ) : '';
CSF::field( $field, $field_default, $inner_shortcode.'[0]', 'shortcode' );
}
echo '</div>';
echo '</div>';
echo '</div>';
echo '<div class="csf--repeat-button-block"><a class="button csf--repeat-button" href="#"><i class="fas fa-plus-circle"></i> '. $button_title .'</a></div>';
}
}
} else {
echo '<div class="csf-field csf-error-text">'. esc_html__( 'Error: Invalid nonce verification.', 'csf' ) .'</div>';
}
wp_send_json_success( array( 'content' => ob_get_clean() ) );
}
// Once editor setup for gutenberg and media buttons
public static function once_editor_setup() {
if ( function_exists( 'register_block_type' ) ) {
add_action( 'enqueue_block_editor_assets', array( 'CSF_Shortcoder', 'add_guteberg_blocks' ) );
}
if ( csf_wp_editor_api() ) {
add_action( 'media_buttons', array( 'CSF_Shortcoder', 'add_media_buttons' ) );
}
}
// Add gutenberg blocks.
public static function add_guteberg_blocks() {
wp_enqueue_script( 'csf-gutenberg-block', CSF::include_plugin_url( 'assets/js/gutenberg.js' ), array( 'wp-blocks', 'wp-editor', 'wp-element', 'wp-components' ) );
wp_localize_script( 'csf-gutenberg-block', 'csf_gutenberg_blocks', CSF::$shortcode_instances );
foreach ( CSF::$shortcode_instances as $value ) {
register_block_type( 'csf-gutenberg-block/block-'. $value['hash'], array(
'editor_script' => 'csf-gutenberg-block',
) );
}
}
// Add media buttons
public static function add_media_buttons( $editor_id ) {
foreach ( CSF::$shortcode_instances as $value ) {
echo '<a href="#" class="button button-primary csf-shortcode-button" data-editor-id="'. esc_attr( $editor_id ) .'" data-modal-id="'. esc_attr( $value['modal_id'] ) .'">'. $value['button_title'] .'</a>';
}
}
}
}

View File

@@ -0,0 +1,281 @@
<?php if ( ! defined( 'ABSPATH' ) ) { die; } // Cannot access directly.
/**
*
* Taxonomy Options Class
*
* @since 1.0.0
* @version 1.0.0
*
*/
if ( ! class_exists( 'CSF_Taxonomy_Options' ) ) {
class CSF_Taxonomy_Options extends CSF_Abstract{
// constans
public $unique = '';
public $taxonomy = '';
public $abstract = 'taxonomy';
public $pre_fields = array();
public $sections = array();
public $taxonomies = array();
public $args = array(
'taxonomy' => 'category',
'data_type' => 'serialize',
'class' => '',
'enqueue_webfont' => true,
'async_webfont' => false,
'output_css' => true,
'defaults' => array(),
);
// run taxonomy construct
public function __construct( $key, $params ) {
$this->unique = $key;
$this->args = apply_filters( "csf_{$this->unique}_args", wp_parse_args( $params['args'], $this->args ), $this );
$this->sections = apply_filters( "csf_{$this->unique}_sections", $params['sections'], $this );
$this->taxonomies = ( is_array( $this->args['taxonomy'] ) ) ? $this->args['taxonomy'] : array_filter( (array) $this->args['taxonomy'] );
$this->taxonomy = ( ! empty( $_REQUEST[ 'taxonomy' ] ) ) ? sanitize_text_field( wp_unslash( $_REQUEST[ 'taxonomy' ] ) ) : '';
$this->pre_fields = $this->pre_fields( $this->sections );
if ( ! empty( $this->taxonomies ) && in_array( $this->taxonomy, $this->taxonomies ) ) {
add_action( 'admin_init', array( &$this, 'add_taxonomy_options' ) );
}
// wp enqeueu for typography and output css
parent::__construct();
}
// instance
public static function instance( $key, $params ) {
return new self( $key, $params );
}
public function pre_fields( $sections ) {
$result = array();
foreach ( $sections as $key => $section ) {
if ( ! empty( $section['fields'] ) ) {
foreach ( $section['fields'] as $field ) {
$result[] = $field;
}
}
}
return $result;
}
// add taxonomy add/edit fields
public function add_taxonomy_options() {
add_action( $this->taxonomy .'_add_form_fields', array( &$this, 'render_taxonomy_form_fields' ) );
add_action( $this->taxonomy .'_edit_form', array( &$this, 'render_taxonomy_form_fields' ) );
add_action( 'created_'. $this->taxonomy, array( &$this, 'save_taxonomy' ) );
add_action( 'edited_'. $this->taxonomy, array( &$this, 'save_taxonomy' ) );
}
// get default value
public function get_default( $field ) {
$default = ( isset( $field['default'] ) ) ? $field['default'] : '';
$default = ( isset( $this->args['defaults'][$field['id']] ) ) ? $this->args['defaults'][$field['id']] : $default;
return $default;
}
// get meta value
public function get_meta_value( $field, $term_id = null ) {
$value = null;
$term_id = ( ! isset( $term_id ) ) ? get_queried_object_id() : $term_id;
if ( ! empty( $term_id ) && ! empty( $field['id'] ) ) {
if ( $this->args['data_type'] !== 'serialize' ) {
$meta = get_term_meta( $term_id, $field['id'] );
$value = ( isset( $meta[0] ) ) ? $meta[0] : null;
} else {
$meta = get_term_meta( $term_id, $this->unique, true );
$value = ( isset( $meta[$field['id']] ) ) ? $meta[$field['id']] : null;
}
}
$default = ( isset( $field['id'] ) ) ? $this->get_default( $field ) : '';
$value = ( isset( $value ) ) ? $value : $default;
return $value;
}
// render taxonomy add/edit form fields
public function render_taxonomy_form_fields( $term ) {
$is_term = ( is_object( $term ) && isset( $term->taxonomy ) ) ? true : false;
$term_id = ( $is_term ) ? $term->term_id : 0;
$taxonomy = ( $is_term ) ? $term->taxonomy : $term;
$classname = ( $is_term ) ? 'edit' : 'add';
$errors = ( ! empty( $term_id ) ) ? get_term_meta( $term_id, '_csf_errors_'. $this->unique, true ) : array();
$errors = ( ! empty( $errors ) ) ? $errors : array();
$class = ( $this->args['class'] ) ? ' '. $this->args['class'] : '';
if ( ! empty( $errors ) ) {
delete_term_meta( $term_id, '_csf_errors_'. $this->unique );
}
wp_nonce_field( 'csf_taxonomy_nonce', 'csf_taxonomy_nonce'. $this->unique );
echo '<div class="csf csf-taxonomy csf-show-all csf-onload csf-taxonomy-'. esc_attr( $classname ) .'-fields '. esc_attr( $class ) .'">';
foreach ( $this->sections as $section ) {
if ( $taxonomy === $this->taxonomy ) {
$section_icon = ( ! empty( $section['icon'] ) ) ? '<i class="csf-section-icon '. esc_attr( $section['icon'] ) .'"></i>' : '';
$section_title = ( ! empty( $section['title'] ) ) ? $section['title'] : '';
echo ( $section_title || $section_icon ) ? '<div class="csf-section-title"><h3>'. $section_icon . $section_title .'</h3></div>' : '';
if ( ! empty( $section['fields'] ) ) {
foreach ( $section['fields'] as $field ) {
if ( ! empty( $field['id'] ) && ! empty( $errors['fields'][$field['id']] ) ) {
$field['_error'] = $errors['fields'][$field['id']];
}
if ( ! empty( $field['id'] ) ) {
$field['default'] = $this->get_default( $field );
}
CSF::field( $field, $this->get_meta_value( $field, $term_id ), $this->unique, 'taxonomy' );
}
}
}
}
echo '</div>';
}
// save taxonomy form fields
public function save_taxonomy( $term_id ) {
$count = 1;
$data = array();
$errors = array();
$noncekey = 'csf_taxonomy_nonce'. $this->unique;
$nonce = ( ! empty( $_POST[ $noncekey ] ) ) ? sanitize_text_field( wp_unslash( $_POST[ $noncekey ] ) ) : '';
$taxonomy = ( ! empty( $_POST[ 'taxonomy' ] ) ) ? sanitize_text_field( wp_unslash( $_POST[ 'taxonomy' ] ) ) : '';
if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || ! wp_verify_nonce( $nonce, 'csf_taxonomy_nonce' ) ) {
return $term_id;
}
// XSS ok.
// No worries, This "POST" requests is sanitizing in the below foreach.
$request = ( ! empty( $_POST[ $this->unique ] ) ) ? $_POST[ $this->unique ] : array();
if ( ! empty( $request ) ) {
foreach ( $this->sections as $section ) {
if ( ! empty( $section['fields'] ) ) {
foreach ( $section['fields'] as $field ) {
if ( ! empty( $field['id'] ) ) {
$field_id = $field['id'];
$field_value = isset( $request[$field_id] ) ? $request[$field_id] : '';
// Sanitize "post" request of field.
if ( ! isset( $field['sanitize'] ) ) {
if( is_array( $field_value ) ) {
$data[$field_id] = wp_kses_post_deep( $field_value );
} else {
$data[$field_id] = wp_kses_post( $field_value );
}
} else if( isset( $field['sanitize'] ) && is_callable( $field['sanitize'] ) ) {
$data[$field_id] = call_user_func( $field['sanitize'], $field_value );
} else {
$data[$field_id] = $field_value;
}
// Validate "post" request of field.
if ( isset( $field['validate'] ) && is_callable( $field['validate'] ) ) {
$has_validated = call_user_func( $field['validate'], $field_value );
if ( ! empty( $has_validated ) ) {
$errors['sections'][$count] = true;
$errors['fields'][$field_id] = $has_validated;
$data[$field_id] = $this->get_meta_value( $field, $term_id );
}
}
}
}
}
$count++;
}
}
$data = apply_filters( "csf_{$this->unique}_save", $data, $term_id, $this );
do_action( "csf_{$this->unique}_save_before", $data, $term_id, $this );
if ( empty( $data ) ) {
if ( $this->args['data_type'] !== 'serialize' ) {
foreach ( $data as $key => $value ) {
delete_term_meta( $term_id, $key );
}
} else {
delete_term_meta( $term_id, $this->unique );
}
} else {
if ( $this->args['data_type'] !== 'serialize' ) {
foreach ( $data as $key => $value ) {
update_term_meta( $term_id, $key, $value );
}
} else {
update_term_meta( $term_id, $this->unique, $data );
}
if ( ! empty( $errors ) ) {
update_term_meta( $term_id, '_csf_errors_'. $this->unique, $errors );
}
}
do_action( "csf_{$this->unique}_saved", $data, $term_id, $this );
do_action( "csf_{$this->unique}_save_after", $data, $term_id, $this );
}
}
}

View File

@@ -0,0 +1,138 @@
<?php if ( ! defined( 'ABSPATH' ) ) { die; } // Cannot access directly.
/**
*
* Widgets Class
*
* @since 1.0.0
* @version 1.0.0
*
*/
if ( ! class_exists( 'CSF_Widget' ) ) {
class CSF_Widget extends WP_Widget {
// constans
public $unique = '';
public $args = array(
'title' => '',
'classname' => '',
'description' => '',
'width' => '',
'class' => '',
'fields' => array(),
'defaults' => array(),
);
public function __construct( $key, $params ) {
$widget_ops = array();
$control_ops = array();
$this->unique = $key;
$this->args = apply_filters( "csf_{$this->unique}_args", wp_parse_args( $params, $this->args ), $this );
// Set control options
if ( ! empty( $this->args['width'] ) ) {
$control_ops['width'] = esc_attr( $this->args['width'] );
}
// Set widget options
if ( ! empty( $this->args['description'] ) ) {
$widget_ops['description'] = esc_attr( $this->args['description'] );
}
if ( ! empty( $this->args['classname'] ) ) {
$widget_ops['classname'] = esc_attr( $this->args['classname'] );
}
// Set filters
$widget_ops = apply_filters( "csf_{$this->unique}_widget_ops", $widget_ops, $this );
$control_ops = apply_filters( "csf_{$this->unique}_control_ops", $control_ops, $this );
parent::__construct( $this->unique, esc_attr( $this->args['title'] ), $widget_ops, $control_ops );
}
// Register widget with WordPress
public static function instance( $key, $params = array() ) {
return new self( $key, $params );
}
// Front-end display of widget.
public function widget( $args, $instance ) {
call_user_func( $this->unique, $args, $instance );
}
// get default value
public function get_default( $field ) {
$default = ( isset( $field['default'] ) ) ? $field['default'] : '';
$default = ( isset( $this->args['defaults'][$field['id']] ) ) ? $this->args['defaults'][$field['id']] : $default;
return $default;
}
// get widget value
public function get_widget_value( $instance, $field ) {
$default = ( isset( $field['id'] ) ) ? $this->get_default( $field ) : '';
$value = ( isset( $field['id'] ) && isset( $instance[$field['id']] ) ) ? $instance[$field['id']] : $default;
return $value;
}
// Back-end widget form.
public function form( $instance ) {
if ( ! empty( $this->args['fields'] ) ) {
$class = ( $this->args['class'] ) ? ' '. $this->args['class'] : '';
echo '<div class="csf csf-widgets csf-fields'. esc_attr( $class ) .'">';
foreach ( $this->args['fields'] as $field ) {
$field_unique = '';
if ( ! empty( $field['id'] ) ) {
$field_unique = 'widget-' . $this->unique . '[' . $this->number . ']';
if ( $field['id'] === 'title' ) {
$field['attributes']['id'] = 'widget-'. $this->unique .'-'. $this->number .'-title';
}
$field['default'] = $this->get_default( $field );
}
CSF::field( $field, $this->get_widget_value( $instance, $field ), $field_unique );
}
echo '</div>';
}
}
// Sanitize widget form values as they are saved.
public function update( $new_instance, $old_instance ) {
// auto sanitize
foreach ( $this->args['fields'] as $field ) {
if ( ! empty( $field['id'] ) && ( ! isset( $new_instance[$field['id']] ) || is_null( $new_instance[$field['id']] ) ) ) {
$new_instance[$field['id']] = '';
}
}
$new_instance = apply_filters( "csf_{$this->unique}_save", $new_instance, $this->args, $this );
do_action( "csf_{$this->unique}_save_before", $new_instance, $this->args, $this );
return $new_instance;
}
}
}