/** * Check the main WordPress query to match WP Idea Stream conditions * Eventually Override query vars and set global template conditions / vars * * This the key function of the plugin, it is definining the templates * to load and is setting the displayed user. * * Inspired by bbPress 's bbp_parse_query() * * @package WP Idea Stream * @subpackage core/template-functions * * @since 2.0.0 * * @param WP_Query $posts_query The WP_Query instance * @uses WP_Query->is_main_query() to check it's the main query * @uses WP_Query->get() to get a query var * @uses wp_idea_stream_is_admin() to check if in IdeaStream's Admin territory * @uses wp_idea_stream_is_sticky_enabled() to check if sticky feature is available * @uses WP_Query->set() to set a query var * @uses wp_idea_stream_is_rating_disabled() to check if ratings feature are available * @uses wp_idea_stream_set_idea_var() to globalize a var * @uses is_admin() to check for WordPress administration * @uses wp_idea_stream_get_post_type() to get the ideas post type identifier * @uses wp_idea_stream_user_rewrite_id() to get the user rewrite id * @uses wp_idea_stream_users_get_user_data() to get a specific user's data * @uses WP_Query->set_404() to set a 404 * @uses wp_idea_stream_user_rates_rewrite_id() to get the user rates rewrite id * @uses wp_idea_stream_user_comments_rewrite_id() to get the user comments rewrite id * @uses wp_idea_stream_action_rewrite_id() to get the action rewrite id * @uses wp_idea_stream_addnew_slug() to get the add new slug * @uses wp_idea_stream_edit_slug() to get the edit slug * @uses has_action() to check if the action 'wp_idea_stream_custom_action' is used by any plugins * @uses do_action() Calls 'wp_idea_stream_custom_action' to perform actions relative to ideas * @uses wp_idea_stream_get_category() to get the ideas category identifier * @uses wp_idea_stream_get_tag() to get the ideas tag identifier * @uses wp_idea_stream_search_rewrite_id() to get the search rewrite id */ function wp_idea_stream_parse_query($posts_query = null) { // Bail if $posts_query is not the main loop if (!$posts_query->is_main_query()) { return; } // Bail if filters are suppressed on this query if (true === $posts_query->get('suppress_filters')) { return; } // Handle the specific queries in IdeaStream Admin if (wp_idea_stream_is_admin()) { // Display sticky ideas if requested if (wp_idea_stream_is_sticky_enabled() && !empty($_GET['sticky_ideas'])) { $posts_query->set('post__in', wp_idea_stream_ideas_get_stickies()); } // Build meta_query if orderby rates is set if (!wp_idea_stream_is_rating_disabled() && !empty($_GET['orderby']) && 'rates_count' == $_GET['orderby']) { $posts_query->set('meta_query', array(array('key' => '_ideastream_average_rate', 'compare' => 'EXISTS'))); // Set the orderby idea var wp_idea_stream_set_idea_var('orderby', 'rates_count'); } do_action('wp_idea_stream_admin_request', $posts_query); return; } // Bail if else where in admin if (is_admin()) { return; } // Ideas post type for a later use $idea_post_type = wp_idea_stream_get_post_type(); /** User's profile ************************************************************/ // Are we requesting the user-profile template ? $user = $posts_query->get(wp_idea_stream_user_rewrite_id()); $embed_page = wp_idea_stream_is_embed_profile(); if (!empty($user)) { if (!is_numeric($user)) { // Get user by his username $user = wp_idea_stream_users_get_user_data('slug', $user); } else { // Get user by his id $user = wp_idea_stream_users_get_user_data('id', $user); } // No user id: no profile! if (empty($user->ID) || true === apply_filters('wp_idea_stream_users_is_spammy', is_multisite() && is_user_spammy($user), $user)) { $posts_query->set_404(); // Make sure the WordPress Embed Template will be used if ('true' === get_query_var('embed') || true === get_query_var('embed')) { $posts_query->is_embed = true; $posts_query->set('p', -1); } return; } // Set the displayed user id wp_idea_stream_set_idea_var('is_user', absint($user->ID)); // Make sure the post_type is set to ideas. $posts_query->set('post_type', $idea_post_type); // Are we requesting user rates $user_rates = $posts_query->get(wp_idea_stream_user_rates_rewrite_id()); // Or user comments ? $user_comments = $posts_query->get(wp_idea_stream_user_comments_rewrite_id()); if (!empty($user_rates) && !wp_idea_stream_is_rating_disabled()) { // We are viewing user's rates wp_idea_stream_set_idea_var('is_user_rates', true); // Define the Meta Query to get his rates $posts_query->set('meta_query', array(array('key' => '_ideastream_rates', 'value' => ';i:' . $user->ID . ';', 'compare' => 'LIKE'))); } else { if (!empty($user_comments)) { // We are viewing user's comments wp_idea_stream_set_idea_var('is_user_comments', true); /** * Make sure no result. * Query will be built later in user comments loop */ $posts_query->set('p', -1); } else { if ('true' === get_query_var('embed') || true === get_query_var('embed')) { $posts_query->is_embed = true; $posts_query->set('p', -1); if ($embed_page) { wp_idea_stream_set_idea_var('is_user_embed', true); } else { $posts_query->set_404(); return; } } // Default to the ideas the user submitted $posts_query->set('author', $user->ID); } } // No stickies on user's profile $posts_query->set('ignore_sticky_posts', true); // Make sure no 404 $posts_query->is_404 = false; // Set the displayed user. wp_idea_stream_set_idea_var('displayed_user', $user); } /** Actions (New Idea) ********************************************************/ $action = $posts_query->get(wp_idea_stream_action_rewrite_id()); if (!empty($action)) { // Make sure the post type is set to ideas $posts_query->set('post_type', $idea_post_type); // Define a global to inform we're dealing with an action wp_idea_stream_set_idea_var('is_action', true); // Is the new idea form requested ? if (wp_idea_stream_addnew_slug() == $action) { // Yes so set the corresponding var wp_idea_stream_set_idea_var('is_new', true); /** * Make sure no result. * We are not querying any content, but creating one */ $posts_query->set('p', -1); // Edit action ? } else { if (wp_idea_stream_edit_slug() == $action) { // Yes so set the corresponding var wp_idea_stream_set_idea_var('is_edit', true); // Signup support } else { if (wp_idea_stream_signup_slug() == $action && wp_idea_stream_is_signup_allowed_for_current_blog()) { // Set the signup global var wp_idea_stream_set_idea_var('is_signup', true); /** * Make sure no result. * We are not querying any content, but creating one */ $posts_query->set('p', -1); } else { if (has_action('wp_idea_stream_custom_action')) { /** * Allow plugins to other custom idea actions * * @param string $action The requested action * @param WP_Query $posts_query The WP_Query instance */ do_action('wp_idea_stream_custom_action', $action, $posts_query); } else { $posts_query->set_404(); return; } } } } } /** Ideas by category *********************************************************/ $category = $posts_query->get(wp_idea_stream_get_category()); if (!empty($category)) { // Make sure the post type is set to ideas $posts_query->set('post_type', $idea_post_type); // Define the current category wp_idea_stream_set_idea_var('is_category', $category); } /** Ideas by tag **************************************************************/ $tag = $posts_query->get(wp_idea_stream_get_tag()); if (!empty($tag)) { // Make sure the post type is set to ideas $posts_query->set('post_type', $idea_post_type); // Define the current tag wp_idea_stream_set_idea_var('is_tag', $tag); } /** Searching ideas ***********************************************************/ $search = $posts_query->get(wp_idea_stream_search_rewrite_id()); if (!empty($search)) { // Make sure the post type is set to ideas $posts_query->set('post_type', $idea_post_type); // Define the query as a search one $posts_query->set('is_search', true); /** * Temporarly set the 's' parameter of WP Query * This will be reset while building ideas main_query args * @see wp_idea_stream_set_template() */ $posts_query->set('s', $search); // Set the search conditionnal var wp_idea_stream_set_idea_var('is_search', true); } /** Changing order ************************************************************/ // Here we're using built-in var $orderby = $posts_query->get('orderby'); // Make sure we are ordering ideas if (!empty($orderby) && $idea_post_type == $posts_query->get('post_type')) { if (!wp_idea_stream_is_rating_disabled() && 'rates_count' == $orderby) { /** * It's an order by rates request, set the meta query to achieve this. * Here we're not ordering yet, we simply make sure to get ideas that * have been rated. * Order will happen thanks to wp_idea_stream_set_rates_count_orderby() * filter. */ $posts_query->set('meta_query', array(array('key' => '_ideastream_average_rate', 'compare' => 'EXISTS'))); } // Set the order by var wp_idea_stream_set_idea_var('orderby', $orderby); } // Set the idea archive var if viewing ideas archive if ($posts_query->is_post_type_archive()) { wp_idea_stream_set_idea_var('is_idea_archive', true); } /** * Finally if post_type is ideas, then we're in IdeaStream's * territory so set this */ if ($idea_post_type === $posts_query->get('post_type')) { wp_idea_stream_set_idea_var('is_ideastream', true); // Reset the pagination if (-1 !== $posts_query->get('p')) { $posts_query->set('posts_per_page', wp_idea_stream_ideas_per_page()); } } }
/** * Edit WP_Query posts to append sticky ideas * * Simply a "copy paste" of how WordPress deals with sticky posts * * @package WP Idea Stream * @subpackage ideas/functions * * @since 2.0.0 * * @param array $posts The array of retrieved posts. * @param WP_Query The WP_Query instance * @uses wp_idea_stream_is_sticky_enabled() to check sticking ideas is enabled * @uses wp_idea_stream_ideas_get_stickies() to get the sticky ideas * @uses is_post_type_archive() to check the ideas archive page is displayed * @uses apply_filters() call 'wp_idea_stream_ideas_get_stickies' to alter this list * @return array the posts with stickies if some are found */ function wp_idea_stream_ideas_stick_ideas($posts = array(), $wp_query = null) { // Bail if sticky is disabled if (!wp_idea_stream_is_sticky_enabled()) { return $posts; } $q = $wp_query->query_vars; $post_type = $q['post_type']; if ('ideas' != $post_type) { return $posts; } $page = absint($q['paged']); $search = $q['s']; if (!empty($q['orderby'])) { return $posts; } $sticky_posts = wp_idea_stream_ideas_get_stickies(); if (wp_idea_stream_is_admin()) { return $posts; } $post_type_landing_page = is_post_type_archive($post_type) && $page <= 1 && empty($search); if (empty($post_type_landing_page) || empty($sticky_posts) || !empty($q['ignore_sticky_posts'])) { return $posts; } // Put sticky ideas at the top of the posts array $num_posts = count($posts); $sticky_offset = 0; // Loop over posts and relocate stickies to the front. for ($i = 0; $i < $num_posts; $i++) { if (in_array($posts[$i]->ID, $sticky_posts)) { $sticky_post = $posts[$i]; // Remove sticky from current position array_splice($posts, $i, 1); // Move to front, after other stickies array_splice($posts, $sticky_offset, 0, array($sticky_post)); // Increment the sticky offset. The next sticky will be placed at this offset. $sticky_offset++; // Remove post from sticky posts array $offset = array_search($sticky_post->ID, $sticky_posts); unset($sticky_posts[$offset]); } } // If any posts have been excluded specifically, Ignore those that are sticky. if (!empty($sticky_posts) && !empty($q['post__not_in'])) { $sticky_posts = array_diff($sticky_posts, $q['post__not_in']); } // Fetch sticky posts that weren't in the query results if (!empty($sticky_posts)) { $stickies = get_posts(array('post__in' => $sticky_posts, 'post_type' => $post_type, 'post_status' => 'publish', 'nopaging' => true)); foreach ($stickies as $sticky_post) { $sticky_post->is_sticky = true; array_splice($posts, $sticky_offset, 0, array($sticky_post)); $sticky_offset++; } } return $posts; }
/** * Remove some submenus and add some custom styles * * @package WP Idea Stream * @subpackage admin/admin * * @since 2.0.0 * * @uses remove_submenu_page() to remove a page to admin menu * @uses wp_idea_stream_is_admin() to check if on an IdeaStream Administration screen * @uses wp_idea_stream_is_rating_disabled() to check if ratings are enabled * @return string CSS output */ public function admin_head() { // Remove the fake Settings submenu remove_submenu_page('options-general.php', 'ideastream'); // Remove the About & credits pages from menu remove_submenu_page('index.php', 'about-ideastream'); remove_submenu_page('index.php', 'credits-ideastream'); //Generate help if one is available for the current screen if (wp_idea_stream_is_admin() || !empty($this->is_plugin_settings)) { $screen = get_current_screen(); if (!empty($screen->id) && !$screen->get_help_tabs()) { $help_tabs_list = $this->get_help_tabs($screen->id); if (!empty($help_tabs_list)) { // Loop through tabs foreach ($help_tabs_list as $key => $help_tabs) { // Make sure types are a screen method if (!in_array($key, array('add_help_tab', 'set_help_sidebar'))) { continue; } foreach ($help_tabs as $help_tab) { $content = ''; if (empty($help_tab['content']) || !is_array($help_tab['content'])) { continue; } if (!empty($help_tab['strong'])) { $content .= '<p><strong>' . $help_tab['strong'] . '</strong></p>'; } foreach ($help_tab['content'] as $tab_content) { if (is_array($tab_content)) { $content .= '<ul><li>' . join('</li><li>', $tab_content) . '</li></ul>'; } else { $content .= '<p>' . $tab_content . '</p>'; } } $help_tab['content'] = $content; if ('add_help_tab' == $key) { $screen->add_help_tab($help_tab); } else { $screen->set_help_sidebar($content); } } } } } } // Add some css ?> <style type="text/css" media="screen"> /*<![CDATA[*/ /* Bubble style for Main Post type menu */ #adminmenu .wp-menu-open.menu-icon-<?php echo $this->post_type; ?> .awaiting-mod { background-color: #2ea2cc; color: #fff; } .about-wrap .wp-idea-stream-badge { font: normal 150px/1 'dashicons' !important; /* Better Font Rendering =========== */ -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; color: #000; display: inline-block; content:''; } .wp-idea-stream-badge:before{ content: "\f339"; } .about-wrap .wp-idea-stream-badge { position: absolute; top: 0; right: 0; } body.rtl .about-wrap .wp-idea-stream-badge { right: auto; left: 0; } .ideastream-credits { position:relative; float:left; margin-right:15px; } .ideastream-credits img.gravatar { width:150px; height:150px; } .dashboard_page_credits-ideastream .changelog { clear:both; overflow: hidden; } #wp-idea-stream-csv span.dashicons-media-spreadsheet { vertical-align: text-bottom; } <?php if (wp_idea_stream_is_admin() && !wp_idea_stream_is_rating_disabled()) { ?> /* Rating stars in screen options and in ideas WP List Table */ .metabox-prefs .idea-rating-bubble:before, th .idea-rating-bubble:before, .metabox-prefs .idea-group-bubble:before, th .idea-group-bubble:before { font: normal 20px/.5 'dashicons'; speak: none; display: inline-block; padding: 0; top: 4px; left: -4px; position: relative; vertical-align: top; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; text-decoration: none !important; color: #444; } th .idea-rating-bubble:before, .metabox-prefs .idea-rating-bubble:before { content: '\f155'; } .metabox-prefs .idea-group-bubble:before, th .idea-group-bubble:before { content: '\f307'; } .metabox-prefs .idea-rating-bubble:before, .metabox-prefs .idea-group-bubble:before { vertical-align: baseline; } /* Rates management */ #wp_idea_stream_ratings_box ul.admin-idea-rates { width: 100%; list-style: none; clear: both; margin: 0; padding: 0; } #wp_idea_stream_ratings_box ul.admin-idea-rates li { list-style: none; overflow: hidden; position: relative; padding:15px 0; border-bottom:dotted 1px #ccc; } #wp_idea_stream_ratings_box ul.admin-idea-rates li:last-child { border:none; } #wp_idea_stream_ratings_box ul.admin-idea-rates li div.admin-idea-rates-star { float:left; } #wp_idea_stream_ratings_box ul.admin-idea-rates li div.admin-idea-rates-star { width:20%; font-weight: bold; } #wp_idea_stream_ratings_box ul.admin-idea-rates li div.admin-idea-rates-users { margin-left: 20%; } #wp_idea_stream_ratings_box ul.admin-idea-rates li div.admin-idea-rates-users span.user-rated { display:inline-block; margin:5px; padding:5px; -webkit-box-shadow: 0 1px 1px 1px rgba(0,0,0,0.1); box-shadow: 0 1px 1px 1px rgba(0,0,0,0.1); } #wp_idea_stream_ratings_box ul.admin-idea-rates li div.admin-idea-rates-users a.del-rate { text-decoration: none; } #wp_idea_stream_ratings_box ul.admin-idea-rates li div.admin-idea-rates-users a.del-rate div { vertical-align: baseline; } <?php } ?> /*]]>*/ </style> <?php }
/** * Make sure ideas comments are not mixed with posts ones * * @package WP Idea Stream * @subpackage comments/classes * * @since 2.0.0 * * @param array $pieces * @param WP_Comment_Query $wp_comment_query * @uses wp_idea_stream_is_admin() to make sure it's an Idea Administration screen * @uses WP_Idea_Stream_Comments::comments_query_pieces() to build the new pieces * @return array $pieces */ public function maybe_alter_comments_query($pieces = array(), $wp_comment_query = null) { // Bail if Ajax if (defined('DOING_AJAX') && DOING_AJAX) { return $pieces; } /* Bail if not the ideas post type */ if ($this->post_type == $wp_comment_query->query_vars['post_type'] || wp_idea_stream_is_admin()) { return $pieces; } /* Bail if strip ideas query var is not set on front */ if (!is_admin() && empty($wp_comment_query->query_vars['strip_ideas'])) { return $pieces; } // Override pieces return array_merge($pieces, self::comments_query_pieces($pieces)); }
/** * Order the ideas by rates when requested * * This function is hooking to WordPress 'posts_clauses' filter. As the * rating query is first built by using a specific WP_Meta_Query, we need * to also make sure the ORDER BY clause of the sql query is customized. * * @package WP Idea Stream * @subpackage core/functions * * @since 2.0.0 * * @param array $clauses the idea query sql parts * @param WP_Query $wp_query the WordPress query object * @uses wp_idea_stream_is_ideastream() to check it's front end plugin's territory * @uses wp_idea_stream_is_admin() to check it's back end plugin's territory * @uses wp_idea_stream_is_orderby() to check the rates count is the requested order * @return array new order clauses if needed */ function wp_idea_stream_set_rates_count_orderby($clauses = array(), $wp_query = null) { if ((wp_idea_stream_is_ideastream() || wp_idea_stream_is_admin() || wp_idea_stream_get_idea_var('rating_widget')) && wp_idea_stream_is_orderby('rates_count')) { preg_match('/\\(?(\\S*).meta_key = \'_ideastream_average_rate\'/', $clauses['where'], $matches); if (!empty($matches[1])) { // default order $order = 'DESC'; // Specific case for IdeaStream administration. if (!empty($clauses['orderby']) && 'ASC' == strtoupper(substr($clauses['orderby'], -3))) { $order = 'ASC'; } $clauses['orderby'] = "{$matches[1]}.meta_value + 0 {$order}"; } } return $clauses; }
/** * Loads the needed scripts for the Groups autocomplete control * * @package WP Idea Stream * @subpackage buddypress/groups * * @since 2.0.0 * * @param string $hooksuffix the admin page being loaded * @uses wp_idea_stream_is_admin() to check for any IdeaStream Administration screens * @uses bp_loggedin_user_id() to get current user's ID * @uses get_post_field() to get the idea author * @uses wp_enqueue_script() to add the script to WordPress queue * @uses wp_idea_stream_get_js_script() to get a specific javascript * @uses wp_idea_stream_get_version() to get plugin's version * @uses wp_localize_script() to internatianlize data used in the script */ public function admin_scripts($hooksuffix = '') { if (!in_array($hooksuffix, array('post-new.php', 'post.php')) || !wp_idea_stream_is_admin()) { return; } $js_vars = array('is_admin' => 1, 'author' => bp_loggedin_user_id()); if (!empty($_GET['post'])) { $js_vars['author'] = get_post_field('post_author', absint($_GET['post'])); } wp_enqueue_script('wp-idea-stream-admin-script', wp_idea_stream_get_js_script('script'), array('jquery', 'wp-ajax-response', 'jquery-ui-autocomplete'), wp_idea_stream_get_version(), true); wp_localize_script('wp-idea-stream-admin-script', 'wp_idea_stream_vars', $js_vars); }
/** * Disjoin comment count bubbles * * The goal here is to make sure the ajax bubbles count update * are dissociated between posts and ideas * * @package WP Idea Stream * @subpackage admin/comments * * @since 2.0.0 * * @uses wp_idea_stream_is_admin() to check if on an IdeaStream Administration screen * @uses get_current_screen() to get the current screen object * @return string JS output */ public function disjoin_post_bubbles() { if (!wp_idea_stream_is_admin()) { return; } ?> <script type="text/javascript"> /* <![CDATA[ */ ( function( $ ) { <?php if ('edit-comments' == get_current_screen()->id) { ?> // Neutralize post bubbles $( 'span.pending-count' ).each( function() { original = $( this ).prop( 'class' ); $( this ).prop( 'class', original.replace( 'pending-count', 'pending-count-post' ) ) } ); // Activate idea bubbles $( 'span.pending-count-idea' ).each( function() { original = $( this ).prop( 'class' ); $( this ).prop( 'class', original.replace( 'pending-count-idea', 'pending-count' ) ) } ); <?php } ?> // As WP_List_Table->comments_bubble() function is protected and no filter... last option is JS $( '.post-com-count' ).each( function() { original = $( this ).prop( 'href' ); $( this ).prop( 'href', original + '&post_type=<?php wp_idea_stream_post_type(); ?> ' ); } ); } )(jQuery); /* ]]> */ </script> <?php }