PageRenderTime 33ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/php/fields/post.php

https://github.com/quadcodes/wp-front-end-editor
PHP | 410 lines | 280 code | 118 blank | 12 comment | 47 complexity | 5f75f5ceaa6f6955c2d759b900bb0d0f MD5 | raw file
  1. <?php
  2. abstract class FEE_Field_Post extends FEE_Field_Base {
  3. protected $field;
  4. static function get_object_type() {
  5. return 'post';
  6. }
  7. protected function setup() {
  8. $this->field = str_replace( 'the_', 'post_', $this->filter );
  9. if ( FEE_Core::$options->group_post ) {
  10. add_action( 'post_class', array( __CLASS__, 'post_class' ) );
  11. }
  12. }
  13. static function post_class( $classes ) {
  14. $classes[] = 'fee-group';
  15. return $classes;
  16. }
  17. function wrap( $content, $post_id = 0 ) {
  18. if ( !$post_id = $this->_get_id( $post_id ) ) {
  19. return $content;
  20. }
  21. $content = $this->placehold( $content );
  22. return parent::wrap( $content, compact( 'post_id' ) );
  23. }
  24. protected function _get_id( $post_id = 0, $in_loop = true ) {
  25. global $post;
  26. if ( $in_loop ) {
  27. if ( !in_the_loop() ) {
  28. return false;
  29. }
  30. if ( $post_id && $post->ID != $post_id ) {
  31. return false;
  32. }
  33. $post_id = $post->ID;
  34. }
  35. if ( !$post_id || !$this->check( $post_id ) ) {
  36. return false;
  37. }
  38. return $post_id;
  39. }
  40. function get( $data ) {
  41. extract( $data );
  42. # $this->handle_locking( $post_id );
  43. return get_post_field( $this->field, $post_id );
  44. }
  45. function save( $data, $content ) {
  46. extract( $data );
  47. # $this->handle_locking( $post_id );
  48. $postdata = array(
  49. 'ID' => $post_id,
  50. $this->field => $content
  51. );
  52. // reset slug
  53. if ( $this->field == 'post_title' ) {
  54. $current_slug = get_post_field( 'post_name', $post_id );
  55. $current_title = get_post_field( 'post_title', $post_id );
  56. // update only if not explicitly set
  57. if ( empty( $current_slug ) || $current_slug == sanitize_title_with_dashes( $current_title ) ) {
  58. $new_slug = sanitize_title_with_dashes( $content );
  59. $postdata['post_name'] = $new_slug;
  60. }
  61. }
  62. wp_update_post( (object) $postdata );
  63. }
  64. function check( $post_id = 0 ) {
  65. if ( is_array( $post_id ) ) {
  66. extract( $post_id );
  67. }
  68. return current_user_can( 'edit_post', $post_id );
  69. }
  70. protected function set_post_global( $post_id ) {
  71. global $post;
  72. $post = get_post( $post_id );
  73. setup_postdata( $post );
  74. }
  75. protected function handle_locking( $post_id ) {
  76. $last_user = wp_check_post_lock( $post_id );
  77. if ( $last_user ) {
  78. $message = __( 'Error: %s is currently editing this.', 'front-end-editor' );
  79. $message = sprintf( $message, esc_html( get_userdata( $last_user )->display_name ) );
  80. throw new Exception( $message );
  81. }
  82. wp_set_post_lock( $post_id );
  83. }
  84. }
  85. // Handles the_title field
  86. class FEE_Field_Post_Title extends FEE_Field_Post {
  87. function get( $data ) {
  88. extract( $data );
  89. if ( 'auto-draft' == get_post_field( 'post_status', $post_id ) )
  90. return '';
  91. return get_post_field( $this->field, $post_id, 'edit' );
  92. }
  93. function get_filtered( $data ) {
  94. $this->set_post_global( $data['post_id'] );
  95. return $this->placehold( get_the_title() );
  96. }
  97. }
  98. // Handles the_content field
  99. class FEE_Field_Post_Content extends FEE_Field_Post {
  100. function get_filtered( $data ) {
  101. $this->set_post_global( $data['post_id'] );
  102. ob_start();
  103. the_content();
  104. return $this->placehold( ob_get_clean() );
  105. }
  106. }
  107. // Handles the_excerpt field
  108. class FEE_Field_Post_Excerpt extends FEE_Field_Post {
  109. function get( $data ) {
  110. $post = get_post( $data['post_id'] );
  111. $this->set_post_global( $data['post_id'] );
  112. return wp_trim_excerpt( $post->post_excerpt );
  113. }
  114. function save( $data, $excerpt ) {
  115. if ( $excerpt == $this->get( $data ) ) {
  116. return;
  117. }
  118. $postdata = array(
  119. 'ID' => $data['post_id'],
  120. 'post_excerpt' => $excerpt
  121. );
  122. wp_update_post( (object) $postdata );
  123. }
  124. function get_filtered( $data ) {
  125. $this->set_post_global( $data['post_id'] );
  126. ob_start();
  127. the_excerpt();
  128. return ob_get_clean();
  129. }
  130. }
  131. // Handles the_terms field
  132. class FEE_Field_Terms extends FEE_Field_Post {
  133. function wrap( $content, $taxonomy, $before, $sep, $after ) {
  134. global $post;
  135. if ( !in_the_loop() ) {
  136. return $content;
  137. }
  138. $post_id = $post->ID;
  139. if ( !$post_id ) {
  140. return $content;
  141. }
  142. $data = compact( 'post_id', 'taxonomy', 'before', 'sep', 'after' );
  143. if ( !$this->check( $data ) ) {
  144. return $content;
  145. }
  146. $content = $this->placehold( $content );
  147. $data['type'] = is_taxonomy_hierarchical( $taxonomy ) ? FEE_Core::$options->taxonomy_ui : 'terminput';
  148. return FEE_Field_Base::wrap( $content, $data );
  149. }
  150. function get( $data ) {
  151. extract( $data );
  152. if ( 'terminput' == $type ) {
  153. $tags = get_terms_to_edit( $post_id, $taxonomy );
  154. $tags = str_replace( ',', ', ', $tags );
  155. return $tags;
  156. } else {
  157. $terms = get_the_terms( $post_id, $taxonomy );
  158. if ( empty( $terms ) )
  159. $selected = 0;
  160. else
  161. $selected = reset( $terms )->term_id;
  162. return wp_dropdown_categories( array(
  163. 'taxonomy' => $taxonomy,
  164. 'selected' => $selected,
  165. 'hide_empty' => false,
  166. 'hierarchical' => true,
  167. 'show_option_none' => __( '&mdash; None &mdash;', 'front-end-editor' ),
  168. 'echo' => false
  169. ) );
  170. }
  171. }
  172. function save( $data, $terms ) {
  173. extract( $data );
  174. if ( 'termselect' == $type ) {
  175. wp_set_object_terms( $post_id, absint( $terms ), $taxonomy );
  176. } elseif ( !is_taxonomy_hierarchical( $taxonomy ) ) {
  177. wp_set_post_terms( $post_id, $terms, $taxonomy );
  178. } else {
  179. $term_ids = array();
  180. foreach ( explode( ',', $terms ) as $term_name ) {
  181. $term = get_term_by( 'name', trim( $term_name ), $taxonomy );
  182. if ( !$term ) {
  183. $r = wp_insert_term( $term_name, $taxonomy );
  184. if ( is_wp_error( $r ) )
  185. continue;
  186. $term_ids[] = (int) $r['term_id'];
  187. } else {
  188. $term_ids[] = (int) $term->term_id;
  189. }
  190. }
  191. wp_set_object_terms( $post_id, $term_ids, $taxonomy );
  192. }
  193. }
  194. function get_filtered( $data ) {
  195. extract( $data );
  196. $content = get_the_term_list( $post_id, $taxonomy, $before, $sep, $after );
  197. return $this->placehold( $content );
  198. }
  199. function check( $data = 0 ) {
  200. extract( $data );
  201. return current_user_can( 'edit_post', $post_id ) && current_user_can( get_taxonomy( $taxonomy )->cap->assign_terms );
  202. }
  203. }
  204. // Handles the_tags field
  205. class FEE_Field_Tags extends FEE_Field_Terms {
  206. function wrap( $content, $before, $sep, $after ) {
  207. return parent::wrap( $content, 'post_tag', $before, $sep, $after );
  208. }
  209. }
  210. // Handles the_category field
  211. class FEE_Field_Category extends FEE_Field_Terms {
  212. function wrap( $content, $sep, $parents ) {
  213. return parent::wrap( $content, 'category', '', $sep, '' );
  214. }
  215. }
  216. // Handles the post thumbnail
  217. class FEE_Field_Post_Thumbnail extends FEE_Field_Post {
  218. function wrap( $html, $post_id, $post_thumbnail_id, $size ) {
  219. if ( !$post_id = $this->_get_id( $post_id, false ) ) {
  220. return $html;
  221. }
  222. return FEE_Field_Base::wrap( $html, compact( 'post_id', 'size' ) );
  223. }
  224. function get( $data ) {
  225. extract( $data );
  226. return get_post_thumbnail_id( $post_id );
  227. }
  228. function save( $data, $thumbnail_id ) {
  229. extract( $data );
  230. if ( -1 == $thumbnail_id ) {
  231. delete_post_meta( $post_id, '_thumbnail_id' );
  232. } else {
  233. update_post_meta( $post_id, '_thumbnail_id', $thumbnail_id );
  234. }
  235. }
  236. function get_filtered( $data ) {
  237. extract( $data );
  238. $thumbnail_id = get_post_thumbnail_id( $post_id );
  239. if ( !$thumbnail_id )
  240. return -1;
  241. list( $url ) = image_downsize( $thumbnail_id, $size );
  242. return $url;
  243. }
  244. }
  245. // Handles post_meta field
  246. class FEE_Field_Post_Meta extends FEE_Field_Post {
  247. function setup() {
  248. add_filter( 'post_meta', array( __CLASS__, 'prewrap' ), 9, 4 );
  249. }
  250. function prewrap( $data, $post_id, $key, $ui ) {
  251. if ( 'rich' == $ui )
  252. $data = wpautop( $data );
  253. return $data;
  254. }
  255. function wrap( $data, $post_id, $key, $ui, $single ) {
  256. if ( $this->check( $post_id ) ) {
  257. if ( $single ) {
  258. $data = array( $this->placehold( $data ) );
  259. }
  260. $r = array();
  261. foreach ( $data as $i => $val ) {
  262. $r[$i] = FEE_Field_Base::wrap( $val, compact( 'post_id', 'key', 'ui', 'i' ) );
  263. }
  264. }
  265. else {
  266. $r = (array) $data;
  267. }
  268. if ( $single )
  269. return $r[0];
  270. return $r;
  271. }
  272. function get( $data ) {
  273. extract( $data );
  274. $data = get_post_meta( $post_id, $key );
  275. return @$data[$i];
  276. }
  277. function save( $data, $new_value ) {
  278. extract( $data );
  279. $data = get_post_meta( $post_id, $key );
  280. $old_value = @$data[$i];
  281. if ( !$new_value ) {
  282. delete_post_meta( $post_id, $key, $old_value );
  283. }
  284. else {
  285. update_post_meta( $post_id, $key, $new_value, $old_value );
  286. }
  287. }
  288. function get_filtered( $data ) {
  289. extract( $data );
  290. return $this->placehold( editable_post_meta( $post_id, $key, $type, false ) );
  291. }
  292. }