PodsField_WYSIWYG

The Pods - Custom Content Types and Fields PodsField WYSIWYG class.

Defined (1)

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

/classes/fields/wysiwyg.php  
  1. class PodsField_WYSIWYG extends PodsField { 
  2.  
  3. /** 
  4. * Field Type Group 
  5. * @var string 
  6. * @since 2.0 
  7. */ 
  8. public static $group = 'Paragraph'; 
  9.  
  10. /** 
  11. * Field Type Identifier 
  12. * @var string 
  13. * @since 2.0 
  14. */ 
  15. public static $type = 'wysiwyg'; 
  16.  
  17. /** 
  18. * Field Type Label 
  19. * @var string 
  20. * @since 2.0 
  21. */ 
  22. public static $label = 'WYSIWYG (Visual Editor)'; 
  23.  
  24. /** 
  25. * Field Type Preparation 
  26. * @var string 
  27. * @since 2.0 
  28. */ 
  29. public static $prepare = '%s'; 
  30.  
  31. /** 
  32. * Do things like register/enqueue scripts and stylesheets 
  33. * @since 2.0 
  34. */ 
  35. public function __construct () { 
  36.  
  37.  
  38. /** 
  39. * Add options and set defaults to 
  40. * @return array 
  41. * @since 2.0 
  42. */ 
  43. public function options () { 
  44. $options = array( 
  45. self::$type . '_repeatable' => array( 
  46. 'label' => __( 'Repeatable Field', 'pods' ),  
  47. 'default' => 0,  
  48. 'type' => 'boolean',  
  49. 'help' => __( 'Making a field repeatable will add controls next to the field which allows users to Add/Remove/Reorder additional values. These values are saved in the database as an array, so searching and filtering by them may require further adjustments".', 'pods' ),  
  50. 'boolean_yes_label' => '',  
  51. 'dependency' => true,  
  52. 'developer_mode' => true 
  53. ),  
  54. self::$type . '_editor' => array( 
  55. 'label' => __( 'Editor', 'pods' ),  
  56. 'default' => 'tinymce',  
  57. 'type' => 'pick',  
  58. 'data' => 
  59. apply_filters( 
  60. 'pods_form_ui_field_wysiwyg_editors',  
  61. array( 
  62. 'tinymce' => __( 'TinyMCE (WP Default)', 'pods' ),  
  63. 'cleditor' => __( 'CLEditor', 'pods' ) 
  64. ),  
  65. 'editor_options' => array( 
  66. 'label' => __( 'Editor Options', 'pods' ),  
  67. 'depends-on' => array( self::$type . '_editor' => 'tinymce' ),  
  68. 'group' => array( 
  69. self::$type . '_media_buttons' => array( 
  70. 'label' => __( 'Enable Media Buttons?', 'pods' ),  
  71. 'default' => 1,  
  72. 'type' => 'boolean' 
  73. ),  
  74. 'output_options' => array( 
  75. 'label' => __( 'Output Options', 'pods' ),  
  76. 'group' => array( 
  77. self::$type . '_oembed' => array( 
  78. 'label' => __( 'Enable oEmbed?', 'pods' ),  
  79. 'default' => 0,  
  80. 'type' => 'boolean',  
  81. 'help' => array( 
  82. __( 'Embed videos, images, tweets, and other content.', 'pods' ),  
  83. 'http://codex.wordpress.org/Embeds' 
  84. ),  
  85. self::$type . '_wptexturize' => array( 
  86. 'label' => __( 'Enable wptexturize?', 'pods' ),  
  87. 'default' => 1,  
  88. 'type' => 'boolean',  
  89. 'help' => array( 
  90. __( 'Transforms less-beautfiul text characters into stylized equivalents.', 'pods' ),  
  91. 'http://codex.wordpress.org/Function_Reference/wptexturize' 
  92. ),  
  93. self::$type . '_convert_chars' => array( 
  94. 'label' => __( 'Enable convert_chars?', 'pods' ),  
  95. 'default' => 1,  
  96. 'type' => 'boolean',  
  97. 'help' => array( 
  98. __( 'Converts text into valid XHTML and Unicode', 'pods' ),  
  99. 'http://codex.wordpress.org/Function_Reference/convert_chars' 
  100. ),  
  101. self::$type . '_wpautop' => array( 
  102. 'label' => __( 'Enable wpautop?', 'pods' ),  
  103. 'default' => 1,  
  104. 'type' => 'boolean',  
  105. 'help' => array( 
  106. __( 'Changes double line-breaks in the text into HTML paragraphs', 'pods' ),  
  107. 'http://codex.wordpress.org/Function_Reference/wpautop' 
  108. ),  
  109. self::$type . '_allow_shortcode' => array( 
  110. 'label' => __( 'Allow Shortcodes?', 'pods' ),  
  111. 'default' => 0,  
  112. 'type' => 'boolean',  
  113. 'dependency' => true,  
  114. 'help' => array( 
  115. __( 'Embed [shortcodes] that help transform your static content into dynamic content.', 'pods' ),  
  116. 'http://codex.wordpress.org/Shortcode_API' 
  117. ),  
  118. self::$type . '_allowed_html_tags' => array( 
  119. 'label' => __( 'Allowed HTML Tags', 'pods' ),  
  120. 'default' => '',  
  121. 'type' => 'text',  
  122. 'help' => __( 'Format: strong em a ul ol li b i', 'pods' ) 
  123. ), /** 
  124. self::$type . '_max_length' => array( 
  125. 'label' => __( 'Maximum Length', 'pods' ),  
  126. 'default' => 0,  
  127. 'type' => 'number',  
  128. 'help' => __( 'Set to -1 for no limit', 'pods' ) 
  129. ),  
  130. self::$type . '_size' => array( 
  131. 'label' => __( 'Field Size', 'pods' ),  
  132. 'default' => 'medium',  
  133. 'type' => 'pick',  
  134. 'data' => array( 
  135. 'small' => __( 'Small', 'pods' ),  
  136. 'medium' => __( 'Medium', 'pods' ),  
  137. 'large' => __( 'Large', 'pods' ) 
  138. )*/ 
  139. ); 
  140.  
  141. if ( function_exists( 'Markdown' ) ) { 
  142. $options[ 'output_options' ][ 'group' ][ self::$type . '_allow_markdown' ] = array( 
  143. 'label' => __( 'Allow Markdown Syntax?', 'pods' ),  
  144. 'default' => 0,  
  145. 'type' => 'boolean' 
  146. ); 
  147.  
  148. return $options; 
  149.  
  150. /** 
  151. * Define the current field's schema for DB table storage 
  152. * @param array $options 
  153. * @return string 
  154. * @since 2.0 
  155. */ 
  156. public function schema ( $options = null ) { 
  157. $schema = 'LONGTEXT'; 
  158.  
  159. return $schema; 
  160.  
  161. /** 
  162. * Change the way the value of the field is displayed with Pods::get 
  163. * @param mixed $value 
  164. * @param string $name 
  165. * @param array $options 
  166. * @param array $fields 
  167. * @param array $pod 
  168. * @param int $id 
  169. * @since 2.0 
  170. */ 
  171. public function display ( $value = null, $name = null, $options = null, $pod = null, $id = null ) { 
  172. $value = $this->strip_html( $value, $options ); 
  173.  
  174. if ( 1 == pods_var( self::$type . '_oembed', $options, 0 ) ) { 
  175. $post_temp = false; 
  176.  
  177. // Workaround for WP_Embed since it needs a $post to work from 
  178. if ( 'post_type' == pods_var( 'type', $pod ) && 0 < $id && ( !isset( $GLOBALS[ 'post' ] ) || empty( $GLOBALS[ 'post' ] ) ) ) { 
  179. $post_temp = true; 
  180.  
  181. $GLOBALS[ 'post' ] = get_post( $id ); 
  182.  
  183. /** 
  184. * @var $embed WP_Embed 
  185. */ 
  186. $embed = $GLOBALS[ 'wp_embed' ]; 
  187. $value = $embed->run_shortcode( $value ); 
  188. $value = $embed->autoembed( $value ); 
  189.  
  190. // Cleanup after ourselves 
  191. if ( $post_temp ) 
  192. $GLOBALS[ 'post' ] = null; 
  193.  
  194. if ( 1 == pods_var( self::$type . '_wptexturize', $options, 1 ) ) 
  195. $value = wptexturize( $value ); 
  196.  
  197. if ( 1 == pods_var( self::$type . '_convert_chars', $options, 1 ) ) 
  198. $value = convert_chars( $value ); 
  199.  
  200. if ( 1 == pods_var( self::$type . '_wpautop', $options, 1 ) ) 
  201. $value = wpautop( $value ); 
  202.  
  203. if ( 1 == pods_var( self::$type . '_allow_shortcode', $options, 0 ) ) { 
  204. if ( 1 == pods_var( self::$type . '_wpautop', $options, 1 ) ) 
  205. $value = shortcode_unautop( $value ); 
  206.  
  207. $value = do_shortcode( $value ); 
  208.  
  209. if ( function_exists( 'Markdown' ) && 1 == pods_v( self::$type . '_allow_markdown', $options ) ) { 
  210. $value = Markdown( $value ); 
  211.  
  212. return $value; 
  213.  
  214. /** 
  215. * Customize output of the form field 
  216. * @param string $name 
  217. * @param mixed $value 
  218. * @param array $options 
  219. * @param array $pod 
  220. * @param int $id 
  221. * @since 2.0 
  222. */ 
  223. public function input ( $name, $value = null, $options = null, $pod = null, $id = null ) { 
  224. $options = (array) $options; 
  225. $form_field_type = PodsForm::$field_type; 
  226.  
  227. if ( is_array( $value ) ) 
  228. $value = implode( "\n", $value ); 
  229.  
  230. if ( isset( $options[ 'name' ] ) && false === PodsForm::permission( self::$type, $options[ 'name' ], $options, null, $pod, $id ) ) { 
  231. if ( pods_var( 'read_only', $options, false ) ) { 
  232. $options[ 'readonly' ] = true; 
  233.  
  234. $field_type = 'textarea'; 
  235. else 
  236. return; 
  237. elseif ( !pods_has_permissions( $options ) && pods_var( 'read_only', $options, false ) ) { 
  238. $options[ 'readonly' ] = true; 
  239.  
  240. $field_type = 'textarea'; 
  241. elseif ( 'tinymce' == pods_var( self::$type . '_editor', $options ) ) 
  242. $field_type = 'tinymce'; 
  243. elseif ( 'cleditor' == pods_var( self::$type . '_editor', $options ) ) 
  244. $field_type = 'cleditor'; 
  245. else { 
  246. // Support custom WYSIWYG integration 
  247. do_action( 'pods_form_ui_field_wysiwyg_' . pods_var( self::$type . '_editor', $options ), $name, $value, $options, $pod, $id ); 
  248. do_action( 'pods_form_ui_field_wysiwyg', pods_var( self::$type . '_editor', $options ), $name, $value, $options, $pod, $id ); 
  249.  
  250. return; 
  251.  
  252. pods_view( PODS_DIR . 'ui/fields/' . $field_type . '.php', compact( array_keys( get_defined_vars() ) ) ); 
  253.  
  254. /** 
  255. * Change the value or perform actions after validation but before saving to the DB 
  256. * @param mixed $value 
  257. * @param int $id 
  258. * @param string $name 
  259. * @param array $options 
  260. * @param array $fields 
  261. * @param array $pod 
  262. * @param object $params 
  263. * @since 2.0 
  264. */ 
  265. public function pre_save ( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) { 
  266. $value = $this->strip_html( $value, $options ); 
  267.  
  268. $length = (int) pods_var( self::$type . '_max_length', $options, 0 ); 
  269.  
  270. if ( 0 < $length && $length < pods_mb_strlen( $value ) ) { 
  271. $value = pods_mb_substr( $value, 0, $length ); 
  272.  
  273. return $value; 
  274.  
  275. /** 
  276. * Customize the Pods UI manage table column output 
  277. * @param int $id 
  278. * @param mixed $value 
  279. * @param string $name 
  280. * @param array $options 
  281. * @param array $fields 
  282. * @param array $pod 
  283. * @since 2.0 
  284. */ 
  285. public function ui ( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) { 
  286. $value = $this->strip_html( $value, $options ); 
  287.  
  288. $value = wp_trim_words( $value ); 
  289.  
  290. return $value; 
  291.  
  292. /** 
  293. * Strip HTML based on options 
  294. * @param string $value 
  295. * @param array $options 
  296. * @return string 
  297. */ 
  298. public function strip_html ( $value, $options = null ) { 
  299. if ( is_array( $value ) ) 
  300. $value = @implode( ' ', $value ); 
  301.  
  302. $value = trim( $value ); 
  303.  
  304. if ( empty( $value ) ) 
  305. return $value; 
  306.  
  307. $options = (array) $options; 
  308.  
  309. $allowed_html_tags = ''; 
  310.  
  311. if ( 0 < strlen( pods_var( self::$type . '_allowed_html_tags', $options ) ) ) { 
  312. $allowed_tags = pods_var( self::$type . '_allowed_html_tags', $options ); 
  313. $allowed_tags = trim( str_replace( array( '<', '>', ', ' ), ' ', $allowed_tags ) ); 
  314. $allowed_tags = explode( ' ', $allowed_tags ); 
  315. $allowed_tags = array_unique( array_filter( $allowed_tags ) ); 
  316.  
  317. if ( !empty( $allowed_tags ) ) { 
  318. $allowed_html_tags = '<' . implode( '><', $allowed_tags ) . '>'; 
  319.  
  320. if ( !empty( $allowed_html_tags ) ) 
  321. $value = strip_tags( $value, $allowed_html_tags ); 
  322.  
  323. return $value;