Wysiwyg.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. // Don't load directly
  3. if ( ! defined( 'ABSPATH' ) ) {
  4. die( '-1' );
  5. }
  6. if ( class_exists( 'Pngx__Admin__Field__Wysiwyg' ) ) {
  7. return;
  8. }
  9. /**
  10. * Class Pngx__Admin__Field__Wysiwyg
  11. * Visual Editor Field
  12. */
  13. class Pngx__Admin__Field__Wysiwyg {
  14. public static function display( $field = array(), $options = array(), $options_id = null, $meta = null, $repeat_obj = null ) {
  15. if ( ! empty( $options_id ) ) {
  16. $name = $options_id;
  17. $value = $options[ $field['id'] ];
  18. } else {
  19. $name = $field['id'];
  20. $value = $meta;
  21. }
  22. global $post, $wp_version;
  23. $settings = array();
  24. if ( ! class_exists( '_WP_Editors' ) ) {
  25. require( ABSPATH . WPINC . '/class-wp-editor.php' );
  26. }
  27. $set = _WP_Editors::parse_settings( esc_attr( $field['id'] ), $settings );
  28. if ( ! current_user_can( 'upload_files' ) ) {
  29. $set['media_buttons'] = false;
  30. }
  31. if ( $set['media_buttons'] ) {
  32. wp_enqueue_script( 'thickbox' );
  33. wp_enqueue_style( 'thickbox' );
  34. wp_enqueue_script( 'media-upload' );
  35. $post = get_post();
  36. if ( ! $post && ! empty( $GLOBALS['post_ID'] ) ) {
  37. $post = $GLOBALS['post_ID'];
  38. }
  39. wp_enqueue_media( array(
  40. 'post' => $post
  41. ) );
  42. }
  43. wp_enqueue_script( 'tiny_mce' );
  44. _WP_Editors::editor_settings( esc_attr( $field['id'] ), $set );
  45. // Only Localize Script Once Per Page
  46. if ( ! isset( $post->load_scripts ) ) {
  47. /**
  48. * Filter Tiny MCE Buttons for PNGX Editor Script
  49. *
  50. * @param array() an array of attributes to create the button
  51. * @param $post current post object
  52. */
  53. $pngx_visual_editor_buttons = apply_filters( 'pngx_visual_editor_functions', array(), $post );
  54. /**
  55. * Filter HTML Editor Buttons for PNGX Editor Script
  56. *
  57. * @param array() an array of attributes to create the button
  58. * @param $post current post object
  59. */
  60. $pngx_html_editor_buttons = apply_filters( 'pngx_html_editor_functions', array(), $post );
  61. /**
  62. * Variables for WP Editor Script
  63. */
  64. $pngx_editor_vars = array(
  65. 'url' => get_home_url(),
  66. 'includes_url' => includes_url(),
  67. 'visual_editor_buttons' => $pngx_visual_editor_buttons,
  68. 'html_editor_buttons' => $pngx_html_editor_buttons,
  69. 'rich_editing' => get_user_meta( get_current_user_id(), 'rich_editing', true ),
  70. );
  71. wp_localize_script( 'pngx-wp-editor', 'pngx_editor_vars', $pngx_editor_vars );
  72. if ( is_object( $post ) ) {
  73. $post->load_scripts = true;
  74. }
  75. }
  76. $std = isset( $field['std'] ) ? $field['std'] : '';
  77. $rows = isset( $field['rows'] ) ? $field['rows'] : 12;
  78. $cols = isset( $field['cols'] ) ? $field['cols'] : 50;
  79. $class = isset( $field['class'] ) ? $field['class'] : '';
  80. $repeating = isset( $field['repeating'] ) ? '[]' : '';
  81. ?>
  82. <textarea
  83. class="pngx-ajax-wp-editor <?php echo esc_attr( $class ); ?>"
  84. id="<?php echo esc_attr( $field['id'] ); ?>"
  85. name="<?php echo esc_attr( $name ) . $repeating; ?>"
  86. placeholder="<?php echo esc_attr( $std ); ?>"
  87. rows="<?php echo absint( $rows ); ?>"
  88. cols="<?php echo absint( $cols ); ?>"
  89. <?php echo isset( $field['data'] ) ? Pngx__Admin__Fields::toggle( $field['data'], null ) : ''; ?>
  90. ><?php
  91. if ( version_compare( $wp_version, '4.3', '<' ) ) {
  92. echo wp_htmledit_pre( $value );
  93. } else {
  94. echo format_for_editor( $value );
  95. }
  96. ?></textarea>
  97. <?php
  98. if ( isset( $field['desc'] ) && ! empty( $field['desc'] ) ) {
  99. echo '<span class="description">' . esc_html( $field['desc'] ) . '</span>';
  100. }
  101. }
  102. }