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; 
  19.  
  20. /** 
  21. * Model name. 
  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; 
  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 2.0 
  65. * @var array 
  66. */ 
  67. protected $custom_data = array(); 
  68.  
  69. /** 
  70. * Not persisted fields. 
  71. * @since 1.0.0 
  72. * @var string[] 
  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 2.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. * @var string[] 
  92. */ 
  93. public function save() { 
  94. MS_Factory::select_blog(); 
  95. $this->before_save(); 
  96.  
  97. $this->post_modified = gmdate( 'Y-m-d H:i:s' ); 
  98.  
  99. $class = get_class( $this ); 
  100. $post = array( 
  101. 'comment_status' => 'closed',  
  102. 'ping_status' => 'closed',  
  103. 'post_author' => $this->user_id,  
  104. 'post_content' => $this->description,  
  105. 'post_excerpt' => $this->description,  
  106. 'post_name' => sanitize_text_field( $this->name ),  
  107. 'post_status' => 'private',  
  108. 'post_title' => sanitize_title( ! empty( $this->title ) ? $this->title : $this->name ),  
  109. 'post_type' => $this->get_post_type(),  
  110. 'post_modified' => $this->post_modified,  
  111. ); 
  112.  
  113. if ( empty( $this->id ) ) { 
  114. $this->id = wp_insert_post( $post ); 
  115. } else { 
  116. $post[ 'ID' ] = $this->id; 
  117. wp_update_post( $post ); 
  118.  
  119. // save attributes in postmeta table 
  120. $data = MS_Factory::serialize_model( $this ); 
  121.  
  122. // We first remove any metadata of our custom post type that is not 
  123. // contained in the serialized data collection. 
  124. $this->clean_metadata( array_keys( $data ) ); 
  125.  
  126. // Then we update all meta fields that are inside the collection 
  127. foreach ( $data as $field => $val ) { 
  128. update_post_meta( $this->id, $field, $val ); 
  129.  
  130. wp_cache_set( $this->id, $this, $class ); 
  131. $this->after_save(); 
  132. MS_Factory::revert_blog(); 
  133.  
  134. global $wp_current_filter; 
  135. if ( ! in_array( 'ms_saved_' . $class, $wp_current_filter ) ) { 
  136. /** 
  137. * Action triggered after a custom post type model was saved to 
  138. * database. 
  139. * @since 2.0.0 
  140. */ 
  141. do_action( 'ms_saved_' . $class, $this ); 
  142.  
  143. /** 
  144. * Delete post from wp table 
  145. * @since 1.0.0 
  146. * @return bool 
  147. */ 
  148. public function delete() { 
  149. MS_Factory::select_blog(); 
  150. do_action( 'MS_Model_CustomPostType_delete_before', $this ); 
  151. $res = false; 
  152.  
  153. if ( ! empty( $this->id ) ) { 
  154. $res = ( false !== wp_delete_post( $this->id, true ) ); 
  155.  
  156. do_action( 'MS_Model_CustomPostType_delete_after', $this, $res ); 
  157. MS_Factory::revert_blog(); 
  158. return $res; 
  159.  
  160. /** 
  161. * Removes all meta fields, except the ones that are specified in the 
  162. * second parameter. 
  163. * @since 1.1.0 
  164. * @param array $data_to_keep List of meta-fields to keep (field-names) 
  165. */ 
  166. private function clean_metadata( $data_to_keep ) { 
  167. global $wpdb; 
  168.  
  169. $sql = "SELECT meta_key FROM {$wpdb->postmeta} WHERE post_id = %s;"; 
  170. $sql = $wpdb->prepare( $sql, $this->id ); 
  171. $all_fields = $wpdb->get_col( $sql ); 
  172.  
  173. $remove = array_diff( $all_fields, $data_to_keep ); 
  174.  
  175. $remove = apply_filters( 
  176. 'ms_model_clean_metadata',  
  177. $remove,  
  178. $all_fields,  
  179. $this->id,  
  180. $data_to_keep 
  181. ); 
  182.  
  183. foreach ( $remove as $key ) { 
  184. delete_post_meta( $this->id, $key ); 
  185.  
  186. /** 
  187. * Get custom register post type args for this model. 
  188. * @since 1.0.0 
  189. */ 
  190. public static function get_register_post_type_args() { 
  191. return apply_filters( 
  192. 'ms_customposttype_register_args',  
  193. array() 
  194. ); 
  195.  
  196. /** 
  197. * Check to see if the post is currently being edited. 
  198. * @see wp_check_post_lock. 
  199. * @since 1.0.0 
  200. * @return boolean True if locked. 
  201. */ 
  202. public function check_object_lock() { 
  203. MS_Factory::select_blog(); 
  204. $locked = false; 
  205.  
  206. if ( $this->is_valid() 
  207. && $lock = get_post_meta( $this->id, '_ms_edit_lock', true ) 
  208. ) { 
  209. $time = $lock; 
  210. $time_window = apply_filters( 
  211. 'MS_Model_CustomPostType_check_object_lock_window',  
  212. 150 
  213. ); 
  214. if ( $time && $time > time() - $time_window ) { 
  215. $locked = true; 
  216.  
  217. MS_Factory::revert_blog(); 
  218. return apply_filters( 
  219. 'MS_Model_CustomPostType_check_object_lock',  
  220. $locked,  
  221. $this 
  222. ); 
  223.  
  224. /** 
  225. * Mark the object as currently being edited. 
  226. * Based in the wp_set_post_lock 
  227. * @since 1.0.0 
  228. * @return bool|int 
  229. */ 
  230. public function set_object_lock() { 
  231. MS_Factory::select_blog(); 
  232. $lock = false; 
  233.  
  234. if ( $this->is_valid() ) { 
  235. $lock = apply_filters( 
  236. 'MS_Model_CustomPostType_set_object_lock',  
  237. time() 
  238. ); 
  239. update_post_meta( $this->id, '_ms_edit_lock', $lock ); 
  240.  
  241. MS_Factory::revert_blog(); 
  242. return apply_filters( 
  243. 'MS_Model_CustomPostType_set_object_lock',  
  244. $lock,  
  245. $this 
  246. ); 
  247.  
  248. /** 
  249. * Delete object lock. 
  250. * @since 1.0.0 
  251. */ 
  252. public function delete_object_lock() { 
  253. MS_Factory::select_blog(); 
  254. if ( $this->is_valid() ) { 
  255. update_post_meta( $this->id, '_ms_edit_lock', '' ); 
  256.  
  257. do_action( 'MS_Model_CustomPostType_delete_object_lock', $this ); 
  258. MS_Factory::revert_blog(); 
  259.  
  260. /** 
  261. * Check if the current post type exists. 
  262. * @since 1.0.0 
  263. * @return boolean True if valid. 
  264. */ 
  265. public function is_valid() { 
  266. $valid = false; 
  267.  
  268. if ( $this->id > 0 ) { 
  269. $valid = true; 
  270.  
  271. return apply_filters( 
  272. 'MS_Model_CustomPostType_is_valid',  
  273. $valid,  
  274. $this 
  275. ); 
  276.  
  277. /** 
  278. * Either creates or updates the value of a custom data field. 
  279. * Note: Remember to prefix the $key with a unique string to prevent 
  280. * conflicts with other plugins that also use this function. 
  281. * @since 2.0.0 
  282. * @api 
  283. * @param string $key The field-key. 
  284. * @param mixed $value The new value to assign to the field. 
  285. */ 
  286. public function set_custom_data( $key, $value ) { 
  287. $this->custom_data[ $key ] = $value; 
  288.  
  289. /** 
  290. * Removes a custom data field from this object. 
  291. * @since 2.0.0 
  292. * @api 
  293. * @param string $key The field-key. 
  294. */ 
  295. public function delete_custom_data( $key ) { 
  296. unset( $this->custom_data[ $key ] ); 
  297.  
  298. /** 
  299. * Returns the value of a custom data field. 
  300. * @since 2.0.0 
  301. * @api 
  302. * @param string $key The field-key. 
  303. * @return mixed The value that was previously assigned to the custom field 
  304. * or false if no value was set for the field. 
  305. */ 
  306. public function get_custom_data( $key ) { 
  307. $res = false; 
  308. if ( isset( $this->custom_data[ $key ] ) ) { 
  309. $res = $this->custom_data[ $key ]; 
  310. return $res; 
  311.  
  312. /** 
  313. * Returns the post-type of the current object. 
  314. * @since 2.0.0 
  315. * @return string The post-type name. 
  316. */ 
  317. protected static function _post_type( $orig_posttype ) { 
  318. // Post-type is always lower case. 
  319. $posttype = strtolower( substr( $orig_posttype, 0, 20 ) ); 
  320.  
  321. return $posttype;