/php/fields/other.php

https://github.com/quadcodes/wp-front-end-editor · PHP · 329 lines · 216 code · 105 blank · 8 comment · 15 complexity · 103cde300f705e4023228e272ff5a820 MD5 · raw file

  1. <?php
  2. // Handles comment_text field
  3. class FEE_Field_Comment extends FEE_Field_Base {
  4. static function get_object_type() {
  5. return 'comment';
  6. }
  7. function wrap( $content ) {
  8. global $comment;
  9. $data = array( 'comment_id' => $comment->comment_ID );
  10. if ( !$this->check( $data ) )
  11. return $content;
  12. return parent::wrap( wpautop( $content ), $data );
  13. }
  14. function get( $data ) {
  15. extract( $data );
  16. return get_comment( $comment_id )->comment_content;
  17. }
  18. function save( $data, $content ) {
  19. extract( $data );
  20. wp_update_comment( array(
  21. 'comment_ID' => $comment_id,
  22. 'comment_content' => $content
  23. ) );
  24. }
  25. function get_filtered( $data ) {
  26. extract( $data );
  27. return wpautop( get_comment( $comment_id )->comment_content );
  28. }
  29. function check( $data = 0 ) {
  30. extract( $data );
  31. if ( current_user_can( 'moderate_comments' ) )
  32. return true;
  33. global $user_ID;
  34. $comment = get_comment( $comment_id );
  35. return $user_ID == $comment->user_id;
  36. }
  37. }
  38. // Handles term_{$field} fields
  39. class FEE_Field_Term_Field extends FEE_Field_Base {
  40. protected $field;
  41. static function get_object_type() {
  42. return 'term';
  43. }
  44. function setup() {
  45. $this->field = str_replace( 'term_', '', $this->filter );
  46. }
  47. function wrap( $content, $term_id, $taxonomy ) {
  48. $data = compact( 'term_id', 'taxonomy' );
  49. if ( !$this->check( $data ) )
  50. return $content;
  51. return parent::wrap( $this->placehold( $content ), $data );
  52. }
  53. function get( $data ) {
  54. extract( $data );
  55. return get_term_field( $this->field, $term_id, $taxonomy, 'raw' );
  56. }
  57. function save( $data, $content ) {
  58. extract( $data );
  59. wp_update_term( $term_id, $taxonomy, array( $this->field => $content ) );
  60. return $content;
  61. }
  62. function get_filtered( $data ) {
  63. extract( $data );
  64. return get_term_field( $this->field, $term_id, $taxonomy );
  65. }
  66. function check( $data = 0 ) {
  67. extract( $data );
  68. return current_user_can( get_taxonomy( $taxonomy )->cap->edit_terms );
  69. }
  70. }
  71. // Handles single_*_title fields
  72. class FEE_Field_Single_Title extends FEE_Field_Term_Field {
  73. function setup() {
  74. $this->field = 'name';
  75. remove_filter( $this->filter, 'strip_tags' );
  76. }
  77. function wrap( $title ) {
  78. $term = get_queried_object();
  79. return parent::wrap( $title, $term->term_id, $term->taxonomy );
  80. }
  81. }
  82. // Handles the_author_description field
  83. class FEE_Field_Author_Desc extends FEE_Field_Base {
  84. static function get_object_type() {
  85. return 'user';
  86. }
  87. function wrap( $content, $author_id = '' ) {
  88. if ( !$author_id )
  89. $author_id = $GLOBALS['authordata']->ID;
  90. if ( !$this->check( compact( 'author_id' ) ) )
  91. return $content;
  92. $content = $this->placehold( $content );
  93. return parent::wrap( $content, compact( 'author_id' ) );
  94. }
  95. // Retrieve the current data for the field
  96. function get( $data ) {
  97. extract( $data );
  98. return get_user_meta( $author_id, 'description', true );
  99. }
  100. function save( $data, $content ) {
  101. extract( $data );
  102. update_user_meta( $author_id, 'description', $content );
  103. return $content;
  104. }
  105. function get_filtered( $data ) {
  106. extract( $data );
  107. return get_the_author_meta( 'description', $author_id );
  108. }
  109. function check( $data = 0 ) {
  110. extract( $data );
  111. return current_user_can( 'edit_user', $author_id );
  112. }
  113. }
  114. // Handles bloginfo fields
  115. class FEE_Field_Bloginfo extends FEE_Field_Base {
  116. static function get_object_type() {
  117. return 'option';
  118. }
  119. function wrap( $content, $show ) {
  120. if ( !$this->check() )
  121. return $content;
  122. if ( empty( $show ) && get_option( 'blogname' ) == $content )
  123. $show = 'name';
  124. if ( !in_array( $show, array( 'name', 'description' ) ) )
  125. return $content;
  126. return parent::wrap( $content, compact( 'show' ) );
  127. }
  128. function get( $data ) {
  129. extract( $data );
  130. return get_option( 'blog' . $show );
  131. }
  132. function save( $data, $content ) {
  133. extract( $data );
  134. update_option( 'blog' . $show, $content );
  135. }
  136. function get_filtered( $data ) {
  137. return get_bloginfo( $data['show'] );
  138. }
  139. function check( $data = 0 ) {
  140. return current_user_can( 'manage_options' );
  141. }
  142. }
  143. // Handles editable_option fields
  144. class FEE_Field_Option extends FEE_Field_Base {
  145. static function get_object_type() {
  146. return 'option';
  147. }
  148. static function init( $file ) {
  149. register_uninstall_hook( $file, array( __CLASS__, 'uninstall' ) );
  150. }
  151. static function uninstall() {
  152. global $wpdb;
  153. $wpdb->query( "DELETE FROM $wpdb->options WHERE option_name LIKE 'editable!_option!_%' ESCAPE '!'" );
  154. }
  155. function wrap( $content, $key, $ui ) {
  156. $data = compact( 'key', 'ui' );
  157. if ( !$this->check( $data ) )
  158. return $content;
  159. $content = $this->placehold( $content );
  160. return parent::wrap( $content, $data );
  161. }
  162. function get( $data ) {
  163. extract( $data );
  164. return get_option( $key );
  165. }
  166. function save( $data, $content ) {
  167. extract( $data );
  168. update_option( $key, $content );
  169. }
  170. function get_filtered( $data ) {
  171. extract( $data );
  172. return $this->placehold( get_option( $key ) );
  173. }
  174. function check( $data = 0 ) {
  175. extract( $data );
  176. $cap = ( 0 === strpos( $key, 'editable_option_' ) ) ? 'edit_theme_options' : 'manage_options';
  177. return current_user_can( $cap );
  178. }
  179. }
  180. // Handles editable_image fields
  181. class FEE_Field_Image extends FEE_Field_Base {
  182. static function get_object_type() {
  183. return 'option';
  184. }
  185. static function init( $file ) {
  186. add_action( 'wp_ajax_fee_image_insert', array( __CLASS__, 'image_insert' ) );
  187. register_uninstall_hook( $file, array( __CLASS__, 'uninstall' ) );
  188. }
  189. static function image_insert() {
  190. add_filter( 'media_send_to_editor', array( __CLASS__, '_capture_html' ), 99 );
  191. media_upload_form_handler();
  192. }
  193. static function _capture_html( $html ) {
  194. die( $html );
  195. }
  196. static function uninstall() {
  197. global $wpdb;
  198. $wpdb->query( "DELETE FROM $wpdb->options WHERE option_name LIKE 'editable!_image!_%' ESCAPE '!'" );
  199. }
  200. function wrap( $img, $key ) {
  201. if ( !$this->check() )
  202. return $img;
  203. return parent::wrap( $img, compact( 'key' ) );
  204. }
  205. function get( $data ) {
  206. return get_option( self::get_key( $data ) );
  207. }
  208. function save( $data, $url ) {
  209. if ( $url == -1 )
  210. delete_option( self::get_key( $data ) );
  211. else
  212. update_option( self::get_key( $data ), $url );
  213. }
  214. function get_filtered( $data ) {
  215. return $this->get( $data );
  216. }
  217. private static function get_key( $data ) {
  218. extract( $data );
  219. return 'editable_image_' . trim( strip_tags( $key ) );
  220. }
  221. function check( $data = 0 ) {
  222. return current_user_can( 'edit_theme_options' );
  223. }
  224. }