Skip to content

Instantly share code, notes, and snippets.

@merianos
Last active December 3, 2022 06:54
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save merianos/4a8b994c235774d6db33ad38c06c6b51 to your computer and use it in GitHub Desktop.
Save merianos/4a8b994c235774d6db33ad38c06c6b51 to your computer and use it in GitHub Desktop.
WordPress Taxonomies / Users MetaBox using OptionTree
<?php
<?php
require_once('OtUsersMetabox.php');
add_action( 'admin_init', array( $this, 'users_meta_boxs' ) );
function users_meta_boxs() {
OtUsersMetabox::build(
[
'id' => 'general_meta_box',
'title' => __( 'General Meta Box' ),
'fields' => [
[
'label' => __( 'First Tab' ),
'id' => 'first_tab',
'type' => 'tab'
],
[
'label' => __( 'On/Off Option' ),
'id' => 'on_off_option',
'type' => 'on-off',
'std' => 'off',
],
[
'label' => __( 'Second Tab' ),
'id' => 'second_tab',
'type' => 'tab'
],
[
'label' => __( 'Link Hover Color' ),
'id' => 'text_color_hover',
'type' => 'select',
'std' => 'black',
'choices' => [
[
'label' => __( 'White' ),
'value' => 'white'
],
[
'label' => __( 'Black' ),
'value' => 'black'
],
]
],
]
]
);
OtUsersMetabox::build(
[
'id' => 'social_meta_box',
'title' => __( 'Social Meta Box' ),
'taxonomies' => [
'custom_taxonomy'
],
'fields' => [
[
'label' => __( 'Display Social' ),
'id' => 'displa_social',
'type' => 'on-off',
'std' => 'off',
],
]
]
);
}
<?php
require_once('OtTaxonomiesMetabox.php');
add_action( 'admin_init', array( $this, 'taxonomies_meta_boxs' ) );
function taxonomies_meta_boxs() {
OtTaxonomiesMetabox::build(
[
'id' => 'general_meta_box',
'title' => __( 'General Meta Box' ),
'taxonomies' => [
'category',
'post_tag',
'custom_taxonomy'
],
'fields' => [
[
'label' => __( 'First Tab' ),
'id' => 'first_tab',
'type' => 'tab'
],
[
'label' => __( 'On/Off Option' ),
'id' => 'on_off_option',
'type' => 'on-off',
'std' => 'off',
],
[
'label' => __( 'Second Tab' ),
'id' => 'second_tab',
'type' => 'tab'
],
[
'label' => __( 'Link Hover Color' ),
'id' => 'text_color_hover',
'type' => 'select',
'std' => 'black',
'choices' => [
[
'label' => __( 'White' ),
'value' => 'white'
],
[
'label' => __( 'Black' ),
'value' => 'black'
],
]
],
]
]
);
OtTaxonomiesMetabox::build(
[
'id' => 'social_meta_box',
'title' => __( 'Social Meta Box' ),
'taxonomies' => [
'custom_taxonomy'
],
'fields' => [
[
'label' => __( 'Display Social' ),
'id' => 'displa_social',
'type' => 'on-off',
'std' => 'off',
],
]
]
);
}
<?php
/**
* Created by PhpStorm.
* User: merianos
* Date: 6/7/2018
* Time: 8:39 μμ
*/
class OtTaxonomiesMetabox {
private $metabox_settings = [];
/**
* Responsible to build a theme options meta box for taxonomies.
*
* @param array $args
*
* @return null
*/
public static function build( array $args = [] ) {
if ( empty( $args ) ) {
return null;
}
return new self( $args );
}
/**
* OtTaxonomiesMetabox constructor.
*
* @param array $args
*/
public function __construct( array $args = [] ) {
if ( ! is_admin() ) {
return;
}
global $ot_meta_boxes;
if ( ! isset( $ot_meta_boxes ) ) {
$ot_meta_boxes = [];
}
$ot_meta_boxes[] = $args;
$this->metabox_settings = $args;
add_action( 'admin_enqueue_scripts', [ $this, 'load_assets' ] );
foreach ( $this->metabox_settings['taxonomies'] as $taxonomy ) {
add_action( $taxonomy . '_edit_form_fields', [ $this, 'register_taxonomy_meta_fields' ], 100 );
add_action( 'edited_' . $taxonomy, [ $this, 'save_taxonomy_meta' ] );
}
}
/**
* Responsible to load the Theme Options Assets
*/
public function load_assets() {
if ( ! isset( $_GET['taxonomy'] ) || ! in_array( $_GET['taxonomy'], $this->metabox_settings['taxonomies'] ) ) {
return;
}
$this->load_scripts();
$this->load_styles();
}
/**
* Responsible to build and inject the fields based on the user settings.
*
* @param $term
*
* @return bool
*/
public function register_taxonomy_meta_fields( $term ) {
if (
'string' === gettype( $term ) &&
! in_array( $term, $this->metabox_settings['taxonomies'] )
) {
return false;
}
if (
'object' === gettype( $term ) &&
! in_array( $term->taxonomy, $this->metabox_settings['taxonomies'] )
) {
return false;
}
$add_term = 'string' === gettype( $term );
$edit_term = 'object' === gettype( $term );
if ( $add_term ) {
?>
<div class="form-field">
<?php
}
if ( $edit_term ) {
?>
<tr class="form-field">
<th scope="row" valign="top"></th>
<td>
<?php
}
?>
<div class="postbox-container" style="min-width:calc( 100% - 29px );">
<div id="normal-sortables" class="meta-box-sortables ui-sortable">
<div id="theme_options" class="postbox">
<h2 class="hndle" style="padding:12px;margin:0;cursor:default;">
<span>
Theme Options
</span>
</h2>
<div class="inside">
<div class="ot-metabox-wrapper">
<input
type="hidden"
name="<?php echo esc_attr( $this->metabox_settings['id'] ); ?>_nonce"
value="<?php echo esc_attr( wp_create_nonce( $this->metabox_settings['id'] ) ); ?>"
/>
<?php
if (
isset( $this->metabox_settings['desc'] ) &&
! empty( $this->metabox_settings['desc'] )
) {
?>
<div class="description" style="padding-top:10px;">
<?php
echo htmlspecialchars_decode( $this->metabox_settings['desc'] );
?>
</div>
<?php
}
foreach ( $this->metabox_settings['fields'] as $field ) {
/* get current term meta data */
$field_value = '';
if ( $edit_term ) {
$field_value = get_term_meta( $term->term_id, $field['id'], true );
}
/* set standard value */
if ( isset( $field['std'] ) ) {
$field_value = ot_filter_std_value( $field_value, $field['std'] );
}
/* build the arguments array */
$_args = array(
'type' => $field['type'],
'field_id' => $field['id'],
'field_name' => $field['id'],
'field_value' => $field_value,
'field_desc' => isset( $field['desc'] ) ? $field['desc'] : '',
'field_std' => isset( $field['std'] ) ? $field['std'] : '',
'field_rows' => isset( $field['rows'] ) && ! empty( $field['rows'] ) ? $field['rows'] : 10,
'field_post_type' => isset( $field['post_type'] ) && ! empty( $field['post_type'] ) ? $field['post_type'] : 'post',
'field_taxonomy' => isset( $field['taxonomy'] ) && ! empty( $field['taxonomy'] ) ? $field['taxonomy'] : 'category',
'field_min_max_step' => isset( $field['min_max_step'] ) && ! empty( $field['min_max_step'] ) ? $field['min_max_step'] : '0,100,1',
'field_class' => isset( $field['class'] ) ? $field['class'] : '',
'field_condition' => isset( $field['condition'] ) ? $field['condition'] : '',
'field_operator' => isset( $field['operator'] ) ? $field['operator'] : 'and',
'field_choices' => isset( $field['choices'] ) ? $field['choices'] : [],
'field_settings' => isset( $field['settings'] ) && ! empty( $field['settings'] ) ? $field['settings'] : [],
'post_id' => $add_term ? 0 : $term->term_id,
'meta' => true
);
$conditions = '';
/* setup the conditions */
if ( isset( $field['condition'] ) && ! empty( $field['condition'] ) ) {
$conditions = ' data-condition="' . $field['condition'] . '"';
$conditions .= isset( $field['operator'] ) && in_array( $field['operator'], [
'and',
'AND',
'or',
'OR'
] ) ? ' data-operator="' . $field['operator'] . '"' : '';
}
/* only allow simple textarea due to DOM issues with wp_editor() */
if (
apply_filters(
'ot_override_forced_textarea_simple',
false,
$field['id']
) == false &&
$_args['type'] == 'textarea'
) {
$_args['type'] = 'textarea-simple';
}
// Build the setting CSS class
if ( ! empty( $_args['field_class'] ) ) {
$classes = explode( ' ', $_args['field_class'] );
foreach ( $classes as $key => $value ) {
$classes[ $key ] = $value . '-wrap';
}
$class = 'format-settings ' . implode( ' ', $classes );
} else {
$class = 'format-settings';
}
?>
<div
id="setting_<?php echo esc_attr( $field['id'] ) ?>"
class="<?php echo esc_attr( $class ); ?>"
<?php echo esc_attr( $conditions ) ?>
>
<div class="format-setting-wrap">
<?php
/* don't show title with textblocks */
if (
$_args['type'] !== 'textblock' &&
! empty( $field['label'] )
) {
?>
<div class="format-setting-label">
<label for="<?php echo esc_attr( $field['id'] ); ?>" class="label">
<?php echo $field['label']; ?>
</label>
</div>
<?php
}
echo ot_display_by_type( $_args );
?>
</div>
</div>
<?php
}
?>
<div class="clear"></div>
</div>
</div>
</div>
</div>
</div>
<?php
if ( $add_term ) {
?>
</div>
<?php
}
if ( $edit_term ) {
?>
</td>
</tr>
<?php
}
}
/**
* Responsible to save the taxonomy meta
*
* @param int $term_id
*
* @return int
*/
public function save_taxonomy_meta( int $term_id = 0 ) {
global $pagenow;
/* don't save if $_POST is empty */
if (
empty( $_POST ) ||
(
isset( $_POST['vc_inline'] ) &&
$_POST['vc_inline'] == true
)
) {
return $term_id;
}
/* don't save during quick edit */
if ( $pagenow == 'admin-ajax.php' ) {
return $term_id;
}
/* don't save during autosave */
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $term_id;
}
/* verify nonce */
if (
isset( $_POST[ $this->metabox_settings['id'] . '_nonce' ] ) &&
! wp_verify_nonce(
$_POST[ $this->metabox_settings['id'] . '_nonce' ],
$this->metabox_settings['id'] )
) {
return $term_id;
}
// todo-merianos: I have to check if it's possible to have permissions on who can edit / update a taxonomy term
foreach ( $this->metabox_settings['fields'] as $field ) {
$old = get_term_meta( $term_id, $field['id'], true );
$new = '';
/* there is data to validate */
if ( isset( $_POST[ $field['id'] ] ) ) {
/* slider and list item */
if ( in_array( $field['type'], [ 'list-item', 'slider' ] ) ) {
/* required title setting */
$required_setting = [
[
'id' => 'title',
'label' => __( 'Title', 'option-tree' ),
'desc' => '',
'std' => '',
'type' => 'text',
'rows' => '',
'class' => 'option-tree-setting-title',
'post_type' => '',
'choices' => []
]
];
/* get the settings array */
$settings = isset( $_POST[ $field['id'] . '_settings_array' ] ) ? unserialize( ot_decode( $_POST[ $field['id'] . '_settings_array' ] ) ) : [];
/* settings are empty for some odd ass reason get the defaults */
if ( empty( $settings ) ) {
$settings = 'slider' == $field['type'] ? ot_slider_settings( $field['id'] ) : ot_list_item_settings( $field['id'] );
}
/* merge the two settings array */
$settings = array_merge( $required_setting, $settings );
foreach ( $_POST[ $field['id'] ] as $k => $setting_array ) {
foreach ( $settings as $sub_setting ) {
/* verify sub setting has a type & value */
if ( isset( $sub_setting['type'] ) && isset( $_POST[ $field['id'] ][ $k ][ $sub_setting['id'] ] ) ) {
$_POST[ $field['id'] ][ $k ][ $sub_setting['id'] ] = ot_validate_setting( $_POST[ $field['id'] ][ $k ][ $sub_setting['id'] ], $sub_setting['type'], $sub_setting['id'] );
}
}
}
/* set up new data with validated data */
$new = $_POST[ $field['id'] ];
} else if ( $field['type'] == 'social-links' ) {
/* get the settings array */
$settings = isset( $_POST[ $field['id'] . '_settings_array' ] ) ? unserialize( ot_decode( $_POST[ $field['id'] . '_settings_array' ] ) ) : [];
/* settings are empty get the defaults */
if ( empty( $settings ) ) {
$settings = ot_social_links_settings( $field['id'] );
}
foreach ( $_POST[ $field['id'] ] as $k => $setting_array ) {
foreach ( $settings as $sub_setting ) {
/* verify sub setting has a type & value */
if ( isset( $sub_setting['type'] ) && isset( $_POST[ $field['id'] ][ $k ][ $sub_setting['id'] ] ) ) {
$_POST[ $field['id'] ][ $k ][ $sub_setting['id'] ] = ot_validate_setting( $_POST[ $field['id'] ][ $k ][ $sub_setting['id'] ], $sub_setting['type'], $sub_setting['id'] );
}
}
}
/* set up new data with validated data */
$new = $_POST[ $field['id'] ];
} else {
/* run through validattion */
$new = ot_validate_setting( $_POST[ $field['id'] ], $field['type'], $field['id'] );
}
/* insert CSS */
if ( $field['type'] == 'css' ) {
/* insert CSS into dynamic.css */
if ( '' !== $new ) {
ot_insert_css_with_markers( $field['id'], $new, true );
/* remove old CSS from dynamic.css */
} else {
ot_remove_old_css( $field['id'] );
}
}
}
if ( isset( $new ) && $new !== $old ) {
update_term_meta( $term_id, $field['id'], $new );
} else if ( '' == $new && $old ) {
delete_term_meta( $term_id, $field['id'], $old );
}
}
return $term_id;
}
/**
* Responsible to load the required JS for the Theme Options in the taxonomies pages.
*/
protected function load_scripts() {
/* execute scripts before actions */
do_action( 'ot_admin_scripts_before' );
if ( function_exists( 'wp_enqueue_media' ) ) {
/* WP 3.5 Media Uploader */
wp_enqueue_media();
} else {
/* Legacy Thickbox */
add_thickbox();
}
/* load jQuery-ui slider */
wp_enqueue_script( 'jquery-ui-slider' );
/* load jQuery-ui datepicker */
wp_enqueue_script( 'jquery-ui-datepicker' );
/* load WP colorpicker */
wp_enqueue_script( 'wp-color-picker' );
/* load Ace Editor for CSS Editing */
wp_enqueue_script(
'ace-editor',
'https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/ace.js',
null,
'1.1.3'
);
/* load jQuery UI timepicker addon */
wp_enqueue_script(
'jquery-ui-timepicker',
OT_URL . 'assets/js/vendor/jquery/jquery-ui-timepicker.js',
[ 'jquery', 'jquery-ui-slider', 'jquery-ui-datepicker' ],
'1.4.3'
);
/* load the post formats */
if ( OT_META_BOXES == true && OT_POST_FORMATS == true ) {
wp_enqueue_script(
'ot-postformats',
OT_URL . 'assets/js/ot-postformats.js',
[ 'jquery' ],
'1.0.1'
);
}
/* load all the required scripts */
wp_enqueue_script(
'ot-admin-js',
OT_URL . 'assets/js/ot-admin.js',
[
'jquery',
'jquery-ui-tabs',
'jquery-ui-sortable',
'jquery-ui-slider',
'wp-color-picker',
'ace-editor',
'jquery-ui-datepicker',
'jquery-ui-timepicker',
],
OT_VERSION
);
/* create localized JS array */
$localized_array = array(
'ajax' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'option_tree' ),
'upload_text' => apply_filters( 'ot_upload_text',
__( 'Send to OptionTree', 'option-tree' ) ),
'remove_media_text' => __( 'Remove Media', 'option-tree' ),
'reset_agree' => __( 'Are you sure you want to reset back to the defaults?',
'option-tree' ),
'remove_no' => __( 'You can\'t remove this! But you can edit the values.',
'option-tree' ),
'remove_agree' => __( 'Are you sure you want to remove this?', 'option-tree' ),
'activate_layout_agree' => __( 'Are you sure you want to activate this layout?',
'option-tree' ),
'setting_limit' => __( 'Sorry, you can\'t have settings three levels deep.',
'option-tree' ),
'delete' => __( 'Delete Gallery', 'option-tree' ),
'edit' => __( 'Edit Gallery', 'option-tree' ),
'create' => __( 'Create Gallery', 'option-tree' ),
'confirm' => __( 'Are you sure you want to delete this Gallery?',
'option-tree' ),
'date_current' => __( 'Today', 'option-tree' ),
'date_time_current' => __( 'Now', 'option-tree' ),
'date_close' => __( 'Close', 'option-tree' ),
'replace' => __( 'Featured Image', 'option-tree' ),
'with' => __( 'Image', 'option-tree' ),
);
/* localized script attached to 'option_tree' */
wp_localize_script( 'ot-admin-js', 'option_tree', $localized_array );
/* execute scripts after actions */
do_action( 'ot_admin_scripts_after' );
}
/**
* Responsible to load the required CSS for the Theme Options in the taxonomies pages.
*/
protected function load_styles() {
global $wp_styles;
/* execute styles before actions */
do_action( 'ce_admin_styles_before' );
/* load WP colorpicker */
wp_enqueue_style( 'wp-color-picker' );
/* load admin styles */
wp_enqueue_style( 'ot-admin-css', OT_URL . 'assets/css/ot-admin.css', false, OT_VERSION );
/* load the RTL stylesheet */
$wp_styles->add_data( 'ot-admin-css', 'rtl', true );
/**
* Filter the screen IDs used to dequeue `jquery-ui-css`.
*
* @since 2.5.0
*
* @param array $screen_ids An array of screen IDs.
*/
$screen_ids = apply_filters(
'ot_dequeue_jquery_ui_css_screen_ids',
[
'toplevel_page_ot-settings',
'optiontree_page_ot-documentation',
'appearance_page_ot-theme-options',
]
);
/* Remove styles added by the WP Review plugin and any custom pages added through filtering */
if ( in_array( get_current_screen()->id, $screen_ids ) ) {
wp_dequeue_style( 'plugin_name-admin-ui-css' );
wp_dequeue_style( 'jquery-ui-css' );
}
/* execute styles after actions */
do_action( 'ce_admin_styles_after' );
}
}
<?php
/**
* Created by PhpStorm.
* User: merianos
* Date: 6/7/2018
* Time: 8:39 μμ
*/
use WP_User;
class OtUsersMetabox {
private $metabox_settings = [];
/**
* Responsible to build a theme options meta box for taxonomies.
*
* @param array $args
*
* @return null
*/
public static function build( array $args = [] ) {
if ( empty( $args ) ) {
return null;
}
return new self( $args );
}
/**
* OtTaxonomiesMetabox constructor.
*
* @param array $args
*/
public function __construct( array $args = [] ) {
if ( ! is_admin() ) {
return;
}
global $ot_meta_boxes;
if ( ! isset( $ot_meta_boxes ) ) {
$ot_meta_boxes = [];
}
$ot_meta_boxes[] = $args;
$this->metabox_settings = $args;
add_action( 'admin_enqueue_scripts', [ $this, 'load_assets' ] );
add_action( 'show_user_profile', [ $this, 'register_taxonomy_meta_fields' ], 100 );
add_action( 'edit_user_profile', [ $this, 'register_taxonomy_meta_fields' ], 100 );
add_action( 'personal_options_update', [ $this, 'save_user_meta' ] );
add_action( 'edit_user_profile_update', [ $this, 'save_user_meta' ] );
}
/**
* Responsible to load the Theme Options Assets
*/
public function load_assets() {
global $pagenow;
if ( ! in_array( $pagenow, [ 'profile.php', 'user-edit.php' ] ) ) {
return;
}
$this->load_scripts();
$this->load_styles();
}
/**
* Responsible to build and inject the fields based on the user settings.
*
* @param $user
*/
public function register_taxonomy_meta_fields( WP_User $user = null ) {
?>
<table class="form-table">
<tr>
<th></th>
<td>
<div class="postbox-container" style="min-width:calc( 100% - 29px );">
<div id="normal-sortables" class="meta-box-sortables ui-sortable">
<div id="theme_options" class="postbox">
<h2 class="hndle" style="padding:12px;margin:0;cursor:default;">
<span>
Theme Options
</span>
</h2>
<div class="inside">
<div class="ot-metabox-wrapper">
<input
type="hidden"
name="<?php echo esc_attr( $this->metabox_settings['id'] ); ?>_nonce"
value="<?php echo esc_attr( wp_create_nonce( $this->metabox_settings['id'] ) ); ?>"
/>
<?php
if (
isset( $this->metabox_settings['desc'] ) &&
! empty( $this->metabox_settings['desc'] )
) {
?>
<div class="description" style="padding-top:10px;">
<?php
echo htmlspecialchars_decode( $this->metabox_settings['desc'] );
?>
</div>
<?php
}
foreach ( $this->metabox_settings['fields'] as $field ) {
/* get current term meta data */
$field_value = get_user_meta( $user->ID, $field['id'], true );
/* set standard value */
if ( isset( $field['std'] ) ) {
$field_value = ot_filter_std_value( $field_value, $field['std'] );
}
/* build the arguments array */
$_args = array(
'type' => $field['type'],
'field_id' => $field['id'],
'field_name' => $field['id'],
'field_value' => $field_value,
'field_desc' => isset( $field['desc'] ) ? $field['desc'] : '',
'field_std' => isset( $field['std'] ) ? $field['std'] : '',
'field_rows' => isset( $field['rows'] ) && ! empty( $field['rows'] ) ? $field['rows'] : 10,
'field_post_type' => isset( $field['post_type'] ) && ! empty( $field['post_type'] ) ? $field['post_type'] : 'post',
'field_taxonomy' => isset( $field['taxonomy'] ) && ! empty( $field['taxonomy'] ) ? $field['taxonomy'] : 'category',
'field_min_max_step' => isset( $field['min_max_step'] ) && ! empty( $field['min_max_step'] ) ? $field['min_max_step'] : '0,100,1',
'field_class' => isset( $field['class'] ) ? $field['class'] : '',
'field_condition' => isset( $field['condition'] ) ? $field['condition'] : '',
'field_operator' => isset( $field['operator'] ) ? $field['operator'] : 'and',
'field_choices' => isset( $field['choices'] ) ? $field['choices'] : [],
'field_settings' => isset( $field['settings'] ) && ! empty( $field['settings'] ) ? $field['settings'] : [],
'post_id' => $user->ID,
'meta' => true
);
$conditions = '';
/* setup the conditions */
if ( isset( $field['condition'] ) && ! empty( $field['condition'] ) ) {
$conditions = ' data-condition="' . $field['condition'] . '"';
$conditions .= isset( $field['operator'] ) && in_array( $field['operator'], [
'and',
'AND',
'or',
'OR'
] ) ? ' data-operator="' . $field['operator'] . '"' : '';
}
/* only allow simple textarea due to DOM issues with wp_editor() */
if (
apply_filters(
'ot_override_forced_textarea_simple',
false,
$field['id']
) == false &&
$_args['type'] == 'textarea'
) {
$_args['type'] = 'textarea-simple';
}
// Build the setting CSS class
if ( ! empty( $_args['field_class'] ) ) {
$classes = explode( ' ', $_args['field_class'] );
foreach ( $classes as $key => $value ) {
$classes[ $key ] = $value . '-wrap';
}
$class = 'format-settings ' . implode( ' ', $classes );
} else {
$class = 'format-settings';
}
?>
<div
id="setting_<?php echo esc_attr( $field['id'] ) ?>"
class="<?php echo esc_attr( $class ); ?>"
<?php echo esc_attr( $conditions ) ?>
>
<div class="format-setting-wrap">
<?php
/* don't show title with textblocks */
if (
$_args['type'] !== 'textblock' &&
! empty( $field['label'] )
) {
?>
<div class="format-setting-label">
<label
for="<?php echo esc_attr( $field['id'] ); ?>"
class="label"
>
<?php echo $field['label']; ?>
</label>
</div>
<?php
}
echo ot_display_by_type( $_args );
?>
</div>
</div>
<?php
}
?>
<div class="clear"></div>
</div>
</div>
</div>
</div>
</div>
</td>
</tr>
</table>
<?php
}
/**
* Responsible to save the taxonomy meta
*
* @param int $user_id
*
* @return int
*/
public function save_user_meta( int $user_id = 0 ) {
global $pagenow;
if ( !current_user_can( 'edit_user', $user_id ) ) {
return false;
}
/* don't save during quick edit */
if ( $pagenow == 'admin-ajax.php' ) {
return $user_id;
}
/* verify nonce */
if (
isset( $_POST[ $this->metabox_settings['id'] . '_nonce' ] ) &&
! wp_verify_nonce(
$_POST[ $this->metabox_settings['id'] . '_nonce' ],
$this->metabox_settings['id'] )
) {
return $user_id;
}
foreach ( $this->metabox_settings['fields'] as $field ) {
$old = get_user_meta( $user_id, $field['id'], true );
$new = '';
/* there is data to validate */
if ( isset( $_POST[ $field['id'] ] ) ) {
/* slider and list item */
if ( in_array( $field['type'], [ 'list-item', 'slider' ] ) ) {
/* required title setting */
$required_setting = [
[
'id' => 'title',
'label' => __( 'Title', 'option-tree' ),
'desc' => '',
'std' => '',
'type' => 'text',
'rows' => '',
'class' => 'option-tree-setting-title',
'post_type' => '',
'choices' => []
]
];
/* get the settings array */
$settings = isset( $_POST[ $field['id'] . '_settings_array' ] ) ? unserialize( ot_decode( $_POST[ $field['id'] . '_settings_array' ] ) ) : [];
/* settings are empty for some odd ass reason get the defaults */
if ( empty( $settings ) ) {
$settings = 'slider' == $field['type'] ? ot_slider_settings( $field['id'] ) : ot_list_item_settings( $field['id'] );
}
/* merge the two settings array */
$settings = array_merge( $required_setting, $settings );
foreach ( $_POST[ $field['id'] ] as $k => $setting_array ) {
foreach ( $settings as $sub_setting ) {
/* verify sub setting has a type & value */
if ( isset( $sub_setting['type'] ) && isset( $_POST[ $field['id'] ][ $k ][ $sub_setting['id'] ] ) ) {
$_POST[ $field['id'] ][ $k ][ $sub_setting['id'] ] = ot_validate_setting( $_POST[ $field['id'] ][ $k ][ $sub_setting['id'] ], $sub_setting['type'], $sub_setting['id'] );
}
}
}
/* set up new data with validated data */
$new = $_POST[ $field['id'] ];
} else if ( $field['type'] == 'social-links' ) {
/* get the settings array */
$settings = isset( $_POST[ $field['id'] . '_settings_array' ] ) ? unserialize( ot_decode( $_POST[ $field['id'] . '_settings_array' ] ) ) : [];
/* settings are empty get the defaults */
if ( empty( $settings ) ) {
$settings = ot_social_links_settings( $field['id'] );
}
foreach ( $_POST[ $field['id'] ] as $k => $setting_array ) {
foreach ( $settings as $sub_setting ) {
/* verify sub setting has a type & value */
if ( isset( $sub_setting['type'] ) && isset( $_POST[ $field['id'] ][ $k ][ $sub_setting['id'] ] ) ) {
$_POST[ $field['id'] ][ $k ][ $sub_setting['id'] ] = ot_validate_setting( $_POST[ $field['id'] ][ $k ][ $sub_setting['id'] ], $sub_setting['type'], $sub_setting['id'] );
}
}
}
/* set up new data with validated data */
$new = $_POST[ $field['id'] ];
} else {
/* run through validattion */
$new = ot_validate_setting( $_POST[ $field['id'] ], $field['type'], $field['id'] );
}
/* insert CSS */
if ( $field['type'] == 'css' ) {
/* insert CSS into dynamic.css */
if ( '' !== $new ) {
ot_insert_css_with_markers( $field['id'], $new, true );
/* remove old CSS from dynamic.css */
} else {
ot_remove_old_css( $field['id'] );
}
}
}
if ( isset( $new ) && $new !== $old ) {
update_user_meta( $user_id, $field['id'], $new );
} else if ( '' == $new && $old ) {
delete_user_meta( $user_id, $field['id'], $old );
}
}
return $user_id;
}
/**
* Responsible to load the required JS for the Theme Options in the taxonomies pages.
*/
protected function load_scripts() {
/* execute scripts before actions */
do_action( 'ot_admin_scripts_before' );
if ( function_exists( 'wp_enqueue_media' ) ) {
/* WP 3.5 Media Uploader */
wp_enqueue_media();
} else {
/* Legacy Thickbox */
add_thickbox();
}
/* load jQuery-ui slider */
wp_enqueue_script( 'jquery-ui-slider' );
/* load jQuery-ui datepicker */
wp_enqueue_script( 'jquery-ui-datepicker' );
/* load WP colorpicker */
wp_enqueue_script( 'wp-color-picker' );
/* load Ace Editor for CSS Editing */
wp_enqueue_script(
'ace-editor',
'https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/ace.js',
null,
'1.1.3'
);
/* load jQuery UI timepicker addon */
wp_enqueue_script(
'jquery-ui-timepicker',
OT_URL . 'assets/js/vendor/jquery/jquery-ui-timepicker.js',
[ 'jquery', 'jquery-ui-slider', 'jquery-ui-datepicker' ],
'1.4.3'
);
/* load the post formats */
if ( OT_META_BOXES == true && OT_POST_FORMATS == true ) {
wp_enqueue_script(
'ot-postformats',
OT_URL . 'assets/js/ot-postformats.js',
[ 'jquery' ],
'1.0.1'
);
}
/* load all the required scripts */
wp_enqueue_script(
'ot-admin-js',
OT_URL . 'assets/js/ot-admin.js',
[
'jquery',
'jquery-ui-tabs',
'jquery-ui-sortable',
'jquery-ui-slider',
'wp-color-picker',
'ace-editor',
'jquery-ui-datepicker',
'jquery-ui-timepicker',
],
OT_VERSION
);
/* create localized JS array */
$localized_array = array(
'ajax' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'option_tree' ),
'upload_text' => apply_filters( 'ot_upload_text',
__( 'Send to OptionTree', 'option-tree' ) ),
'remove_media_text' => __( 'Remove Media', 'option-tree' ),
'reset_agree' => __( 'Are you sure you want to reset back to the defaults?',
'option-tree' ),
'remove_no' => __( 'You can\'t remove this! But you can edit the values.',
'option-tree' ),
'remove_agree' => __( 'Are you sure you want to remove this?', 'option-tree' ),
'activate_layout_agree' => __( 'Are you sure you want to activate this layout?',
'option-tree' ),
'setting_limit' => __( 'Sorry, you can\'t have settings three levels deep.',
'option-tree' ),
'delete' => __( 'Delete Gallery', 'option-tree' ),
'edit' => __( 'Edit Gallery', 'option-tree' ),
'create' => __( 'Create Gallery', 'option-tree' ),
'confirm' => __( 'Are you sure you want to delete this Gallery?',
'option-tree' ),
'date_current' => __( 'Today', 'option-tree' ),
'date_time_current' => __( 'Now', 'option-tree' ),
'date_close' => __( 'Close', 'option-tree' ),
'replace' => __( 'Featured Image', 'option-tree' ),
'with' => __( 'Image', 'option-tree' ),
);
/* localized script attached to 'option_tree' */
wp_localize_script( 'ot-admin-js', 'option_tree', $localized_array );
/* execute scripts after actions */
do_action( 'ot_admin_scripts_after' );
}
/**
* Responsible to load the required CSS for the Theme Options in the taxonomies pages.
*/
protected function load_styles() {
global $wp_styles;
/* execute styles before actions */
do_action( 'ce_admin_styles_before' );
/* load WP colorpicker */
wp_enqueue_style( 'wp-color-picker' );
/* load admin styles */
wp_enqueue_style( 'ot-admin-css', OT_URL . 'assets/css/ot-admin.css', false, OT_VERSION );
/* load the RTL stylesheet */
$wp_styles->add_data( 'ot-admin-css', 'rtl', true );
/**
* Filter the screen IDs used to dequeue `jquery-ui-css`.
*
* @since 2.5.0
*
* @param array $screen_ids An array of screen IDs.
*/
$screen_ids = apply_filters(
'ot_dequeue_jquery_ui_css_screen_ids',
[
'toplevel_page_ot-settings',
'optiontree_page_ot-documentation',
'appearance_page_ot-theme-options',
]
);
/* Remove styles added by the WP Review plugin and any custom pages added through filtering */
if ( in_array( get_current_screen()->id, $screen_ids ) ) {
wp_dequeue_style( 'plugin_name-admin-ui-css' );
wp_dequeue_style( 'jquery-ui-css' );
}
/* execute styles after actions */
do_action( 'ce_admin_styles_after' );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment