function hocwp_remove_empty_array_item($arr, $remove_zero = false) { if (is_array($arr)) { foreach ($arr as $key => $item) { if (is_array($item)) { $arr[$key] = hocwp_remove_empty_array_item($item); } elseif (hocwp_string_empty($item) || $remove_zero && 0 === $item) { unset($arr[$key]); } } } return $arr; }
function hocwp_update_user_saved_posts($user_id = null, $post_id = null) { $user_id = hocwp_return_user($user_id, 'id'); if (hocwp_id_number_valid($user_id)) { $post_id = hocwp_return_post($post_id, 'id'); if (hocwp_id_number_valid($post_id)) { $saved = hocwp_get_user_saved_posts($user_id); $saved = hocwp_sanitize_array($saved); if (in_array($post_id, $saved)) { unset($saved[array_search($post_id, $saved)]); } else { array_push($saved, $post_id); } $saved = hocwp_remove_empty_array_item($saved, true); return update_user_meta($user_id, 'saved_posts', $saved); } } return false; }
function hocwp_field_select_term($args = array()) { hocwp_field_sanitize_args($args); $taxonomy = hocwp_get_value_by_key($args, 'taxonomy'); $taxonomies = hocwp_get_value_by_key($args, 'taxonomies'); $taxonomies = hocwp_sanitize_array($taxonomies); $taxonomies = hocwp_remove_empty_array_item($taxonomies); if (!hocwp_array_has_value($taxonomies) && empty($taxonomy)) { $taxonomy = 'category'; } $taxonomies[] = $taxonomy; $taxonomies = hocwp_sanitize_array($taxonomies); $options = isset($args['options']) ? $args['options'] : array(); $force_empty = isset($args['force_empty']) ? (bool) $args['force_empty'] : false; $tax = get_taxonomy($taxonomy); if (!$force_empty) { if (!hocwp_array_has_value($taxonomies) && !hocwp_array_has_value($options)) { _e('Please pass a taxonomy or set options for arguments.', 'hocwp-theme'); return; } } $only_parent = isset($args['only_parent']) ? $args['only_parent'] : false; $id = isset($args['id']) ? $args['id'] : ''; $name = isset($args['name']) ? $args['name'] : ''; $field_class = isset($args['field_class']) ? $args['field_class'] : ''; if (is_object($tax)) { hocwp_add_string_with_space_before($field_class, 'select-' . $tax->rewrite['slug'] . '-terms'); } $args['field_class'] = hocwp_add_string_with_space_before($field_class, 'select-term'); $label = isset($args['label']) ? $args['label'] : ''; $value = isset($args['value']) ? $args['value'] : ''; $description = isset($args['description']) ? $args['description'] : ''; $taxonomy_id = isset($args['taxonomy_id']) ? $args['taxonomy_id'] : ''; $taxonomy_name = isset($args['taxonomy_name']) ? $args['taxonomy_name'] : ''; $show_count = isset($args['show_count']) ? $args['show_count'] : true; $load_item = isset($args['load_item']) ? (bool) $args['load_item'] : true; $option_default = ''; if (isset($args['option_default'])) { $option_default = $args['option_default']; } else { $default_text = isset($args['default_text']) ? $args['default_text'] : __('Choose term'); $option_default = '<option value="0" data-taxonomy="">-- ' . $default_text . ' --</option>'; } $all_option = $option_default; if ($load_item) { $options = wp_parse_args($options, $taxonomies); $options = hocwp_sanitize_array($options); if (hocwp_array_has_value($options)) { foreach ($options as $tax) { if (!is_object($tax)) { $tax = get_taxonomy($tax); } $term_args = array(); if ($only_parent) { $term_args['parent'] = 0; } if (!is_object($tax)) { continue; } $terms = hocwp_get_terms($tax->name, $term_args); if (hocwp_array_has_value($terms)) { $show_count = isset($args['show_count']) ? $args['show_count'] : true; $hirachical = isset($args['hirachical']) ? $args['hirachical'] : true; $option_group = isset($args['option_group']) ? $args['option_group'] : true; $select_args = array('selected' => $value, 'taxonomy' => $tax->name, 'show_count' => $show_count, 'hirachical' => $hirachical); $select = hocwp_get_term_drop_down($select_args); $select = hocwp_remove_select_tag_keep_content($select); $tmp = ''; if (!empty($select)) { if ($option_group) { $tmp = '<optgroup label="' . $tax->labels->singular_name . '" data-taxonomy="' . $tax->name . '">'; $tmp .= $select; $tmp .= '</optgroup>'; } else { $tmp .= $select; } } $all_option .= $tmp; } } } } $args['all_option'] = $all_option; $args['label'] = $label; if (!isset($args['attributes']['data-taxonomy'])) { $args['attributes']['data-taxonomy'] = $taxonomy; } $args['attributes']['data-show-count'] = absint($show_count); hocwp_field_select($args); }