/mfields-post-label.php

https://github.com/kaambiz/nighthawk · PHP · 218 lines · 170 code · 20 blank · 28 comment · 8 complexity · a4f0c4b6a863b809c64b5422bbe56b21 MD5 · raw file

  1. <?php
  2. /**
  3. * Post Label.
  4. *
  5. * Returns a noun representing the type or format of the global
  6. * post object. This function is used internally by the
  7. * nighthawk_entry_meta_taxonomy() function to create a sentence much
  8. * like the following: "This Status Update is filed under News."
  9. * where "Status Update" is the post label and "News" is the category.
  10. *
  11. * A "post label" can be one of three things:'
  12. * post format, custom post_type label or the mime type of an attachment.
  13. *
  14. * For "posts" having a post format, a string representing the format will be used.
  15. * If no format has been defined (assumung "standard" post format) This function
  16. * will use the term "post".
  17. *
  18. * For all other post_types, Nighthawk will use the values defined in
  19. * the post_type's "labels" array for singular and plural values.
  20. *
  21. * The output of this function may be extended by using the built-in filters:
  22. *
  23. * 'nighthawk_post_label_single' and 'nighthawk_post_label_plural'
  24. *
  25. * @return array Index "0" is the singular form while index "1" is the plural form.
  26. *
  27. * @access private
  28. * @since 1.0
  29. */
  30. class Mfields_Post_Label {
  31. const version = '2.0';
  32. const context = 'post label';
  33. static public $map = null;
  34. static public $labels = null;
  35. static public $textdomain = null;
  36. static public $count = null;
  37. static public function init( $textdomain = null ) {
  38. self::$labels = array(
  39. 'page' => array(
  40. 'standard' => _nx_noop( 'page', 'pages', self::context ),
  41. ),
  42. 'post_format' => array(
  43. 'standard' => _nx_noop( 'post', 'posts', self::context ),
  44. 'aside' => _nx_noop( 'aside', 'asides', self::context ),
  45. 'audio' => _nx_noop( 'audio file', 'audio files', self::context ),
  46. 'chat' => _nx_noop( 'chat transcript', 'chat transcripts', self::context ),
  47. 'gallery' => _nx_noop( 'gallery', 'galleries', self::context ),
  48. 'image' => _nx_noop( 'image', 'images', self::context ),
  49. 'link' => _nx_noop( 'link', 'links', self::context ),
  50. 'quote' => _nx_noop( 'quote', 'quotes', self::context ),
  51. 'status' => _nx_noop( 'status update', 'status updates', self::context ),
  52. 'video' => _nx_noop( 'video', 'videos', self::context )
  53. ),
  54. 'attachment' => array(
  55. 'standard' => _nx_noop( 'file', 'files', self::context ),
  56. 'image' => _nx_noop( 'image', 'images', self::context ),
  57. 'icon' => _nx_noop( 'icon', 'icons', self::context ),
  58. 'zip' => _nx_noop( 'zip archive', 'zip archives', self::context ),
  59. 'doc' => _nx_noop( 'document', 'documents', self::context ),
  60. 'pdf' => _nx_noop( 'PDF', 'PDFs', self::context ),
  61. 'spreadsheet' => _nx_noop( 'spreadsheet', 'spreadsheets', self::context ),
  62. 'video' => _nx_noop( 'video', 'videos', self::context ),
  63. ),
  64. );
  65. $post_types = get_post_types( array( 'public' => true, '_builtin' => false ), 'objects' );
  66. if ( ! empty( $post_types ) && is_array( $post_types ) ) {
  67. self::$labels['custom'] = array();
  68. foreach( (array) $post_types as $name => $post_type ) {
  69. self::$labels['custom'][$name] = array(
  70. 0 => $post_type->labels->singular_name,
  71. 1 => $post_type->labels->name,
  72. 2 => self::context,
  73. 'singular' => $post_type->labels->singular_name,
  74. 'plural' => $post_type->labels->name,
  75. 'context' => self::context,
  76. );
  77. }
  78. }
  79. }
  80. static public function get_label() {
  81. self::$count++;
  82. $key = get_the_ID();
  83. if ( is_tax( 'post_format' ) ) {
  84. global $wp_query;
  85. $term = get_term( $wp_query->get_queried_object(), 'post_format' );
  86. if ( isset( $term->slug ) ) {
  87. $key = str_replace( 'post-format-', '', $term->slug );
  88. }
  89. }
  90. if ( isset( self::$map[$key] ) ) {
  91. return self::$map[$key];
  92. }
  93. $post_type = get_post_type();
  94. switch( get_post_type() ) {
  95. case 'post' :
  96. $output = self::post();
  97. break;
  98. case 'page' :
  99. $output = array(
  100. 'context' => 'page',
  101. 'type' => 'standard',
  102. );
  103. break;
  104. case 'attachment' :
  105. $output = self::attachment();
  106. break;
  107. default :
  108. $output = array(
  109. 'context' => 'custom',
  110. 'type' => $post_type,
  111. );
  112. break;
  113. }
  114. $output = self::find( $output );
  115. self::$map[$key] = $output;
  116. return $output;
  117. }
  118. static public function post() {
  119. $type = get_post_format();
  120. if ( empty( $type ) ) {
  121. $type = 'standard';
  122. }
  123. return array(
  124. 'context' => 'post_format',
  125. 'type' => $type,
  126. );
  127. }
  128. static public function attachment() {
  129. $mime = get_post_mime_type();
  130. switch( $mime ) {
  131. case 'image/jpeg' :
  132. case 'image/gif' :
  133. case 'image/png' :
  134. case 'image/bmp' :
  135. case 'image/tiff' :
  136. case 'video/asf' :
  137. case 'video/avi' :
  138. case 'video/divx' :
  139. case 'video/x-flv' :
  140. case 'video/quicktime' :
  141. case 'video/mpeg' :
  142. case 'video/mp4' :
  143. case 'video/ogg' :
  144. case 'video/x-matroska' :
  145. $parts = explode( '/', $mime );
  146. $type = $parts[0];
  147. break;
  148. case 'image/x-icon' :
  149. $type = 'icon';
  150. break;
  151. case 'application/pdf' :
  152. $type = 'pdf';
  153. break;
  154. case 'application/zip' :
  155. $type = 'zip';
  156. break;
  157. case 'image/x-icon' :
  158. $type = 'icon';
  159. break;
  160. case 'application/vnd.ms-excel' :
  161. case 'application/vnd.oasis.opendocument.spreadsheet' :
  162. $type = 'doc';
  163. break;
  164. case 'application/msword' :
  165. case 'application/vnd.oasis.opendocument.text' :
  166. $type = 'doc';
  167. break;
  168. default :
  169. $type = 'default';
  170. break;
  171. }
  172. return array(
  173. 'context' => 'attachment',
  174. 'type' => $type,
  175. );
  176. }
  177. static public function find( $v ) {
  178. $v = wp_parse_args( $v, array(
  179. 'context' => null,
  180. 'type' => null,
  181. ) );
  182. extract( (array) $v );
  183. if ( isset( self::$labels[$context][$type] ) ) {
  184. return self::$labels[$context][$type];
  185. }
  186. else {
  187. return _nx_noop( 'entry', 'entries', 'post label' );
  188. }
  189. }
  190. static public function dump() {
  191. print '<pre>';
  192. print __class__ . ' v' . self::version . "\n";
  193. print "\n" . 'Times used: ' . self::$count;
  194. print "\n" . '$map: ' . print_r( self::$map, true );
  195. print "\n" . '$labels: ' . print_r( self::$labels, true );
  196. print '</pre>';
  197. }
  198. }