PageRenderTime 27ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/gravity-forms-wysiwyg/gf_wysiwyg_class.php

https://gitlab.com/Blueprint-Marketing/interoccupy.net
PHP | 229 lines | 149 code | 63 blank | 17 comment | 34 complexity | a6135404d9d5faea9a00a214eddb7719 MD5 | raw file
  1. <?php
  2. if (!class_exists('gf_wysiwyg_logic')) {
  3. /*
  4. * Base class for the GFCPT Addon. All common code is in here and differences per version are overrided
  5. */
  6. class gf_wysiwyg_logic {
  7. protected $_wysiwygs = array();
  8. /*
  9. * Main initilize method for wiring up all the hooks
  10. */
  11. public function init() {
  12. //adds the button to the std field box
  13. add_filter( 'gform_add_field_buttons', array(&$this, 'add_wysiwyg_button') );
  14. // Adds title to GF custom field
  15. add_filter( 'gform_field_type_title', array(&$this, 'wysiwyg_title') );
  16. add_action( 'gform_field_input', array(&$this, 'wysiwyg_field_input'), 10, 5 );
  17. add_action( 'gform_editor_js', array(&$this, 'wysiwyg_editor_js') );
  18. add_action( 'gform_enqueue_scripts' , array(&$this, 'wysiwyg_enqueue_scripts') , 10 , 2 );
  19. add_filter( 'gform_save_field_value' , array(&$this, 'save_wysiwyg_values'), 10, 4);
  20. //add our advanced options to the form builder
  21. add_action( 'gform_field_standard_settings', array(&$this, 'wysiwyg_field_settings'), 10, 2);
  22. //include javascript for the form builder
  23. add_action( 'gform_editor_js', array(&$this, 'render_editor_js'));
  24. // filter to add a new tooltip
  25. //add_filter( 'gform_tooltips', array(&$this, 'add_gf_tooltips'));
  26. add_action( 'gform_preview_footer', array(&$this, 'render_preview_footer_js') );
  27. }
  28. function add_wysiwyg_button( $field_groups ) {
  29. foreach( $field_groups as &$group ){
  30. // to add to the Standard Fields
  31. if( $group['name'] == 'standard_fields' ) {
  32. $group['fields'][] = array(
  33. 'class' => 'button',
  34. 'value' => __('WYSIWYG', 'gravityforms'),
  35. 'onclick' => "StartAddField('wysiwyg');"
  36. );
  37. break;
  38. }
  39. }
  40. return $field_groups;
  41. }
  42. function wysiwyg_title( $type ) {
  43. if ( $type == 'wysiwyg' ) {
  44. return __( 'WYSIWYG' , 'gravityforms' );
  45. }
  46. }
  47. function wysiwyg_field_input( $input, $field, $value, $lead_id, $form_id ) {
  48. if ( $this->is_wysiwyg($field) ) {
  49. $input_id = 'input_' . $form_id . '_' . $field["id"];
  50. if (is_admin()) {
  51. $tabindex = GFCommon::get_tabindex();
  52. return sprintf("<div class='ginput_container'><textarea readonly name='input_%s' id='input_%s' class='textarea gform_wysiwyg' {$tabindex} rows='10' cols='50'>WYSIWYG editor</textarea></div>", $field["id"], 'wysiwyg-'.$field['id']);
  53. } else {
  54. if (array_key_exists($field["id"], $this->_wysiwygs)) {
  55. return $this->_wysiwygs[$field["id"]];
  56. }
  57. return "";
  58. }
  59. }
  60. return false;
  61. }
  62. function wysiwyg_editor_js () {
  63. ?>
  64. <script type='text/javascript'>
  65. jQuery(document).ready(function($) {
  66. //this determines which fields to show for the wysiwyg field
  67. fieldSettings["wysiwyg"] = ".label_setting, .description_setting, .rules_setting, .admin_label_setting, .default_value_textarea_setting, .error_message_setting, .css_class_setting, .visibility_setting";
  68. });
  69. </script>
  70. <?php
  71. }
  72. function wysiwyg_enqueue_scripts( $form, $ajax ) {
  73. if (sizeof($this->_wysiwygs)>0) { return; }
  74. foreach ( $form['fields'] as $field ) {
  75. if ( $this->is_wysiwyg($field) ) {
  76. //$tabindex = GFCommon::$tab_index > 0 ? GFCommon::$tab_index++ : 0;
  77. $input_id = 'input_' . $form["id"] . '_' . $field["id"];
  78. $args = array(
  79. 'textarea_name' => 'input_'.$field["id"],
  80. 'wpautop' => true,
  81. 'media_buttons' => false,
  82. 'editor_class' => 'frontend',
  83. 'textarea_rows' => 5,
  84. 'tabindex' => 0 //$tabindex
  85. );
  86. ob_start();
  87. wp_editor('', $input_id, $args);
  88. $html = ob_get_contents();
  89. ob_end_clean();
  90. $this->_wysiwygs[$field["id"]] = "<div class='ginput_container'>" . $html . "</div>";
  91. }
  92. }
  93. }
  94. function is_wysiwyg($field) {
  95. $type = $this->get_type($field);
  96. if ( $type == 'wysiwyg' ) { return true; }
  97. else if ( $type = 'post_content' ) {
  98. if ( array_key_exists( 'enableWysiwyg', $field ) ) {
  99. return $field['enableWysiwyg'] == 'true';
  100. }
  101. }
  102. return false;
  103. }
  104. function save_wysiwyg_values($value, $lead, $field, $form) {
  105. if ( $this->is_wysiwyg($field) ) {
  106. $value = rgpost("input_{$field['id']}");
  107. }
  108. return $value;
  109. }
  110. function wysiwyg_field_settings($position, $form_id) {
  111. if($position == 25) { ?>
  112. <li class="wysiwyg_field_setting field_setting" style="display:list-item;">
  113. <input type="checkbox" id="field_enable_wysiwyg" />
  114. <label for="field_enable_wysiwyg" class="inline">
  115. <?php _e("Enable WYSIWYG", "gravityforms"); ?>
  116. </label>
  117. <?php gform_tooltip("form_field_enable_wysiwyg") ?><br />
  118. </li>
  119. <?php
  120. }
  121. }
  122. function render_editor_js() { ?>
  123. <script type='text/javascript'>
  124. jQuery(document).bind("gform_load_field_settings", function(event, field, form){
  125. var field_type = field['type'];
  126. if (field_type == 'post_content') {
  127. var $wysiwyg_container = jQuery(".wysiwyg_field_setting");
  128. $wysiwyg_container.show();
  129. var enableWysiwyg = (typeof field['enableWysiwyg'] != 'undefined' && field['enableWysiwyg'] != '') ? field['enableWysiwyg'] : false;
  130. if (enableWysiwyg != false) {
  131. //check the checkbox if previously checked
  132. $wysiwyg_container.find("input:checkbox").attr("checked", "checked");
  133. } else {
  134. $wysiwyg_container.find("input:checkbox").removeAttr("checked");
  135. }
  136. }
  137. });
  138. jQuery(".wysiwyg_field_setting input").click(function() {
  139. if (jQuery(this).is(":checked")) {
  140. SetFieldProperty('enableWysiwyg', 'true');
  141. } else {
  142. SetFieldProperty('enableWysiwyg', '');
  143. }
  144. });
  145. </script>
  146. <?php
  147. }
  148. function render_preview_footer_js() {
  149. wp_footer();
  150. }
  151. function get_type($field) {
  152. $type = '';
  153. if ( array_key_exists( 'type', $field ) ) {
  154. $type = $field['type'];
  155. if ($type == 'post_custom_field') {
  156. if ( array_key_exists( 'inputType', $field ) ) {
  157. $type = $field['inputType'];
  158. //print_r($type);
  159. }
  160. }
  161. return $type;
  162. }
  163. }
  164. }
  165. }
  166. ?>