MS_Model_CustomPostType

Abstract Custom Post Type model.

Defined (1)

The class is defined in the following location(s).

/app/model/class-ms-model-customposttype.php  
  1. class MS_Model_CustomPostType extends MS_Model { 
  2.  
  3. /** 
  4. * Model custom post type. 
  5. * Both static and class property are used to handle php 5.2 limitations. 
  6. * Override this value in child object. 
  7. * @since 1.0.0 
  8. * @var string 
  9. */ 
  10. protected static $POST_TYPE = ''; 
  11.  
  12. /** 
  13. * ID of the model object. 
  14. * Saved as WP post ID. 
  15. * @since 1.0.0 
  16. * @var int 
  17. */ 
  18. protected $id = 0; 
  19.  
  20. /** 
  21. * Model name (this is the post slug) 
  22. * @since 1.0.0 
  23. * @var string 
  24. */ 
  25. protected $name = ''; 
  26.  
  27. /** 
  28. * Model title. 
  29. * Saved in $post->post_title. 
  30. * @since 1.0.0 
  31. * @var string 
  32. */ 
  33. protected $title = ''; 
  34.  
  35. /** 
  36. * Model description. 
  37. * Saved in $post->post_content and $post->excerpt. 
  38. * @since 1.0.0 
  39. * @var string 
  40. */ 
  41. protected $description = ''; 
  42.  
  43. /** 
  44. * The user ID of the owner. 
  45. * Saved in $post->post_author 
  46. * @since 1.0.0 
  47. * @var int 
  48. */ 
  49. protected $user_id = 0; 
  50.  
  51. /** 
  52. * The last modified date. 
  53. * Saved in $post->post_modified 
  54. * @since 1.0.0 
  55. * @var string 
  56. */ 
  57. protected $post_modified = ''; 
  58.  
  59. /** 
  60. * Custom data can be used by other plugins via the set_custom_data() and 
  61. * get_custom_data() functions. 
  62. * This can be used to store data that other plugins use to store object 
  63. * related information, like affiliate options for a membership, etc. 
  64. * @since 1.0.0 
  65. * @var array 
  66. */ 
  67. protected $custom_data = array(); 
  68.  
  69. /** 
  70. * Not persisted fields. 
  71. * @since 1.0.0 
  72. * @var array 
  73. */ 
  74. static public $ignore_fields = array(); 
  75.  
  76. /** 
  77. * Validates the object right after it was loaded/initialized. 
  78. * We ensure that the custom_data field is an array. 
  79. * @since 1.0.0 
  80. */ 
  81. public function prepare_obj() { 
  82. parent::prepare_obj(); 
  83.  
  84. if ( ! is_array( $this->custom_data ) ) { 
  85. $this->custom_data = array(); 
  86.  
  87. /** 
  88. * Save content in wp tables (wp_post and wp_postmeta). 
  89. * Update WP cache. 
  90. * @since 1.0.0 
  91. */ 
  92. public function save() { 
  93. MS_Factory::select_blog(); 
  94. $this->before_save(); 
  95.  
  96. $this->post_modified = MS_Helper_Period::current_date( 'Y-m-d H:i:s' ); 
  97. $class = get_class( $this ); 
  98.  
  99. /** 
  100. * Serialize data that is later saved to the postmeta table. 
  101. * While data is serialized it can also modify the model data before 
  102. * writing it to the posts table. 
  103. */ 
  104. $data = MS_Factory::serialize_model( $this ); 
  105.  
  106. $post = array( 
  107. 'comment_status' => 'closed',  
  108. 'ping_status' => 'closed',  
  109. 'post_author' => $this->user_id,  
  110. 'post_content' => $this->description,  
  111. 'post_excerpt' => $this->description,  
  112. 'post_name' => sanitize_text_field( $this->name ),  
  113. 'post_status' => 'private',  
  114. 'post_title' => sanitize_title( ! empty( $this->title ) ? $this->title : $this->name ),  
  115. 'post_type' => $this->get_post_type(),  
  116. 'post_modified' => $this->post_modified,  
  117. ); 
  118.  
  119. /** 
  120. * Give child classes an easy way to modify the post and meta data right 
  121. * before it is saved. 
  122. * @since 1.0.1.0 
  123. */ 
  124. $post = $this->save_post_data( $post ); 
  125. $data = $this->save_meta_data( $data ); 
  126.  
  127. if ( empty( $this->id ) ) { 
  128. $this->id = wp_insert_post( $post ); 
  129. } else { 
  130. $post[ 'ID' ] = $this->id; 
  131. wp_update_post( $post ); 
  132.  
  133. // We first remove any metadata of our custom post type that is not 
  134. // contained in the serialized data collection. 
  135. $this->clean_metadata( array_keys( $data ) ); 
  136.  
  137. // Then we update all meta fields that are inside the collection 
  138. foreach ( $data as $field => $val ) { 
  139. update_post_meta( $this->id, $field, $val ); 
  140.  
  141. wp_cache_set( $this->id, $this, $class ); 
  142. $this->after_save(); 
  143. MS_Factory::revert_blog(); 
  144.  
  145. global $wp_current_filter; 
  146. if ( ! in_array( 'ms_saved_' . $class, $wp_current_filter ) ) { 
  147. /** 
  148. * Action triggered after a custom post type model was saved to 
  149. * database. 
  150. * @since 1.0.0 
  151. */ 
  152. do_action( 'ms_saved_' . $class, $this ); 
  153.  
  154. /** 
  155. * Prepare the post data right before it is saved to the wp_posts table. 
  156. * @see self::save() 
  157. * @since 1.0.1.0 
  158. * @param array $post Data collection passed to wp_update_post(). 
  159. * @return array Data collection passed to wp_update_post(). 
  160. */ 
  161. protected function save_post_data( $post ) { 
  162. return $post; 
  163.  
  164. /** 
  165. * Prepare the meta data right before it is saved to the wp_postmeta table. 
  166. * @see self::save() 
  167. * @since 1.0.1.0 
  168. * @param array $data Key-Value pairs that represent metadata. 
  169. * @return array Key-Value pairs that represent metadata. 
  170. */ 
  171. protected function save_meta_data( $data ) { 
  172. return $data; 
  173.  
  174. /** 
  175. * Populate the model with custom data from the wp_posts table. 
  176. * @see MS_Factory::load_from_wp_custom_post_type() 
  177. * @since 1.0.1.0 
  178. * @param array $post Data collection passed to wp_update_post(). 
  179. */ 
  180. public function load_post_data( $post ) { 
  181.  
  182. /** 
  183. * Populate the model with custom data from the wp_postmeta table. 
  184. * @see MS_Factory::load_from_wp_custom_post_type() 
  185. * @since 1.0.1.0 
  186. * @param array $data Key-Value pairs that represent metadata. 
  187. */ 
  188. public function load_meta_data( $data ) { 
  189.  
  190. /** 
  191. * Delete post from wp table 
  192. * @since 1.0.0 
  193. * @return bool 
  194. */ 
  195. public function delete() { 
  196. MS_Factory::select_blog(); 
  197. do_action( 'MS_Model_CustomPostType_delete_before', $this ); 
  198. $res = false; 
  199.  
  200. if ( ! empty( $this->id ) ) { 
  201. $res = ( false !== wp_delete_post( $this->id, true ) ); 
  202.  
  203. do_action( 'MS_Model_CustomPostType_delete_after', $this, $res ); 
  204. MS_Factory::revert_blog(); 
  205. return $res; 
  206.  
  207. /** 
  208. * Removes all meta fields, except the ones that are specified in the 
  209. * second parameter. 
  210. * @since 1.0.0 
  211. * @param array $data_to_keep List of meta-fields to keep (field-names) 
  212. */ 
  213. private function clean_metadata( $data_to_keep ) { 
  214. global $wpdb; 
  215.  
  216. $sql = "SELECT meta_key FROM {$wpdb->postmeta} WHERE post_id = %s;"; 
  217. $sql = $wpdb->prepare( $sql, $this->id ); 
  218. $all_fields = $wpdb->get_col( $sql ); 
  219.  
  220. $remove = array_diff( $all_fields, $data_to_keep ); 
  221.  
  222. $remove = apply_filters( 
  223. 'ms_model_clean_metadata',  
  224. $remove,  
  225. $all_fields,  
  226. $this->id,  
  227. $data_to_keep 
  228. ); 
  229.  
  230. foreach ( $remove as $key ) { 
  231. delete_post_meta( $this->id, $key ); 
  232.  
  233. /** 
  234. * Get custom register post type args for this model. 
  235. * @since 1.0.0 
  236. */ 
  237. public static function get_register_post_type_args() { 
  238. return apply_filters( 
  239. 'ms_customposttype_register_args',  
  240. array() 
  241. ); 
  242.  
  243. /** 
  244. * Check to see if the post is currently being edited. 
  245. * @see wp_check_post_lock. 
  246. * @since 1.0.0 
  247. * @return boolean True if locked. 
  248. */ 
  249. public function check_object_lock() { 
  250. MS_Factory::select_blog(); 
  251. $locked = false; 
  252.  
  253. if ( $this->is_valid() 
  254. && $lock = get_post_meta( $this->id, '_ms_edit_lock', true ) 
  255. ) { 
  256. $time = $lock; 
  257. $time_window = apply_filters( 
  258. 'MS_Model_CustomPostType_check_object_lock_window',  
  259. 150 
  260. ); 
  261. if ( $time && $time > time() - $time_window ) { 
  262. $locked = true; 
  263.  
  264. MS_Factory::revert_blog(); 
  265. return apply_filters( 
  266. 'MS_Model_CustomPostType_check_object_lock',  
  267. $locked,  
  268. $this 
  269. ); 
  270.  
  271. /** 
  272. * Mark the object as currently being edited. 
  273. * Based in the wp_set_post_lock 
  274. * @since 1.0.0 
  275. * @return bool|int 
  276. */ 
  277. public function set_object_lock() { 
  278. MS_Factory::select_blog(); 
  279. $lock = false; 
  280.  
  281. if ( $this->is_valid() ) { 
  282. $lock = apply_filters( 
  283. 'MS_Model_CustomPostType_set_object_lock',  
  284. time() 
  285. ); 
  286. update_post_meta( $this->id, '_ms_edit_lock', $lock ); 
  287.  
  288. MS_Factory::revert_blog(); 
  289. return apply_filters( 
  290. 'MS_Model_CustomPostType_set_object_lock',  
  291. $lock,  
  292. $this 
  293. ); 
  294.  
  295. /** 
  296. * Delete object lock. 
  297. * @since 1.0.0 
  298. */ 
  299. public function delete_object_lock() { 
  300. MS_Factory::select_blog(); 
  301. if ( $this->is_valid() ) { 
  302. update_post_meta( $this->id, '_ms_edit_lock', '' ); 
  303.  
  304. do_action( 'MS_Model_CustomPostType_delete_object_lock', $this ); 
  305. MS_Factory::revert_blog(); 
  306.  
  307. /** 
  308. * Check if the current post type exists. 
  309. * @since 1.0.0 
  310. * @return boolean True if valid. 
  311. */ 
  312. public function is_valid() { 
  313. $valid = false; 
  314.  
  315. if ( $this->id > 0 ) { 
  316. $valid = true; 
  317.  
  318. return apply_filters( 
  319. 'MS_Model_CustomPostType_is_valid',  
  320. $valid,  
  321. $this 
  322. ); 
  323.  
  324. /** 
  325. * Either creates or updates the value of a custom data field. 
  326. * Note: Remember to prefix the $key with a unique string to prevent 
  327. * conflicts with other plugins that also use this function. 
  328. * @since 1.0.0 
  329. * @api 
  330. * @param string $key The field-key. 
  331. * @param mixed $value The new value to assign to the field. 
  332. */ 
  333. public function set_custom_data( $key, $value ) { 
  334. $this->custom_data[ $key ] = $value; 
  335.  
  336. /** 
  337. * Removes a custom data field from this object. 
  338. * @since 1.0.0 
  339. * @api 
  340. * @param string $key The field-key. 
  341. */ 
  342. public function delete_custom_data( $key ) { 
  343. unset( $this->custom_data[ $key ] ); 
  344.  
  345. /** 
  346. * Returns the value of a custom data field. 
  347. * @since 1.0.0 
  348. * @api 
  349. * @param string $key The field-key. 
  350. * @return mixed The value that was previously assigned to the custom field 
  351. * or false if no value was set for the field. 
  352. */ 
  353. public function get_custom_data( $key ) { 
  354. $res = false; 
  355. if ( isset( $this->custom_data[ $key ] ) ) { 
  356. $res = $this->custom_data[ $key ]; 
  357. return $res; 
  358.  
  359. /** 
  360. * Returns the post-type of the current object. 
  361. * @since 1.0.0 
  362. * @return string The post-type name. 
  363. */ 
  364. protected static function _post_type( $orig_posttype ) { 
  365. // Post-type is always lower case. 
  366. $posttype = strtolower( substr( $orig_posttype, 0, 20 ) ); 
  367.  
  368. // Network-wide mode uses different post-types then single-site mode. 
  369. if ( MS_Plugin::is_network_wide() ) { 
  370. $posttype = substr( $posttype, 0, 18 ); 
  371. $posttype .= '-n'; 
  372.  
  373. return $posttype;