false ]; /** * Create a new AdvancedPostSearch * * @access public * @return void */ public function __construct() { // session if (!session_id()) { session_start(); } // plugin autoloader spl_autoload_register(function ($class) { $classFile = strtolower(str_replace([__CLASS__, '\\'], ['', '/'], $class)); if (file_exists(APS_ROOT.'/core/'.$classFile.'.php')) { require_once(APS_ROOT.'/core/'.$classFile.'.php'); } }); // base url $this->base_url = plugin_dir_url(__FILE__); // initialization $this->init(); } /** * Initialize an AdvancedPostSearch. * * @access public * @return void */ public function init() { // actions add_action('load-edit.php', [$this, 'loadEditInjectForm'], 10, 0); add_action('plugins_loaded', [$this, 'pluginsLoaded'], 10, 0); add_action('admin_menu', [$this, 'adminMenu'], 10, 0); // options if (false === get_option('aps_views', false)) { update_option('aps_views', [], false); } } /** * Load plugin textdomain. * * @access public * @return void */ public function pluginsLoaded() { load_plugin_textdomain('aps', false, dirname(plugin_basename( __FILE__ )).'/lang/'); do_action('aps/plugin/loaded'); } /** * Register the admin menu items. * * @access public * @return void */ public function adminMenu() { global $submenu; add_menu_page(__('Advanced Post Search', 'aps'), __('Advanced Post Search', 'aps'), 'manage_options', 'aps-views', [$this, 'screenViews'], 'dashicons-search', 85); add_submenu_page('aps-views', __('FAQ', 'aps'), __('FAQ', 'aps'), 'manage_options', 'aps-faq', [$this, 'screenFaq']); $submenu['aps-views'][0][0] = __('Views', 'aps'); } /** * Views management. * * @access public * @return void */ public function screenViews() { $post_types = get_post_types(['public' => true], 'objects'); unset($post_types['attachment']); // remove attachments $field_groups = $this->getFieldGroups(); $views = get_option('aps_views', []); if (@$_REQUEST['aps-action'] === 'update-options') { $views = call_user_func_array('array_merge', array_map(function ($post_type) { return [ $post_type => [ 'fields' => ($_REQUEST['views'][$post_type]['fields'] ?: []), 'options' => ($_REQUEST['views'][$post_type]['options'] ?: []) ] ]; }, array_keys($post_types))); update_option('aps_views', $views, false); } ?>


$object): ?> $object): ?> $field_group): ?> $object): ?> isEnabled($post_type, $field_group); ?> fields as $idx => $field): ?> $object): ?> isEnabled($post_type, $field_group, $field); ?> isActive(@$views[$post_type]['fields'], $post_type, $field_group, $field); ?>
label; ?>
label; ?>
label; ?> />
label; ?> />
isProEnabled()): ?>


$object): ?> $object): ?> $object): ?> default_options, (array) @$views[$post_type]['options']); ?>
label; ?>
label; ?>

plugins@var-dump.it'); ?>

