protected static function handle_posted_settings() { $result = array('message' => '', 'type' => 'updated'); if (isset($_POST['posts_per_page']) && check_admin_referer('wpak_save_settings')) { $settings = self::get_settings(); if (!empty($_POST['posts_per_page'])) { $settings['posts_per_page'] = intval($_POST['posts_per_page']); } if (isset($_POST['activate_wp_appkit_editor_role'])) { WpakUserPermissions::create_wp_appkit_user_role(); $settings['activate_wp_appkit_editor_role'] = true; } else { WpakUserPermissions::remove_wp_appkit_user_role(); $settings['activate_wp_appkit_editor_role'] = false; } self::save_settings($settings); $result['message'] = __('Settings saved', WpAppKit::i18n_domain); } return $result; } protected static function save_settings($settings) { if (get_option(self::option_id) != $settings) { update_option(self::option_id, $settings); } else { add_option(self::option_id, $settings, '', 'no'); } } } WpakSettings::hooks();
protected function compute_data($component, $options, $args = array()) { global $wpdb; do_action('wpak_before_component_posts_list', $component, $options); $before_post_date = ''; if (!empty($args['before_item']) && is_numeric($args['before_item'])) { $before_post = get_post($args['before_item']); if (!empty($before_post)) { $before_post_date = $before_post->post_date; } } if ($options['post-type'] == 'custom') { //Custom posts list generated via hook : //Choose "Custom, using hook" when creating the component in BO, and use the following //hook "wpak_posts_list_custom-[your-hook]" to set the component posts. //The wpak_posts_list_custom-[your-hook] filter must return the given $posts_list_data filled in with //your custom data : //- posts : array of the posts retrieved by your component, in the same format as a "get_posts()" or "new WP_Query($query_args)" //- total : total number of those posts (not only those retrieved in posts, taking pagination into account) //- query : data about your query that you want to retrieve on the app side. $posts_list_data = array('posts' => array(), 'total' => 0, 'query' => array('type' => 'custom-posts-list', 'taxonomy' => '', 'terms' => array(), 'is_last_page' => true, 'before_item' => 0)); /** * Filter data from a posts list component. * * @param array $posts_list_data An array of default data. * @param WpakComponent $component The component object. * @param array $options An array of options. * @param array $args An array of complementary arguments. * @param array $before_post_date The publication of the last displayed post. */ $posts_list_data = apply_filters('wpak_posts_list_custom-' . $options['hook'], $posts_list_data, $component, $options, $args, $before_post_date); $posts = $posts_list_data['posts']; $total = !empty($posts_list_data['total']) ? $posts_list_data['total'] : count($posts); $query = $posts_list_data['query']; } else { //WordPress Post type or "Latest posts" $is_last_posts = $options['post-type'] == 'last-posts'; $post_type = !empty($options['post-type']) && !$is_last_posts ? $options['post-type'] : 'post'; $query = array('post_type' => $post_type); $query_args = array('post_type' => $post_type); /** * Filter the number of posts displayed into a posts list component. * * @param int Default number of posts. * @param WpakComponent $component The component object. * @param array $options An array of options. * @param array $args An array of complementary arguments. */ $query_args['posts_per_page'] = apply_filters('wpak_posts_list_posts_per_page', WpakSettings::get_setting('posts_per_page'), $component, $options, $args); if ($is_last_posts) { $query['type'] = 'last-posts'; } elseif (!empty($options['taxonomy'])) { if ($options['taxonomy'] === 'wpak-none') { $query['type'] = 'post-type'; } elseif (!empty($options['term'])) { $query_args['tax_query'] = array(array('taxonomy' => $options['taxonomy'], 'field' => 'slug', 'terms' => $options['term'])); $query['type'] = 'taxonomy'; $query['taxonomy'] = $options['taxonomy']; $query['terms'] = is_array($options['term']) ? $options['term'] : array($options['term']); } } if (!empty($before_post_date)) { if (is_numeric($before_post_date)) { //timestamp $before_post_date = date('Y-m-d H:i:s', $before_post_date); } if (preg_match('/\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}/', $before_post_date)) { $query['before_item'] = intval($args['before_item']); $posts_where_callback = create_function('$where', 'return $where .= " AND post_date < \'' . $before_post_date . '\'";'); add_filter('posts_where', $posts_where_callback); } else { $before_post_date = ''; } } /** * Filter args used for the query made into a posts list component. * * @param array $query_args An array of default args. * @param WpakComponent $component The component object. * @param array $options An array of options. * @param array $args An array of complementary arguments. * @param array $query Data about the query to retrieve on the app side. */ $query_args = apply_filters('wpak_posts_list_query_args', $query_args, $component, $options, $args, $query); $posts_query = new WP_Query($query_args); if (!empty($before_post_date)) { remove_filter('posts_where', $posts_where_callback); $query['is_last_page'] = $posts_query->found_posts <= count($posts_query->posts); } $posts = $posts_query->posts; $total = $posts_query->found_posts; } $posts_by_ids = array(); foreach ($posts as $post) { $posts_by_ids[$post->ID] = self::get_post_data($component, $post); } $this->set_specific('ids', array_keys($posts_by_ids)); $this->set_specific('total', $total); $this->set_specific('query', $query); $this->set_globals('posts', $posts_by_ids); }