function array_merge_replace() { $arrays = func_get_args(); $base = array_shift($arrays); if (!is_array($base)) { $base = empty($base) ? array() : array($base); } foreach ($arrays as $append) { if (!is_array($append)) { $append = array($append); } foreach ($append as $key => $value) { if (!array_key_exists($key, $base) and !is_numeric($key)) { $base[$key] = $append[$key]; continue; } if (is_array($value) or is_array($base[$key])) { $base[$key] = array_merge_replace($base[$key], $append[$key]); } else { if (is_numeric($key)) { if (!in_array($value, $base)) { $base[] = $value; } } else { $base[$key] = $value; } } } } return $base; }
/** * Finds which forum are available to the user for see */ private function getAuthViewSql() { global $total_categories, $total_forums, $category_rows, $userdata, $forum_data, $is_auth_ary; $ary = auth(AUTH_VIEW, AUTH_LIST_ALL, $userdata, $forum_data); $ary2 = auth(AUTH_READ, AUTH_LIST_ALL, $userdata, $forum_data); $is_auth_ary = array_merge_replace($ary, $ary2); $auth_view_forum_sql = ''; for ($i = 0; $i < $total_categories; $i++) { $cat_id = $category_rows[$i]['cat_id']; for ($j = 0; $j < $total_forums; $j++) { if ($is_auth_ary[$forum_data[$j]['forum_id']]['auth_view'] && $is_auth_ary[$forum_data[$j]['forum_id']]['auth_read'] && $forum_data[$j]['cat_id'] == $cat_id) { $auth_view_forum_sql .= ($auth_view_forum_sql == '' ? '' : ', ') . $forum_data[$j]['forum_id']; } } } return $auth_view_forum_sql == '' ? '(0)' : '(' . $auth_view_forum_sql . ')'; }
function array_merge_replace($array, $newValues) { foreach ($newValues as $key => $value) { if (is_array($value)) { if (!isset($array[$key])) { $array[$key] = array(); } $array[$key] = array_merge_replace($array[$key], $value); } else { if (isset($array[$key]) && is_array($array[$key])) { $array[$key][0] = $value; } else { if (isset($array) && !is_array($array)) { $temp = $array; $array = array(); $array[0] = $temp; } $array[$key] = $value; } } } return $array; }
private static function setMeta($options) { if (empty($options['id'])) { throw new Exception('AeriaUserMeta: You must define a user meta id.', 1); } if (empty($options['type'])) { throw new Exception('AeriaUserMeta: You must define a user meta type.', 1); } if (!in_array($options['type'], static::$type)) { throw new Exception("AeriaUserMeta: The " . $options['type'] . " type isn't supported.", 1); } $options = array_merge_replace(array('label' => '', 'title' => 'Campi aggiuntivi'), $options); $saveMeta = function ($user_id) use($options) { global $pagenow; if ($pagenow !== 'profile.php' && (!current_user_can('edit_user', $user_id) && $pagenow === 'user-edit.php' or !current_user_can('create_users', $user_id) && $pagenow === 'user-new.php')) { return false; } update_user_meta($user_id, $options['id'], $_POST[$options['id']]); }; $metaFields = function ($user) use($options) { if (!current_user_can('read')) { return false; } if (!self::$showedTitle) { echo '<h3>', $options['title'], '</h3>'; self::$showedTitle = true; } AeriaForm::create(['action' => 'hack', 'type' => 'wordpress', 'startForm' => '', 'endForm' => ''])->setFields(['id' => $options['id'], 'name' => $options['id'], 'label' => $options['label'], 'type' => $options['type'], 'other' => 'class="regular-text"', 'value' => esc_attr(get_the_author_meta($options['id'], $user->ID))])->getForm(); }; foreach (static::$saveHook as $value) { add_action($value, $saveMeta, 1); } foreach (static::$showHook as $value) { add_action($value, $metaFields, 1); } }
function show_field_posts($field, $meta) { global $post; $this->show_field_begin($field, $meta); $conditions = empty($field['conditions']) ? array() : $field['conditions']; foreach ($conditions as $condition => &$value) { switch ($condition) { case 'only_childs': $condition = 'post_parent'; if ($value && $post) { $value = $post->ID; } else { $value = false; } unset($conditions['only_childs']); break; } } $data = get_posts(array_merge_replace(array('numberposts' => -1, 'post_status' => 'publish', 'post_type' => $field['postTypes']), $conditions)); echo "<select class='select2' data-minimum='" . (isset($field['minimum']) ? $field['minimum'] : '0') . "' name='{$field['id']}' " . (isset($field['single']) && $field['single'] ? '' : "multiple style='height:auto'") . ">"; if (is_callable($field['filter'])) { $data = array_filter($data, $field['filter']); } $ids = is_array($meta) ? $meta : explode(',', $meta); foreach ($data as $thePost) { $key = $thePost->ID; $value = $thePost->post_title; echo "<option value='{$key}'" . selected(in_array($key, $ids), true, false) . ">" . $value . "</option>"; } echo "</select>"; $this->show_field_end($field, $meta); }
public static function register($type) { if (empty($type['id'])) { trigger_error('AeriaType: You must define a post_type id.', E_USER_ERROR); } $type['options'] = empty($type['options']) ? array() : $type['options']; $post_type = $type['id']; $post_name = isset($type['title']) ? $type['title'] : ucfirst($type['id']); static::$types[$post_type] = $type; if (false === empty($type['metabox'])) { // Support one or multiple metabox definitions if (isset($type['metabox']['id'])) { $type['metabox'] = [$type['metabox']]; } foreach ($type['metabox'] as $mbox) { if (empty($mbox['pages'])) { $mbox['pages'] = [$post_type]; } if (!in_array($post_type, $mbox['pages'])) { $mbox['pages'][] = $post_type; } AeriaMetabox::register($mbox); } unset($type['metabox']); } if (false === empty($type['taxonomy'])) { // Support one or multiple taxonomy definitions if (isset($type['taxonomy']['id'])) { $type['taxonomy'] = [$type['taxonomy']]; } foreach ($type['taxonomy'] as $tax) { if (empty($tax['types'])) { $tax['types'] = [$post_type]; } if (!in_array($post_type, $tax['types'])) { $tax['types'][] = $post_type; } AeriaTaxonomy::register($tax); } unset($type['taxonomy']); } if (false === empty($type['columns'])) { AeriaColumns::register($type['id'], $type['columns']); unset($type['columns']); } if (false === empty($type['sections'])) { $sections_args = ['type' => $post_type]; if (isset($type['sections']['fields'])) { $sections_args['fields'] = $type['sections']['fields']; } if (isset($type['sections']['description'])) { $sections_args['description'] = $type['sections']['description']; } if (isset($type['sections']['supports'])) { $sections_args['supports'] = $type['sections']['supports']; } AeriaSection::register($sections_args); unset($type['sections']); } add_action('init', function () use($type, $post_type, $post_name) { $options = array_merge_replace(array('public' => true, 'publicly_queryable' => true, 'show_ui' => true, 'show_in_menu' => true, 'query_var' => true, 'rewrite' => ['slug' => $post_type, 'with_front' => false], 'capability_type' => 'post', 'with_front' => false, 'has_archive' => true, 'hierarchical' => true, 'feeds' => false, 'menu_position' => null, 'reorder' => false, 'supports' => false), $type['options']); unset($type['options']); $options_supports = $options['supports']; $options_supports = !$options_supports ? 'title,editor' : str_replace(' ', '', $options_supports); $options['supports'] = explode(',', $options_supports); if ($options['hierarchical']) { $options['supports'][] = 'page-attributes'; } $options['labels'] = array_merge_replace(array('name' => $post_name, 'singular_name' => $post_name, 'add_new' => 'Add new', 'add_new_item' => 'Add new Item', 'edit_item' => 'Edit', 'new_item' => 'New', 'all_items' => 'Show all', 'view_item' => 'Show item', 'search_items' => 'Search', 'not_found' => 'Not found', 'not_found_in_trash' => 'Not found in trash', 'parent_item_colon' => '', 'taxonomies' => [], 'menu_name' => $post_name, 'menu_icon' => null), $type); register_post_type($post_type, $options); if ($options['reorder']) { new AeriaReorder(array('post_type' => $post_type, 'order' => 'ASC', 'heading' => $options['labels']['singular_name'], 'final' => '', 'initial' => '', 'menu_label' => __('Reorder', 'reorder'), 'icon' => '', 'post_status' => 'publish', 'show_title' => isset($options['reorder']['show_title']) ? $options['reorder']['show_title'] : true, 'fields' => $options['reorder']['fields'] ?: false)); } // Check and register relations $relations = isset($type['relations']) ? $type['relations'] : false; if ($relations) { $meta_fields = []; foreach ((array) $relations as $key => $relation) { $multiple = isset($relation['multiple']) ? $relation['multiple'] : false; $meta_fields[] = ['name' => $relation['title'], 'id' => 'relations_' . $post_type . '_' . $relation['type'], 'type' => 'select_ajax', 'multiple' => $multiple, 'relation' => $relation['type']]; } AeriaMetabox::register(['id' => 'relations_' . $post_type, 'title' => 'Relations ' . $options['labels']['name'], 'pages' => [$post_type], 'fields' => $meta_fields]); } }); }
private function renderFormType($formType) { if (self::validateArrayKey(static::$customType, $formType, false)) { if (self::validateArrayKey(static::$customType[$formType], 'before', false)) { $this->before = array_merge_replace($this->before, static::$customType[$formType]['before']); } if (self::validateArrayKey(static::$customType[$formType], 'after', false)) { $this->after = array_merge_replace($this->after, static::$customType[$formType]['after']); } } else { throw new Exception("AeriaForm: Form type " . $formType . " isn't supported.", 1); } }