[ Index ]

WordPress Cross Reference

title

Body

[close]

/wp-includes/ -> post-formats.php (source)

   1  <?php
   2  /**
   3   * Post format functions.
   4   *
   5   * @package WordPress
   6   * @subpackage Post
   7   */
   8  
   9  /**
  10   * Retrieve the format slug for a post
  11   *
  12   * @since 3.1.0
  13   *
  14   * @param int|object $post Post ID or post object. Optional, default is the current post from the loop.
  15   * @return mixed The format if successful. False otherwise.
  16   */
  17  function get_post_format( $post = null ) {
  18      if ( ! $post = get_post( $post ) )
  19          return false;
  20  
  21      if ( ! post_type_supports( $post->post_type, 'post-formats' ) )
  22          return false;
  23  
  24      $_format = get_the_terms( $post->ID, 'post_format' );
  25  
  26      if ( empty( $_format ) )
  27          return false;
  28  
  29      $format = array_shift( $_format );
  30  
  31      return str_replace('post-format-', '', $format->slug );
  32  }
  33  
  34  /**
  35   * Check if a post has any of the given formats, or any format.
  36   *
  37   * @since 3.1.0
  38   *
  39   * @uses has_term()
  40   *
  41   * @param string|array $format Optional. The format or formats to check.
  42   * @param object|int $post Optional. The post to check. If not supplied, defaults to the current post if used in the loop.
  43   * @return bool True if the post has any of the given formats (or any format, if no format specified), false otherwise.
  44   */
  45  function has_post_format( $format = array(), $post = null ) {
  46      $prefixed = array();
  47  
  48      if ( $format ) {
  49          foreach ( (array) $format as $single ) {
  50              $prefixed[] = 'post-format-' . sanitize_key( $single );
  51          }
  52      }
  53  
  54      return has_term( $prefixed, 'post_format', $post );
  55  }
  56  
  57  /**
  58   * Assign a format to a post
  59   *
  60   * @since 3.1.0
  61   *
  62   * @param int|object $post The post for which to assign a format.
  63   * @param string $format A format to assign. Use an empty string or array to remove all formats from the post.
  64   * @return mixed WP_Error on error. Array of affected term IDs on success.
  65   */
  66  function set_post_format( $post, $format ) {
  67      $post = get_post( $post );
  68  
  69      if ( empty( $post ) )
  70          return new WP_Error( 'invalid_post', __( 'Invalid post' ) );
  71  
  72      if ( ! empty( $format ) ) {
  73          $format = sanitize_key( $format );
  74          if ( 'standard' === $format || ! in_array( $format, get_post_format_slugs() ) )
  75              $format = '';
  76          else
  77              $format = 'post-format-' . $format;
  78      }
  79  
  80      return wp_set_post_terms( $post->ID, $format, 'post_format' );
  81  }
  82  
  83  /**
  84   * Returns an array of post format slugs to their translated and pretty display versions
  85   *
  86   * @since 3.1.0
  87   *
  88   * @return array The array of translated post format names.
  89   */
  90  function get_post_format_strings() {
  91      $strings = array(
  92          'standard' => _x( 'Standard', 'Post format' ), // Special case. any value that evals to false will be considered standard
  93          'aside'    => _x( 'Aside',    'Post format' ),
  94          'chat'     => _x( 'Chat',     'Post format' ),
  95          'gallery'  => _x( 'Gallery',  'Post format' ),
  96          'link'     => _x( 'Link',     'Post format' ),
  97          'image'    => _x( 'Image',    'Post format' ),
  98          'quote'    => _x( 'Quote',    'Post format' ),
  99          'status'   => _x( 'Status',   'Post format' ),
 100          'video'    => _x( 'Video',    'Post format' ),
 101          'audio'    => _x( 'Audio',    'Post format' ),
 102      );
 103      return $strings;
 104  }
 105  
 106  /**
 107   * Retrieves an array of post format slugs.
 108   *
 109   * @since 3.1.0
 110   *
 111   * @uses get_post_format_strings()
 112   *
 113   * @return array The array of post format slugs.
 114   */
 115  function get_post_format_slugs() {
 116      $slugs = array_keys( get_post_format_strings() );
 117      return array_combine( $slugs, $slugs );
 118  }
 119  
 120  /**
 121   * Returns a pretty, translated version of a post format slug
 122   *
 123   * @since 3.1.0
 124   *
 125   * @uses get_post_format_strings()
 126   *
 127   * @param string $slug A post format slug.
 128   * @return string The translated post format name.
 129   */
 130  function get_post_format_string( $slug ) {
 131      $strings = get_post_format_strings();
 132      if ( !$slug )
 133          return $strings['standard'];
 134      else
 135          return ( isset( $strings[$slug] ) ) ? $strings[$slug] : '';
 136  }
 137  
 138  /**
 139   * Returns a link to a post format index.
 140   *
 141   * @since 3.1.0
 142   *
 143   * @param string $format The post format slug.
 144   * @return string The post format term link.
 145   */
 146  function get_post_format_link( $format ) {
 147      $term = get_term_by('slug', 'post-format-' . $format, 'post_format' );
 148      if ( ! $term || is_wp_error( $term ) )
 149          return false;
 150      return get_term_link( $term );
 151  }
 152  
 153  /**
 154   * Filters the request to allow for the format prefix.
 155   *
 156   * @access private
 157   * @since 3.1.0
 158   */
 159  function _post_format_request( $qvs ) {
 160      if ( ! isset( $qvs['post_format'] ) )
 161          return $qvs;
 162      $slugs = get_post_format_slugs();
 163      if ( isset( $slugs[ $qvs['post_format'] ] ) )
 164          $qvs['post_format'] = 'post-format-' . $slugs[ $qvs['post_format'] ];
 165      $tax = get_taxonomy( 'post_format' );
 166      if ( ! is_admin() )
 167          $qvs['post_type'] = $tax->object_type;
 168      return $qvs;
 169  }
 170  add_filter( 'request', '_post_format_request' );
 171  
 172  /**
 173   * Filters the post format term link to remove the format prefix.
 174   *
 175   * @access private
 176   * @since 3.1.0
 177   */
 178  function _post_format_link( $link, $term, $taxonomy ) {
 179      global $wp_rewrite;
 180      if ( 'post_format' != $taxonomy )
 181          return $link;
 182      if ( $wp_rewrite->get_extra_permastruct( $taxonomy ) ) {
 183          return str_replace( "/{$term->slug}", '/' . str_replace( 'post-format-', '', $term->slug ), $link );
 184      } else {
 185          $link = remove_query_arg( 'post_format', $link );
 186          return add_query_arg( 'post_format', str_replace( 'post-format-', '', $term->slug ), $link );
 187      }
 188  }
 189  add_filter( 'term_link', '_post_format_link', 10, 3 );
 190  
 191  /**
 192   * Remove the post format prefix from the name property of the term object created by get_term().
 193   *
 194   * @access private
 195   * @since 3.1.0
 196   */
 197  function _post_format_get_term( $term ) {
 198      if ( isset( $term->slug ) ) {
 199          $term->name = get_post_format_string( str_replace( 'post-format-', '', $term->slug ) );
 200      }
 201      return $term;
 202  }
 203  add_filter( 'get_post_format', '_post_format_get_term' );
 204  
 205  /**
 206   * Remove the post format prefix from the name property of the term objects created by get_terms().
 207   *
 208   * @access private
 209   * @since 3.1.0
 210   */
 211  function _post_format_get_terms( $terms, $taxonomies, $args ) {
 212      if ( in_array( 'post_format', (array) $taxonomies ) ) {
 213          if ( isset( $args['fields'] ) && 'names' == $args['fields'] ) {
 214              foreach( $terms as $order => $name ) {
 215                  $terms[$order] = get_post_format_string( str_replace( 'post-format-', '', $name ) );
 216              }
 217          } else {
 218              foreach ( (array) $terms as $order => $term ) {
 219                  if ( isset( $term->taxonomy ) && 'post_format' == $term->taxonomy ) {
 220                      $terms[$order]->name = get_post_format_string( str_replace( 'post-format-', '', $term->slug ) );
 221                  }
 222              }
 223          }
 224      }
 225      return $terms;
 226  }
 227  add_filter( 'get_terms', '_post_format_get_terms', 10, 3 );
 228  
 229  /**
 230   * Remove the post format prefix from the name property of the term objects created by wp_get_object_terms().
 231   *
 232   * @access private
 233   * @since 3.1.0
 234   */
 235  function _post_format_wp_get_object_terms( $terms ) {
 236      foreach ( (array) $terms as $order => $term ) {
 237          if ( isset( $term->taxonomy ) && 'post_format' == $term->taxonomy ) {
 238              $terms[$order]->name = get_post_format_string( str_replace( 'post-format-', '', $term->slug ) );
 239          }
 240      }
 241      return $terms;
 242  }
 243  add_filter( 'wp_get_object_terms', '_post_format_wp_get_object_terms' );


Generated: Tue Mar 25 01:41:18 2014 WordPress honlapkészítés: online1.hu