/app/rule/media/class-ms-rule-media-model.php

  1. <?php 
  2. /** 
  3. * Membership Media Rule class. 
  4. * 
  5. * Persisted by Membership class. 
  6. * 
  7. * @since 1.0.0 
  8. * 
  9. * @package Membership2 
  10. * @subpackage Model 
  11. */ 
  12. class MS_Rule_Media_Model extends MS_Rule { 
  13.  
  14. /** 
  15. * Rule type. 
  16. * 
  17. * @since 1.0.0 
  18. * 
  19. * @var string $rule_type 
  20. */ 
  21. protected $rule_type = MS_Rule_Media::RULE_ID; 
  22.  
  23. /** 
  24. * Media protection type constants. 
  25. * 
  26. * @since 1.0.0 
  27. * 
  28. * @var string $protection_type 
  29. */ 
  30. const PROTECTION_TYPE_BASIC = 'protection_type_basic'; 
  31. const PROTECTION_TYPE_COMPLETE = 'protection_type_complete'; 
  32. const PROTECTION_TYPE_HYBRID = 'protection_type_hybrid'; 
  33.  
  34. /** 
  35. * Media protection file change prefix. 
  36. * 
  37. * @since 1.0.0 
  38. * 
  39. * @var string 
  40. */ 
  41. const FILE_PROTECTION_PREFIX = 'ms_'; 
  42.  
  43. /** 
  44. * Media protection file seed/token. 
  45. * 
  46. * @since 1.0.0 
  47. * 
  48. * @var string 
  49. */ 
  50. const FILE_PROTECTION_INCREMENT = 2771; 
  51.  
  52. /** 
  53. * Returns the active flag for a specific rule. 
  54. * State depends on Add-on 
  55. * 
  56. * @since 1.0.0 
  57. * @return bool 
  58. */ 
  59. static public function is_active() { 
  60. return MS_Model_Addon::is_enabled( MS_Model_Addon::ADDON_MEDIA ); 
  61.  
  62.  
  63. /** 
  64. * Verify access to the current content. 
  65. * 
  66. * This rule will return NULL (not relevant), because the media-item is 
  67. * protected via the download URL and not by protecting the current page. 
  68. * 
  69. * @since 1.0.0 
  70. * 
  71. * @param string $id The content id to verify access. 
  72. * @return bool|null True if has access, false otherwise. 
  73. * Null means: Rule not relevant for current page. 
  74. */ 
  75. public function has_access( $id, $admin_has_access = true ) { 
  76. return null; 
  77.  
  78. /** 
  79. * Get the protection type available. 
  80. * 
  81. * @since 1.0.0 
  82. * 
  83. * @return array { 
  84. * The protection type => Description. 
  85. * @type string $protection_type The media protection type. 
  86. * @type string $description The media protection description. 
  87. * } 
  88. */ 
  89. public static function get_protection_types() { 
  90. $settings = MS_Factory::load( 'MS_Model_Settings' ); 
  91. $mask = $settings->downloads['masked_url']; 
  92. $example1 = MS_Helper_Utility::home_url( $mask . date( '/Y/m/' ) . 'my-image.jpg' ); 
  93. $example2 = MS_Helper_Utility::home_url( $mask . '/ms_12345.jpg' ); 
  94. $example3 = MS_Helper_Utility::home_url( $mask . '/?ms_file=ms_12345.jpg' ); 
  95. $example1 = '<br /><small>' . __( 'Example:', 'membership2' ) . ' ' . $example1 . '</small>'; 
  96. $example2 = '<br /><small>' . __( 'Example:', 'membership2' ) . ' ' . $example2 . '</small>'; 
  97. $example3 = '<br /><small>' . __( 'Example:', 'membership2' ) . ' ' . $example3 . '</small>'; 
  98.  
  99. $protection_types = array( 
  100. self::PROTECTION_TYPE_BASIC => __( 'Basic protection (default)', 'membership2' ) . $example1,  
  101. self::PROTECTION_TYPE_COMPLETE => __( 'Complete protection', 'membership2' ) . $example2,  
  102. self::PROTECTION_TYPE_HYBRID => __( 'Hybrid protection', 'membership2' ) . $example3,  
  103. ); 
  104.  
  105. return apply_filters( 
  106. 'ms_rule_media_model_get_protection_types',  
  107. $protection_types 
  108. ); 
  109.  
  110. /** 
  111. * Validate protection type. 
  112. * 
  113. * @since 1.0.0 
  114. * 
  115. * @param string $type The protection type to validate. 
  116. * @return boolean True if is valid. 
  117. */ 
  118. public static function is_valid_protection_type( $type ) { 
  119. $valid = false; 
  120. $types = self::get_protection_types(); 
  121.  
  122. if ( array_key_exists( $type, $types ) ) { 
  123. $valid = true; 
  124.  
  125. return apply_filters( 
  126. 'ms_rule_media_model_is_valid_protection_type',  
  127. $valid 
  128. ); 
  129.  
  130. /** 
  131. * Starts the output buffering to replace all links in the final HTML 
  132. * document. 
  133. * 
  134. * Related filter: 
  135. * - init 
  136. * 
  137. * @since 1.0.0 
  138. */ 
  139. public function buffer_start() { 
  140. ob_start( array( $this, 'protect_download_content' ) ); 
  141.  
  142. /** 
  143. * Ends the output buffering and calls the output_callback function. 
  144. * 
  145. * Related filter: 
  146. * - shutdown 
  147. * 
  148. * @since 1.0.0 
  149. */ 
  150. public function buffer_end() { 
  151. if ( ob_get_level() ) { 
  152. ob_end_flush(); 
  153.  
  154. /** 
  155. * Set initial protection. 
  156. * 
  157. * @since 1.0.0 
  158. */ 
  159. public function initialize() { 
  160. parent::protect_content(); 
  161.  
  162. if ( MS_Model_Addon::is_enabled( MS_Model_Addon::ADDON_MEDIA ) ) { 
  163. // Start buffering during init action, though output should only 
  164. // happen a lot later... This way we're save. 
  165. $this->add_action( 'init', 'buffer_start' ); 
  166.  
  167. // Process the buffer right in the end. 
  168. $this->add_action( 'shutdown', 'buffer_end' ); 
  169.  
  170. $this->add_action( 'parse_request', 'handle_download_protection', 3 ); 
  171.  
  172. /** 
  173. * Protect media file. 
  174. * 
  175. * Search content and mask media filename and path. 
  176. * 
  177. * This function is called as output_callback for ob_start() - as a result 
  178. * we cannot echo/output anything in this function! The return value of the 
  179. * function will be displayed to the user. 
  180. * 
  181. * @since 1.0.0 
  182. * 
  183. * @param string $the_content The content before filter. 
  184. * @return string The content with masked media url. 
  185. */ 
  186. public function protect_download_content( $the_content ) { 
  187. do_action( 
  188. 'ms_rule_media_model_protect_download_content_before',  
  189. $the_content,  
  190. $this 
  191. ); 
  192.  
  193. $download_settings = MS_Plugin::instance()->settings->downloads; 
  194.  
  195. if ( ! MS_Model_Addon::is_enabled( MS_Model_Addon::ADDON_MEDIA ) ) { 
  196. return $the_content; 
  197.  
  198. $upload_dir = wp_upload_dir(); 
  199. $original_url = trailingslashit( $upload_dir['baseurl'] ); 
  200. $new_path = trailingslashit( 
  201. trailingslashit( get_option( 'home' ) ) . 
  202. $download_settings['masked_url'] 
  203. ); 
  204.  
  205. /** 
  206. * Find all the urls in the post and then we'll check if they are protected 
  207. * Regex from http://blog.mattheworiordan.com/post/13174566389/url-regular-expression-for-links-with-or-without-the 
  208. */ 
  209. $url_exp = '/((([A-Za-z]{3, 9}:(?:\/\/)?)' . 
  210. '(?:[-;:&=\+\$, \w]+@)?'. 
  211. '[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$, \w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?' . 
  212. '\??(?:[-\+=&;%@.\w_]*)#?(?:[.\!\/\\w]*))?)/'; 
  213.  
  214. $matches = array(); 
  215. if ( preg_match_all( $url_exp, $the_content, $matches ) ) { 
  216. $home = untrailingslashit( get_option( 'home' ) ); 
  217.  
  218. /** 
  219. * $matches[0] .. Full link 'http://example.com/blog/img.png?ver=1' 
  220. * $matches[1] .. Full link 'http://example.com/blog/img.png?ver=1' 
  221. * $matches[2] .. Domain only 'http://example.com' 
  222. * $matches[3] .. Protocol 'http://' 
  223. * $matches[4] .. File path '/blog/img.png?ver=1' 
  224. */ 
  225. if ( ! empty( $matches ) && ! empty( $matches[2] ) ) { 
  226. $links = (array) $matches[0]; 
  227. $paths = (array) $matches[4]; 
  228.  
  229. foreach ( $links as $key => $link ) { 
  230. // Ignore all external links 
  231. if ( 0 !== strpos( $link, $home ) ) { continue; } 
  232.  
  233. // The file is on local site - is it a valid attachment? 
  234. $file = basename( $paths[ $key ] ); 
  235. $post_id = $this->get_attachment_id( $link ); 
  236.  
  237. // Ignore links that have no relevant wp_posts entry. 
  238. if ( empty( $post_id ) ) { continue; } 
  239. $f_info = $this->extract_file_info( $file ); 
  240.  
  241. // We have a protected file - so we'll mask it! 
  242. switch ( $download_settings['protection_type'] ) { 
  243. case self::PROTECTION_TYPE_COMPLETE: 
  244. $protected_filename = self::FILE_PROTECTION_PREFIX . 
  245. ( $post_id + (int) self::FILE_PROTECTION_INCREMENT ) . 
  246. $f_info->size_extension . 
  247. '.' . pathinfo( $f_info->filename, PATHINFO_EXTENSION ); 
  248.  
  249. $the_content = str_replace( 
  250. $link,  
  251. $new_path . $protected_filename,  
  252. $the_content 
  253. ); 
  254. break; 
  255.  
  256. case self::PROTECTION_TYPE_HYBRID: 
  257. $protected_filename = self::FILE_PROTECTION_PREFIX . 
  258. ($post_id + (int) self::FILE_PROTECTION_INCREMENT ) . 
  259. $f_info->size_extension . 
  260. '.' . pathinfo( $f_info->filename, PATHINFO_EXTENSION ); 
  261.  
  262. $the_content = str_replace( 
  263. $link,  
  264. $new_path . '?ms_file=' . $protected_filename,  
  265. $the_content 
  266. ); 
  267. break; 
  268.  
  269. case self::PROTECTION_TYPE_BASIC: 
  270. default: 
  271. $the_content = str_replace( 
  272. $link,  
  273. str_replace( 
  274. $original_url,  
  275. $new_path,  
  276. $link 
  277. ),  
  278. $the_content 
  279. ); 
  280. break; 
  281.  
  282. return apply_filters( 
  283. 'ms_rule_media_model_protect_download_content',  
  284. $the_content,  
  285. $this 
  286. ); 
  287.  
  288. /** 
  289. * Extract filename and size extension info. 
  290. * 
  291. * @since 1.0.0 
  292. * 
  293. * @param string $file The filename to extract info from. 
  294. * @return array { 
  295. * @type string $filename The filename. 
  296. * @type string $size_extension The wordpress thumbnail size extension. Default empty. 
  297. * } 
  298. */ 
  299. public function extract_file_info( $file ) { 
  300. // See if the filename has a size extension and if so, strip it out 
  301. $filename_exp_full = '/(.+)\-(\d+[x]\d+)\.(.+)$/'; 
  302. $filename_exp_min = '/(.+)\.(.+)$/'; 
  303. $filematch = array(); 
  304.  
  305. if ( preg_match( $filename_exp_full, $file, $filematch ) ) { 
  306. // Image with an image size attached 
  307. $type = strtolower( $filematch[3] ); 
  308. $filename = $filematch[1] . '.' . $type; 
  309. $size_extension = '-' . $filematch[2]; 
  310. } elseif ( preg_match( $filename_exp_min, $file, $filematch ) ) { 
  311. // Image without an image size definition 
  312. $type = strtolower( $filematch[2] ); 
  313. $filename = $filematch[1] . '.' . $type; 
  314. $size_extension = ''; 
  315. } else { 
  316. // Image without an extension. 
  317. $type = ''; 
  318. $filename = $file; 
  319. $size_extension = ''; 
  320.  
  321. $info = (object) array( 
  322. 'filename' => $filename,  
  323. 'size_extension' => $size_extension,  
  324. 'type' => $type,  
  325. ); 
  326.  
  327. return apply_filters( 
  328. 'ms_rule_media_model_extract_file_info',  
  329. $info,  
  330. $file,  
  331. $this 
  332. ); 
  333.  
  334. /** 
  335. * Get attachment post_id using the filename. 
  336. * 
  337. * @since 1.0.0 
  338. * 
  339. * @param string $filename The filename to obtain the post_id. 
  340. * @return int The post ID or 0 if not found. 
  341. */ 
  342. public function get_attachment_id( $url ) { 
  343. static $Uploads_Url = null; 
  344. static $Uploads_Url_Len = 0; 
  345. global $wpdb; 
  346.  
  347. // First let WordPress try to find the Attachment ID. 
  348. $id = url_to_postid( $url ); 
  349.  
  350. if ( $id ) { 
  351. // Make sure the result ID is a valid attachment ID. 
  352. if ( 'attachment' != get_post_type( $id ) ) { 
  353. $id = 0; 
  354. } else { 
  355. // Manual attempt: Get the filename from the URL and use a custom query. 
  356.  
  357. if ( null === $Uploads_Url ) { 
  358. $uploads = wp_upload_dir(); 
  359. $Uploads_Url = trailingslashit( $uploads['baseurl'] ); 
  360. $Uploads_Url_Len = strlen( $Uploads_Url ); 
  361.  
  362. if ( false !== strpos( $url, $Uploads_Url ) ) { 
  363. $url = substr( $url, $Uploads_Url_Len ); 
  364.  
  365. // See if we cached that URL already. 
  366. $id = wp_cache_get( $url, 'ms_attachment_id' ); 
  367.  
  368. if ( empty( $id ) ) { 
  369. $sql = " 
  370. SELECT wposts.ID 
  371. FROM $wpdb->posts wposts 
  372. INNER JOIN $wpdb->postmeta wpostmeta ON wposts.ID = wpostmeta.post_id 
  373. WHERE 
  374. wposts.post_type = 'attachment' 
  375. AND wpostmeta.meta_key = '_wp_attached_file' 
  376. AND wpostmeta.meta_value = %s 
  377. "; 
  378. $sql = $wpdb->prepare( $sql, $url ); 
  379. $id = $wpdb->get_var( $sql ); 
  380.  
  381. wp_cache_set( $url, $id, 'ms_attachment_id' ); 
  382.  
  383. return apply_filters( 
  384. 'ms_rule_get_attachment_id',  
  385. absint( $id ),  
  386. $url,  
  387. $this 
  388. ); 
  389.  
  390. /** 
  391. * Handle protected media access. 
  392. * 
  393. * Search for masked file and show the proper content, or no access image if don't have access. 
  394. * 
  395. * Realted Action Hooks: 
  396. * - parse_request 
  397. * 
  398. * @since 1.0.0 
  399. * 
  400. * @param WP_Query $query The WP_Query object to filter. 
  401. */ 
  402. public function handle_download_protection( $query ) { 
  403. do_action( 
  404. 'ms_rule_media_model_handle_download_protection_before',  
  405. $query,  
  406. $this 
  407. ); 
  408.  
  409. $the_file = false; 
  410. $requested_item = false; 
  411. $download_settings = MS_Plugin::instance()->settings->downloads; 
  412. $protection_type = $download_settings['protection_type']; 
  413.  
  414. if ( ! MS_Model_Addon::is_enabled( MS_Model_Addon::ADDON_MEDIA ) ) { 
  415. return; 
  416.  
  417. if ( ! empty( $query->query_vars['protectedfile'] ) ) { 
  418. $requested_item = explode( '/', $query->query_vars['protectedfile'] ); 
  419. $requested_item = array_pop( $requested_item ); 
  420. } elseif ( ! empty( $_GET['ms_file'] ) 
  421. && self::PROTECTION_TYPE_HYBRID == $protection_type 
  422. ) { 
  423. $requested_item = $_GET['ms_file']; 
  424.  
  425. if ( ! empty( $requested_item ) ) { 
  426. // At this point we know that the requested post is an attachment. 
  427. $f_info = $this->extract_file_info( $requested_item ); 
  428.  
  429. switch ( $protection_type ) { 
  430. case self::PROTECTION_TYPE_COMPLETE: 
  431. case self::PROTECTION_TYPE_HYBRID: 
  432. // Work out the post_id again 
  433. $attachment_id = preg_replace( 
  434. '/^' . self::FILE_PROTECTION_PREFIX . '/',  
  435. '',  
  436. $f_info->filename 
  437. ); 
  438. $attachment_id -= (int) self::FILE_PROTECTION_INCREMENT; 
  439.  
  440. $the_file = $this->restore_filename( $attachment_id, $f_info->size_extension ); 
  441. break; 
  442.  
  443. default: 
  444. case self::PROTECTION_TYPE_BASIC: 
  445. $home = untrailingslashit( get_option( 'home' ) ); 
  446. $attachment_id = $this->get_attachment_id( $home . $f_info->filename ); 
  447. $the_file = $this->restore_filename( $attachment_id, $f_info->size_extension ); 
  448. break; 
  449.  
  450. if ( ! empty( $the_file ) 
  451. && ! empty( $attachment_id ) 
  452. && is_numeric( $attachment_id ) 
  453. ) { 
  454. if ( $this->can_access_file( $attachment_id ) ) { 
  455. $upload_dir = wp_upload_dir(); 
  456. $file = trailingslashit( $upload_dir['basedir'] ) . $the_file; 
  457. $this->output_file( $file ); 
  458. } else { 
  459. $this->show_no_access_image(); 
  460.  
  461. do_action( 
  462. 'ms_rule_media_model_handle_download_protection_after',  
  463. $query,  
  464. $this 
  465. ); 
  466.  
  467. /** 
  468. * Checks if the current user can access the specified attachment. 
  469. * 
  470. * @since 1.0.0 
  471. * @param int $attachment_id 
  472. * @return bool 
  473. */ 
  474. public function can_access_file( $attachment_id ) { 
  475. $access = false; 
  476.  
  477. if ( MS_Model_Member::is_normal_admin() ) { 
  478. return true; 
  479.  
  480. /** 
  481. * Default protection mode: 
  482. * Protect Attachments based on the parent post. 
  483. */ 
  484. $parent_id = get_post_field( 'post_parent', $attachment_id ); 
  485.  
  486. if ( ! $parent_id ) { 
  487. $access = true; 
  488. } else { 
  489. $member = MS_Model_Member::get_current_member(); 
  490. foreach ( $member->subscriptions as $subscription ) { 
  491. $membership = $subscription->get_membership(); 
  492. $access = $membership->has_access_to_post( $parent_id ); 
  493. if ( $access ) { break; } 
  494.  
  495. return apply_filters( 
  496. 'ms_rule_media_can_access_file',  
  497. $access,  
  498. $attachment_id 
  499. ); 
  500.  
  501. /** 
  502. * Restore filename from post_id. 
  503. * 
  504. * @since 1.0.0 
  505. * 
  506. * @todo refactory hack to get extension. 
  507. * 
  508. * @param int $post_id The attachment post_id. 
  509. * @param string $size_extension The image size extension. 
  510. * @return string The attachment filename. 
  511. */ 
  512. public function restore_filename( $post_id, $size_extension ) { 
  513. $img_filename = null; 
  514.  
  515. if ( ! empty( $post_id ) && is_numeric( $post_id ) ) { 
  516. $img_filename = get_post_meta( $post_id, '_wp_attached_file', true ); 
  517. if ( ! empty( $size_extension ) ) { 
  518. // Add back in a size extension if we need to 
  519. $img_filename = str_replace( 
  520. '.' . pathinfo( $img_filename, PATHINFO_EXTENSION ),  
  521. $size_extension . '.' . pathinfo( $img_filename, PATHINFO_EXTENSION ),  
  522. $img_filename 
  523. ); 
  524.  
  525. // hack to remove any double extensions :/ need to change when work out a neater way 
  526. $img_filename = str_replace( 
  527. $size_extension . $size_extension,  
  528. $size_extension,  
  529. $img_filename 
  530. ); 
  531.  
  532. return apply_filters( 
  533. 'ms_rule_restore_filename',  
  534. $img_filename,  
  535. $post_id,  
  536. $this 
  537. ); 
  538.  
  539. /** 
  540. * Output file to the browser. 
  541. * 
  542. * @since 1.0.0 
  543. * 
  544. * @param string $file The complete path to the file. 
  545. */ 
  546. private function output_file( $file ) { 
  547. do_action( 'ms_rule_media_model_output_file_before', $file, $this ); 
  548.  
  549. if ( ! is_file( $file ) ) { 
  550. status_header( 404 ); 
  551. die( '404 — File not found.' ); 
  552.  
  553. $mime = wp_check_filetype( $file ); 
  554. if ( empty( $mime['type'] ) && function_exists( 'mime_content_type' ) ) { 
  555. $mime['type'] = mime_content_type( $file ); 
  556.  
  557. if ( $mime['type'] ) { 
  558. $mimetype = $mime['type']; 
  559. } else { 
  560. $mimetype = 'image/' . substr( $file, strrpos( $file, '.' ) + 1 ); 
  561.  
  562. header( 'Content-type: ' . $mimetype ); 
  563. if ( false === strpos( $_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS' ) ) { 
  564. header( 'Content-Length: ' . filesize( $file ) ); 
  565.  
  566. $last_modified = gmdate( 'D, d M Y H:i:s', filemtime( $file ) ); 
  567. $etag = '"' . md5( $last_modified ) . '"'; 
  568. header( "Last-Modified: $last_modified GMT" ); 
  569. header( 'ETag: ' . $etag ); 
  570. header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', time() + 100000000 ) . ' GMT' ); 
  571.  
  572. // Support for Conditional GET 
  573. if ( isset( $_SERVER['HTTP_IF_NONE_MATCH'] ) ) { 
  574. $client_etag = stripslashes( $_SERVER['HTTP_IF_NONE_MATCH'] ); 
  575. } else { 
  576. $client_etag = false; 
  577.  
  578. if ( ! isset( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) { 
  579. $_SERVER['HTTP_IF_MODIFIED_SINCE'] = false; 
  580.  
  581. $client_last_modified = trim( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ); 
  582. // If string is empty, return 0. If not, attempt to parse into a timestamp 
  583. $client_modified_timestamp = $client_last_modified ? strtotime( $client_last_modified ) : 0; 
  584.  
  585. // Make a timestamp for our most recent modification... 
  586. $modified_timestamp = strtotime( $last_modified ); 
  587.  
  588. if ( $client_last_modified && $client_etag ) { 
  589. $valid_etag = ( $client_modified_timestamp >= $modified_timestamp ) 
  590. && ( $client_etag === $etag ); 
  591. } else { 
  592. $valid_etag = ( $client_modified_timestamp >= $modified_timestamp ) 
  593. || ( $client_etag === $etag ); 
  594.  
  595. if ( $valid_etag ) { 
  596. status_header( 304 ); 
  597. exit; 
  598.  
  599. // If we made it this far, just serve the file 
  600. readfile( $file ); 
  601.  
  602. do_action( 'ms_rule_media_model_output_file_after', $file, $this ); 
  603.  
  604. die(); 
  605.  
  606. /** 
  607. * Show no access image. 
  608. * 
  609. * @since 1.0.0 
  610. */ 
  611. private function show_no_access_image() { 
  612. $no_access_file = apply_filters( 
  613. 'ms_rule_media_model_show_no_access_image_path',  
  614. MS_Plugin::instance()->dir . 'app/assets/images/no-access.png' 
  615. ); 
  616.  
  617. $this->output_file( $no_access_file ); 
  618.  
  619. do_action( 'ms_rule_show_no_access_image_after', $this ); 
  620.  
  621. /** 
  622. * Get content to protect. 
  623. * 
  624. * @since 1.0.0 
  625. * @param $args The query post args 
  626. * @see @link http://codex.wordpress.org/Class_Reference/WP_Query 
  627. * @return array The contents array. 
  628. */ 
  629. public function get_contents( $args = null ) { 
  630. $args = self::get_query_args( $args ); 
  631. $posts = get_posts( $args ); 
  632.  
  633. $contents = array(); 
  634. foreach ( $posts as $content ) { 
  635. $content->id = $content->ID; 
  636. $content->type = MS_Rule_Media::RULE_ID; 
  637. $content->name = $content->post_name; 
  638. $content->access = $this->can_access_file( $content->id ); 
  639.  
  640. $contents[ $content->id ] = $content; 
  641.  
  642. return apply_filters( 
  643. 'ms_rule_media_model_get_contents',  
  644. $contents,  
  645. $args,  
  646. $this 
  647. ); 
  648.  
  649. /** 
  650. * Get the total content count. 
  651. * 
  652. * @since 1.0.0 
  653. * 
  654. * @param $args The query post args 
  655. * @see @link http://codex.wordpress.org/Class_Reference/WP_Query 
  656. * @return int The total content count. 
  657. */ 
  658. public function get_content_count( $args = null ) { 
  659. $args = self::get_query_args( $args ); 
  660. $query = new WP_Query( $args ); 
  661.  
  662. $count = $query->found_posts; 
  663.  
  664. return apply_filters( 
  665. 'ms_rule_media_model_get_content_count',  
  666. $count,  
  667. $args 
  668. ); 
  669.  
  670. /** 
  671. * Get the default query args. 
  672. * 
  673. * @since 1.0.0 
  674. * 
  675. * @param string $args The query post args. 
  676. * @see @link http://codex.wordpress.org/Class_Reference/WP_Query 
  677. * @return array The parsed args. 
  678. */ 
  679. public function get_query_args( $args = null ) { 
  680. $defaults = array( 
  681. 'orderby' => 'post_date',  
  682. 'order' => 'DESC',  
  683. 'post_type' => 'attachment',  
  684. 'post_status' => 'any',  
  685. ); 
  686. $args = wp_parse_args( $args, $defaults ); 
  687.  
  688. return parent::prepare_query_args( $args, 'get_posts' ); 
.