post_type === $screen->id) { // include js && css wp_enqueue_script('aps', $this->base_url.'/js/advanced-post-search.js', ['jquery', 'jquery-ui-datepicker'], false, true); wp_enqueue_style('aps', $this->base_url.'/css/advanced-post-search.css', false, false, 'all'); wp_enqueue_style('jquery-ui-css', '//ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/themes/smoothness/jquery-ui.css'); // inject HTML add_action('all_admin_notices', [$this, 'allAdminNotices'], 10, 0); // inject query filters add_action('pre_get_posts', [$this, 'preGetPosts'], 10, 1); // inject query WHERE add_filter('posts_where', [$this, 'postsWhere'], 10, 2); // save session if (@$_REQUEST['aps-action'] === 'advanced-search') { if (array_key_exists(self::IDENTIFIER, $_REQUEST)) { $_SESSION[self::IDENTIFIER] = $_REQUEST[self::IDENTIFIER]; } } else { $_SESSION[self::IDENTIFIER] = []; } } } /** * Render search form. * * @access public * @return void */ public function allAdminNotices() { $screen = get_current_screen(); $view = $this->getViewSettings(); if ($field_groups = $this->getFieldGroups()) { ?> query['post_type'] === $screen->post_type) { if (@$_REQUEST['aps-action'] === 'advanced-search') { $wp_query->query_vars['meta_query'] = array_reduce($this->getActiveFields(), function ($meta_query, $field) { return array_merge($meta_query, [$field->getMetaQuery()]); }, ['relation' => 'AND']); } } } /** * Filter post with search settings (WHERE). * * @access public * @param mixed $where * @return string */ public function postsWhere($where, &$wp_query) { $screen = get_current_screen(); if ($wp_query->query['post_type'] === $screen->post_type) { if (@$_REQUEST['aps-action'] === 'advanced-search') { $where = array_reduce($this->getActiveFields(), function ($where, $field) { return $where.(($append = $field->getWhere()) ? $append : ''); }, $where); } } return $where; } /** * Check if a field is active for a view or not * * @access private * @param mixed $view * @param mixed $post_type * @param mixed $field_group * @param bool $field (default: false) * @return bool */ private function isActive($view, $post_type, $field_group, $field = false) { if (!($field_group instanceof \AdvancedPostSearch\FieldGroup)) { return false; } if ($field && !($field instanceof \AdvancedPostSearch\Field)) { return false; } if ($view) { if (array_key_exists($field_group->key, $view)) { if ($field) { $status = in_array($field->key, $view[$field_group->key]); } else { $status = true; } } else { $status = false; } } else { $status = $field_group->defaultStatus($post_type); } if ($field === false) { // group return apply_filters('aps/group/active', $status, $post_type, $field_group->key); } else { // field $status = apply_filters('aps/group/active', $status, $post_type, $field_group->key); return apply_filters('aps/field/active', $status, $post_type, $field_group->key, $field->key); } } /** * Check if a field is enabled for a view or not. * * @access private * @param mixed $post_type * @param mixed $field_group * @param bool $field (default: false) * @return void */ private function isEnabled($post_type, $field_group, $field = false) { if ($field === false) { // group return apply_filters('aps/group/enabled', true, $post_type, $field_group->key); } else { // field $status = apply_filters('aps/group/active', true, $post_type, $field_group->key); return apply_filters('aps/field/enabled', $status, $post_type, $field_group->key, $field->key); } } /** * Get field groups. * * @access private * @return void */ private function getFieldGroups() { $groups = []; // wordpress $wordpress = new \AdvancedPostSearch\WP\FieldGroup('wordpress', __('WordPress', 'aps')); array_map(function ($field) use (&$wordpress) { $wordpress->registerField($field); }, $this->getWordpressFields()); $groups = array_merge($groups, [$wordpress]); // return return apply_filters('aps/groups/list', $groups); } /** * Get active fields. * * @access private * @return void */ private function getActiveFields() { $view = $this->getViewSettings(); return call_user_func_array('array_merge', array_map(function ($field_group) use ($view) { if ($this->isActive($view['fields'], $view['post_type'], $field_group)) { return array_filter($field_group->fields, function ($field) use ($field_group, $view) { return $this->isActive($view['fields'], $view['post_type'], $field_group, $field); }); } return []; }, $this->getFieldGroups())); } /** * Get view settings. * * @access private * @return void */ private function getViewSettings() { $screen = get_current_screen(); $views = get_option('aps_views', []); if (array_key_exists($screen->post_type, $views)) { $options = array_merge($this->default_options, (array) @$views[$screen->post_type]['options']); $options = apply_filters('aps/view/options', $options, $screen->post_type); return [ 'post_type' => $screen->post_type, 'fields' => $views[$screen->post_type]['fields'], 'options' => [ 'show_groups' => $options['show_groups'] ] ]; } return false; } /** * Get wordpress fields. * * @access private * @return array */ private function getWordpressFields() { return array_filter(array_map(function ($field) { $extra = array_diff_key($field, ['type' => '', 'key' => '', 'label' => '']); return \AdvancedPostSearch\WP\Field::get($field['type'], $field['key'], $field['label'], $extra); }, [ [ 'type' => 'text', 'key' => 'post_title', 'label' => __('Post title', 'aps') ], [ 'type' => 'select', 'key' => 'post_status', 'label' => __('Status', 'aps'), 'choices' => array_map(function ($status) { return $status->label; }, get_post_stati(['internal' => false], 'objects')) ], [ 'type' => 'select', 'key' => 'post_author', 'label' => __('Author', 'aps'), 'choices' => array_reduce(get_users(['who' => 'authors']), function ($authors, $author) { return ($authors + [$author->ID => $author->display_name]); }, []) ], [ 'type' => 'text', 'key' => 'post_name', 'label' => __('Slug', 'aps') ], [ 'type' => 'text', 'key' => 'post_password', 'label' => __('Password', 'aps') ], [ 'type' => 'text', 'key' => 'post_excerpt', 'label' => __('Excerpt', 'aps') ], [ 'type' => 'text', 'key' => 'post_content', 'label' => __('Content', 'aps') ], [ 'type' => 'date_range', 'key' => 'post_date', 'label' => __('Published', 'aps') ], [ 'type' => 'date_range', 'key' => 'post_modified', 'label' => __('Last Modified', 'aps') ], ])); } /** * Check if PRO version is enabled. * * @access private * @return void */ private function isProEnabled() { require_once ABSPATH.'wp-admin/includes/plugin.php'; return is_plugin_active('advanced-post-search-pro/advanced-post-search-pro.php'); } } /** * AdvancedPostSearch function * * @return AdvancedPostSearch */ function AdvancedPostSearch() { global $AdvancedPostSearch; if (!isset($AdvancedPostSearch)) { $AdvancedPostSearch = new AdvancedPostSearch(); } return $AdvancedPostSearch; } AdvancedPostSearch(); }