* @copyright Copyright (c) 2008 - 2010, Justin Tadlock * @link http://justintadlock.com/archives/2009/03/15/query-posts-widget-wordpress-plugin * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html */ /* Launch the plugin. */ add_action( 'plugins_loaded', 'query_posts_setup' ); /** * Initialize the plugin. This function loads the required files needed for the plugin * to run in the proper order. * * @since 0.3.0 */ function query_posts_setup() { /* Set constant path to the members plugin directory. */ define( 'QUERY_POSTS_DIR', plugin_dir_path( __FILE__ ) ); /* Load the translation of the plugin. */ load_plugin_textdomain( 'query-posts', false, '/query-posts/languages' ); /* Load the plugin's widgets. */ add_action( 'widgets_init', 'query_posts_load_widgets' ); /* Create shortcodes. */ add_action( 'init', 'query_posts_shortcodes', 11 ); } /** * Loads all the widget files at appropriate time. Calls the register function for each widget. * * @since 0.1.0 */ function query_posts_load_widgets() { require_once( QUERY_POSTS_DIR . 'widget-query-posts.php' ); register_widget( 'Query_Posts_Widget' ); } /** * Check if specific shortcodes exist. If not, create them. * * @since 0.1.0 */ function query_posts_shortcodes() { global $shortcode_tags; if ( !is_array( $shortcode_tags ) ) return; if ( !array_key_exists( 'entry-author', $shortcode_tags ) ) add_shortcode( 'entry-author', 'query_posts_entry_author_shortcode' ); if ( !array_key_exists( 'entry-terms', $shortcode_tags ) ) add_shortcode( 'entry-terms', 'query_posts_entry_terms_shortcode' ); if ( !array_key_exists( 'entry-comments-link', $shortcode_tags ) ) add_shortcode( 'entry-comments-link', 'query_posts_entry_comments_link_shortcode' ); if ( !array_key_exists( 'entry-published', $shortcode_tags ) ) add_shortcode( 'entry-published', 'query_posts_entry_published_shortcode' ); if ( !array_key_exists( 'entry-edit-link', $shortcode_tags ) ) add_shortcode( 'entry-edit-link', 'query_posts_entry_edit_link_shortcode' ); } /** * Displays the edit link for an individual post. * * @since 0.3.0 * @param array $attr */ function query_posts_entry_edit_link_shortcode( $attr ) { global $post; $domain = 'query-posts'; $post_type = get_post_type_object( $post->post_type ); if ( !current_user_can( "edit_{$post_type->capability_type}", $post->ID ) ) return ''; $attr = shortcode_atts( array( 'before' => '', 'after' => '' ), $attr ); return $attr['before'] . '' . __( 'Edit', $domain ) . '' . $attr['after']; } /** * Displays the published date of an individual post. * * @since 0.3.0 * @param array $attr */ function query_posts_entry_published_shortcode( $attr ) { $domain = 'query-posts'; $attr = shortcode_atts( array( 'before' => '', 'after' => '', 'format' => get_option( 'date_format' ) ), $attr ); $published = '' . sprintf( get_the_time( $attr['format'] ) ) . ''; return $attr['before'] . $published . $attr['after']; } /** * Displays a post's number of comments wrapped in a link to the comments area. * * @since 0.3.0 * @param array $attr */ function query_posts_entry_comments_link_shortcode( $attr ) { $domain = 'query-posts'; $number = get_comments_number(); $attr = shortcode_atts( array( 'zero' => __( 'Leave a response', $domain ), 'one' => __( '1 Response', $domain ), 'more' => __( '%1$s Responses', $domain ), 'css_class' => 'comments-link', 'none' => '', 'before' => '', 'after' => '' ), $attr ); if ( 0 == $number && !comments_open() && !pings_open() ) { if ( $attr['none'] ) $comments_link = '' . $attr['none'] . ''; } elseif ( $number == 0 ) $comments_link = '' . $attr['zero'] . ''; elseif ( $number == 1 ) $comments_link = '' . $attr['one'] . ''; elseif ( $number > 1 ) $comments_link = '' . sprintf( $attr['more'], $number ) . ''; if ( $comments_link ) $comments_link = $attr['before'] . $comments_link . $attr['after']; return $comments_link; } /** * Displays an individual post's author with a link to his or her archive. * * @since 0.3.0 * @param array $attr */ function query_posts_entry_author_shortcode( $attr ) { $attr = shortcode_atts( array( 'before' => '', 'after' => '' ), $attr ); $author = ''; return $attr['before'] . $author . $attr['after']; } /** * Displays a list of terms for a specific taxonomy. * * @since 0.3.0 * @param array $attr */ function query_posts_entry_terms_shortcode( $attr ) { global $post; $attr = shortcode_atts( array( 'id' => $post->ID, 'taxonomy' => 'post_tag', 'separator' => ', ', 'before' => '', 'after' => '' ), $attr ); $attr['before'] = '' . $attr['before']; $attr['after'] .= ''; return get_the_term_list( $attr['id'], $attr['taxonomy'], $attr['before'], $attr['separator'], $attr['after'] ); } /** * Returns taxonomies that have $query_var set for the various post types of the current * WordPress installation. * * @since 0.3.0 * @return array $out Array of available taxonomy names. */ function query_posts_get_taxonomies() { $post_types = get_post_types( array( 'exclude_from_search' => false ), 'names' ); $post_type_taxonomies = array(); $all_taxonomies = array(); foreach ( $post_types as $post_type ) { $post_type_taxonomies = get_object_taxonomies( $post_type ); if ( is_array( $post_type_taxonomies ) ) { foreach ( $post_type_taxonomies as $taxonomy ) { $tax = get_taxonomy( $taxonomy ); if ( $tax->query_var || 'category' == $taxonomy || 'post_tag' == $taxonomy ) $all_taxonomies[] = $taxonomy; } } } $out = array_unique( $all_taxonomies ); return $out; } /** * Creates a form label with the given parameters for use with the widget. * * @since 0.3.0 * @param string $label * @param string|int $id */ function query_posts_label( $label, $id ) { echo ''; } /** * Creates a form checkbox for use with the widget. * * @since 0.3.0 * @param string $label * @param string|int $id * @param string $name * @param bool $checked */ function query_posts_input_checkbox( $label, $id, $name, $checked ) { echo "\n\t\t\t
"; echo '"; echo '
'; } /** * Creates a textarea for use with the widget * * @since 0.3.0 * @param string $label * @param string|int $id * @param string $name * @param string $value */ function query_posts_textarea( $label, $id, $name, $value ) { echo "\n\t\t\t"; query_posts_label( $label, $id ); echo ""; echo '
'; } /** * Creates a form text input for use with the widget. * * @since 0.3.0 * @param string $label * @param string|int $id * @param string $name * @param string $value */ function query_posts_input_text( $label, $id, $name, $value ) { echo "\n\t\t\t"; query_posts_label( $label, $id ); echo ""; echo '
'; } /** * Creates a small text input for use with the widget. * * @since 0.3.0 * @param string $label * @param string|int $id * @param string $name * @param string $value */ function query_posts_input_text_small( $label, $id, $name, $value ) { echo "\n\t\t\t"; query_posts_label( $label, $id ); echo ""; echo '
'; } /** * Creates a multiple slect box for use with the widget. * * @since 0.3.0 * @param string $label * @param string|int $id * @param string $name * @param string $value * @param array $options * @param book $blank_option */ function query_posts_select_multiple( $label, $id, $name, $value, $options, $blank_option ) { $value = (array) $value; if ( $blank_option && is_array( $options ) ) $options = array_merge( array( '' ), $options ); echo "\n\t\t\t"; query_posts_label( $label, $id ); echo "'; echo '
'; } /** * Creates a single slect box for use with the widget. * * @since 0.3.0 * @param string $label * @param string|int $id * @param string $name * @param string $value * @param array $options * @param book $blank_option * @param string $class Optional. * @param string $style Optional. */ function query_posts_select_single( $label, $id, $name, $value, $options, $blank_option, $class = '', $style = '' ) { $style = ( ( $style ) ? $style . ' min-width: 50px;' : 'width:100%;' ); $class = ( ( $class ) ? $class : 'widefat;' ); if ( $blank_option ) $options = array_merge( array( '' ), $options ); echo "\n\t\t\t"; query_posts_label( $label, $id ); echo "'; echo '
'; } ?>