ITSG_GF_WYSIWYG_CKEditor

The Gravity Forms - WYSIWYG CKEditor ITSG GF WYSIWYG CKEditor class.

Defined (1)

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

/gf_wysiwyg_ckeditor_plugin.php  
  1. class ITSG_GF_WYSIWYG_CKEditor 
  2. private static $name = 'Gravity Forms - WYSIWYG CKEditor'; 
  3. private static $slug = 'itsg_gf_wysiwyg_ckeditor'; 
  4.  
  5. /** 
  6. * Construct the plugin object 
  7. */ 
  8. public function __construct() 
  9. // register actions 
  10. if (self::is_gravityforms_installed()) { 
  11. //start plug in 
  12. add_action('gform_enqueue_scripts', array(&$this, 'itsg_gf_wysiwyg_ckeditor_js_script'), 10, 2); 
  13. add_filter('gform_save_field_value', array(&$this, 'itsg_gf_wysiwyg_ckeditor_save_values'), 10, 4); 
  14. add_action('gform_field_standard_settings', array(&$this, 'itsg_gf_wysiwyg_ckeditor_field_settings'), 10, 2); 
  15. add_filter('gform_tooltips', array(&$this, 'itsg_gf_wysiwyg_ckeditor_field_tooltips')); 
  16. add_action('gform_editor_js', array(&$this, 'itsg_gf_wysiwyg_ckeditor_editor_js')); 
  17. add_action('gform_field_css_class', array(&$this, 'itsg_gf_wysiwyg_ckeditor_css_class'), 10, 3); 
  18. add_filter('gform_field_content', array(&$this, 'itsg_gf_wysiwyg_ckeditor_add_max_char'), 10, 5); 
  19. add_filter('gform_counter_script', array(&$this, 'itsg_gf_wysiwyg_ckeditor_counter_script'), 10, 4); 
  20. register_activation_hook( __FILE__, array(&$this, 'itsg_gf_wysiwyg_ckeditor_plugin_defaults')); 
  21.  
  22. if(RGForms::get("page") == "gf_settings") { 
  23. // add settings page 
  24. RGForms::add_settings_page("WYSIWYG ckeditor", array("ITSG_GF_WYSIWYG_ckeditor_settings_page", "settings_page"), self::get_base_url() . "/images/user-registration-icon-32.png"); 
  25.  
  26. /** 
  27. * Place ckeditor JavaScript in footer, applies ckeditor to 'textarea' fields  
  28. */ 
  29. public static function itsg_gf_wysiwyg_ckeditor_js_script($form, $is_ajax) { 
  30. foreach ( $form['fields'] as $field ) { 
  31. if (self::is_wysiwyg_ckeditor($field)) { 
  32.  
  33. $ur_settings = get_option('ITSG_gf_wysiwyg_ckeditor_settings') ? get_option('ITSG_gf_wysiwyg_ckeditor_settings') : array(); 
  34. ?> 
  35. <script type="text/javascript" src="<?php echo plugins_url( '/ckeditor/ckeditor.js', __FILE__ ) ?>"></script> 
  36. <script type="text/javascript" src="<?php echo plugins_url( '/ckeditor/adapters/jquery.js', __FILE__ ) ?>"></script> 
  37. <script type="text/javascript"> 
  38. (function( $ ) { 
  39. "use strict"; 
  40. $(function() { 
  41. $('.gform_wysiwyg_ckeditor textarea').each(function() { 
  42. $(this).ckeditor(CKEDITOR.tools.extend( { 
  43. extraPlugins : 'wordcount',  
  44. wordcount : { 
  45. showParagraphs : false,  
  46. showWordCount: false,  
  47. showCharCount: true,  
  48. charLimit: $(this).attr('data-maxlen'),  
  49. hardLimit: true  
  50. },  
  51. toolbar: [ 
  52. <?php /** SOURCE */ 
  53. if (rgar($ur_settings, 'enable_source')) { echo "{ name: 'source', items: [ 'Source' ] }, ";} ?> 
  54. { name: 'basicstyles', items: [ <?php /** BASIC STYLES */  
  55. if (rgar($ur_settings, 'enable_bold')) { echo "'Bold', ";}  
  56. if (rgar($ur_settings, 'enable_italic')) { echo "'Italic', ";}  
  57. if (rgar($ur_settings, 'enable_underline')) { echo "'Underline', ";}  
  58. if (rgar($ur_settings, 'enable_strike')) { echo "'Strike', ";}  
  59. if (rgar($ur_settings, 'enable_subscript')) { echo "'Subscript', ";}  
  60. if (rgar($ur_settings, 'enable_superscript')) { echo "'Superscript', ";}  
  61. if (rgar($ur_settings, 'enable_removeformat')) { echo "'-', 'RemoveFormat'";} ?>  
  62. ] },  
  63. { name: 'clipboard', items: [ <?php /** CLIPBOARD */ 
  64. if (rgar($ur_settings, 'enable_cut')) { echo "'Cut', ";}  
  65. if (rgar($ur_settings, 'enable_copy')) { echo "'Copy', ";}  
  66. if (rgar($ur_settings, 'enable_paste')) { echo "'Paste', ";}  
  67. if (rgar($ur_settings, 'enable_pastetext')) { echo "'PasteText', ";}  
  68. if (rgar($ur_settings, 'enable_pastefromword')) { echo "'PasteFromWord', ";}  
  69. if (rgar($ur_settings, 'enable_undo')) { echo "'-', 'Undo', ";}  
  70. if (rgar($ur_settings, 'enable_redo')) { echo "'Redo'";} ?>  
  71. ] },  
  72. { name: 'paragraph', items: [ <?php /** PARAGRAPH */ 
  73. if (rgar($ur_settings, 'enable_numberedlist')) { echo "'NumberedList', ";}  
  74. if (rgar($ur_settings, 'enable_bulletedlist')) { echo "'BulletedList', ";}  
  75. if (rgar($ur_settings, 'enable_outdent')) { echo "'-', 'Outdent', ";}  
  76. if (rgar($ur_settings, 'enable_indent')) { echo "'Indent', ";}  
  77. if (rgar($ur_settings, 'enable_blockquote')) { echo "'-', 'Blockquote', ";}  
  78. if (rgar($ur_settings, 'enable_creatediv')) { echo "'CreateDiv', ";}  
  79. if (rgar($ur_settings, 'enable_justifyleft')) { echo "'-', 'JustifyLeft', ";}  
  80. if (rgar($ur_settings, 'enable_justifycenter')) { echo "'JustifyCenter', ";}  
  81. if (rgar($ur_settings, 'enable_justifyright')) { echo "'JustifyRight', ";}  
  82. if (rgar($ur_settings, 'enable_justifyblock')) { echo "'JustifyBlock', ";}  
  83. if (rgar($ur_settings, 'enable_bidiltr')) { echo "'-', 'BidiLtr', ";}  
  84. if (rgar($ur_settings, 'enable_bidirtl')) { echo "'BidiRtl', ";}  
  85. if (rgar($ur_settings, 'enable_language')) { echo "'Language'";} ?>  
  86. ] },  
  87. { name: 'links', items: [ <?php /** LINKS */ 
  88. if (rgar($ur_settings, 'enable_link')) { echo "'Link', ";}  
  89. if (rgar($ur_settings, 'enable_unlink')) { echo "'Unlink', ";}  
  90. if (rgar($ur_settings, 'enable_anchor')) { echo "'Anchor'";} ?>  
  91. ] },  
  92. { name: 'document', items: [ <?php /** DOCUMENT */ 
  93. if (rgar($ur_settings, 'enable_preview')) { echo "'Preview', ";} 
  94. if (rgar($ur_settings, 'enable_print')) { echo "'Print', ";} ?>  
  95. ] },  
  96. { name: 'editing', items: [ <?php /** EDITING */ 
  97. if (rgar($ur_settings, 'enable_find')) { echo "'Find', ";} 
  98. if (rgar($ur_settings, 'enable_replace')) { echo "'Replace', ";} 
  99. if (rgar($ur_settings, 'enable_selectall')) { echo "'-', 'SelectAll', ";} 
  100. if (rgar($ur_settings, 'enable_scayt')) { echo "'-', 'Scayt'";} ?> 
  101. ] },  
  102. { name: 'insert', items: [ <?php /** INSERT */ 
  103. if (rgar($ur_settings, 'enable_image')) { echo "'Image', ";} 
  104. if (rgar($ur_settings, 'enable_flash')) { echo "'Flash', ";} 
  105. if (rgar($ur_settings, 'enable_table')) { echo "'Table', ";} 
  106. if (rgar($ur_settings, 'enable_horizontalrule')) { echo "'HorizontalRule', ";} 
  107. if (rgar($ur_settings, 'enable_smiley')) { echo "'Smiley', ";} 
  108. if (rgar($ur_settings, 'enable_specialchar')) { echo "'SpecialChar', ";} 
  109. if (rgar($ur_settings, 'enable_pagebreak')) { echo "'PageBreak', ";} 
  110. if (rgar($ur_settings, 'enable_iframe')) { echo "'Iframe'";} ?> 
  111. ] },  
  112. '/',  
  113. { name: 'styles', items: [ <?php /** STYLES */ 
  114. if (rgar($ur_settings, 'enable_styles')) { echo "'Styles', ";} 
  115. if (rgar($ur_settings, 'enable_format')) { echo "'Format', ";} 
  116. if (rgar($ur_settings, 'enable_font')) { echo "'Font', ";} 
  117. if (rgar($ur_settings, 'enable_fontsize')) { echo "'FontSize'";} ?> 
  118. ] },  
  119. { name: 'colors', items: [ <?php /** COLOURS */ 
  120. if (rgar($ur_settings, 'enable_textcolor')) { echo "'TextColor', ";} 
  121. if (rgar($ur_settings, 'enable_bgcolor')) { echo "'BGColor'";} ?> 
  122. ] },  
  123. { name: 'tools', items: [ <?php /** TOOLS */ 
  124. if (rgar($ur_settings, 'enable_maximize')) { echo "'Maximize', ";} 
  125. if (rgar($ur_settings, 'enable_showblocks')) { echo "'ShowBlocks'";} ?> 
  126. ] },  
  127. { name: 'about', items: [ <?php /** ABOUT */ 
  128. if (rgar($ur_settings, 'enable_about')) { echo "'About'";} ?> 
  129. ] }],  
  130. allowedContent: true 
  131. })); 
  132.  
  133. <?php if (self::is_dpr_installed() ) { ?> 
  134. for (var i in CKEDITOR.instances) { 
  135. CKEDITOR.instances[i].on('change', function() {changed = true;}); 
  136. <?php } ?> 
  137. }); 
  138.  
  139. }); 
  140. }(jQuery)); 
  141. </script> 
  142.  
  143. <?php 
  144.  
  145. /** 
  146. * Customises 'Paragraph Text' field output to include character limit details 
  147. */ 
  148. public static function itsg_gf_wysiwyg_ckeditor_add_max_char($content, $field, $value, $lead_id, $form_id) { 
  149. $field_type = rgar($field, "type"); 
  150. if ('post_content' == $field_type || 'textarea' == $field_type || ('post_custom_field' == $field_type && 'textarea' == $field['inputType'])) { 
  151. $label = rgar($field, "label"); 
  152. $limit = rgar($field, "maxLength"); 
  153. if ('' == $limit) {$limit = 'unlimited';}; 
  154. $content = str_replace("<textarea ", "<textarea data-maxlen='".$limit."'", $content); 
  155. return $content; 
  156. } // END itsg_gf_wysiwyg_ckeditor_add_max_char 
  157.  
  158. /** 
  159. * Customises character limit count down for NON-CKEditor fields to match what CKEditor provides  
  160. * - note that these fields DO count spaces, where as CKEDitor does NOT count spaces. 
  161. */ 
  162. public static function itsg_gf_wysiwyg_ckeditor_counter_script($script, $form_id, $field_id, $max_length) { 
  163. $form = GFFormsModel::get_form_meta($form_id); 
  164. $field_id_id = str_replace("input_".$form_id."_", "", $field_id); 
  165. $field = GFFormsModel::get_field($form, $field_id_id); 
  166. if (true == rgar($field, "enable_wysiwyg_ckeditor")) 
  167. {return "";} else { 
  168. $script = "jQuery('#{$field_id}').textareaCount(" . 
  169. " {" . 
  170. " 'maxCharacterSize': {$max_length}, " . 
  171. " 'originalStyle': 'ginput_counter', " . 
  172. " 'displayFormat' : 'Characters: #input (Limit: $max_length)'" . 
  173. " });"; 
  174.  
  175. return $script; 
  176. } // END itsg_gf_wysiwyg_ckeditor_counter_script 
  177.  
  178. /** 
  179. * Applies CSS class to 'Paragraph text' fields when CKEditor is enabled 
  180. */ 
  181. public static function itsg_gf_wysiwyg_ckeditor_css_class($classes, $field, $form) 
  182. $field_type = rgar($field, "type"); 
  183. if ('post_content' == $field_type || 'textarea' == $field_type || ('post_custom_field' == $field_type && 'textarea' == $field['inputType'])) { 
  184. if (array_key_exists('enable_wysiwyg_ckeditor', $field)) { 
  185. $classes .= " gform_wysiwyg_ckeditor"; 
  186. return $classes; 
  187. } // END itsg_gf_wysiwyg_ckeditor_css_class 
  188.  
  189. /** 
  190. * Applies 'Enable WYSIWYG CKEditor' option to 'Paragraph Text' field 
  191. */ 
  192. public static function itsg_gf_wysiwyg_ckeditor_field_settings($position, $form_id) { 
  193. if ($position == 25) { 
  194. ?> 
  195. <li class="wysiwyg_field_setting_wysiwyg_ckeditor field_setting" style="display:list-item;"> 
  196. <input type="checkbox" id="field_enable_wysiwyg_ckeditor"/> 
  197. <label for="field_enable_wysiwyg_ckeditor" class="inline"> 
  198. <?php _e("Enable WYSIWYG (CKEditor)", "gravityforms"); ?> 
  199. </label> 
  200. <?php gform_tooltip("form_field_enable_wysiwyg_ckeditor") ?><br/> 
  201. </li> 
  202. <?php 
  203.  
  204. /** 
  205. * JavaScript for form editor 
  206. */ 
  207. public static function itsg_gf_wysiwyg_ckeditor_editor_js() { 
  208. ?> 
  209. <script type='text/javascript'> 
  210.  
  211. jQuery(document).bind("gform_load_field_settings", function (event, field, form) { 
  212. var field_type = field['type']; 
  213. if ('post_content' == field_type || 'textarea' == field_type || ('post_custom_field' == field_type && 'textarea' == field['inputType'])) { 
  214.  
  215. var $wysiwyg_container = jQuery(".wysiwyg_field_setting_wysiwyg_ckeditor"); 
  216.  
  217. $wysiwyg_container.show(); 
  218.  
  219. var enable_wysiwyg_ckeditor = (typeof field['enable_wysiwyg_ckeditor'] != 'undefined' && field['enable_wysiwyg_ckeditor'] != '') ? field['enable_wysiwyg_ckeditor'] : false; 
  220.  
  221. if (enable_wysiwyg_ckeditor != false) { 
  222. //check the checkbox if previously checked 
  223. $wysiwyg_container.find("input:checkbox").attr("checked", "checked"); 
  224. } else { 
  225. $wysiwyg_container.find("input:checkbox").removeAttr("checked"); 
  226.  
  227. }); 
  228.  
  229. jQuery(".wysiwyg_field_setting_wysiwyg_ckeditor input").click(function () { 
  230. if (jQuery(this).is(":checked")) { 
  231. SetFieldProperty('enable_wysiwyg_ckeditor', 'true'); 
  232. } else { 
  233. SetFieldProperty('enable_wysiwyg_ckeditor', ''); 
  234. }); 
  235.  
  236. </script> 
  237. <?php 
  238.  
  239. /** 
  240. * Tooltip for field in form editor 
  241. */ 
  242. public static function itsg_gf_wysiwyg_ckeditor_field_tooltips($tooltips) { 
  243. $tooltips["form_field_enable_wysiwyg_ckeditor"] = "<h6>Enable WYSIWYG</h6>Check this box to turn this field into a WYSIWYG editor, using ckeditor."; 
  244. return $tooltips; 
  245.  
  246. /** 
  247. * Checks if field is CKEditor enabled 
  248. */ 
  249. private static function is_wysiwyg_ckeditor($field) { 
  250. $type = self::get_type($field); 
  251. if ('post_content' == $type || 'textarea' == $type) { 
  252. if (array_key_exists('enable_wysiwyg_ckeditor', $field)) { 
  253. return $field['enable_wysiwyg_ckeditor'] == 'true'; 
  254. return false; 
  255.  
  256. /** 
  257. * Get field type 
  258. */ 
  259. private static function get_type($field) { 
  260. $type = ''; 
  261. if (array_key_exists('type', $field)) { 
  262. $type = $field['type']; 
  263. if ($type == 'post_custom_field') { 
  264. if (array_key_exists('inputType', $field)) { 
  265. $type = $field['inputType']; 
  266.  
  267. return $type; 
  268.  
  269. /** 
  270. * Handles data from WYSIWYG when form is submitted 
  271. */ 
  272. public static function itsg_gf_wysiwyg_ckeditor_save_values($value, $lead, $field, $form) { 
  273. if (self::is_wysiwyg_ckeditor($field)) { 
  274. $value = rgpost("input_{$field['id']}"); 
  275. return $value; 
  276.  
  277. /** 
  278. * Warning message if Gravity Forms is installed and enabled 
  279. */ 
  280. public static function admin_warnings() { 
  281. if ( !self::is_gravityforms_installed() ) { 
  282. $message = __('Requires Gravity Forms to be installed.', self::$slug); 
  283. }  
  284.  
  285. if (empty($message)) { 
  286. return; 
  287. ?> 
  288. <div class="error"> 
  289. <p> 
  290. <?php _e('The plugin ', self::$slug); ?><strong><?php echo self::$name; ?></strong> <?php echo $message; ?><br /> 
  291. <?php _e('Please ', self::$slug); ?><a href="http://www.gravityforms.com/"><?php _e(' download the latest version', self::$slug); ?></a><?php _e(' of Gravity Forms and try again.', self::$slug) ?> 
  292. </p> 
  293. </div> 
  294. <?php 
  295.  
  296. /** 
  297. * Called when plugin activated, sets default options  
  298. */ 
  299. public static function itsg_gf_wysiwyg_ckeditor_plugin_defaults() { 
  300. //the default options 
  301. $ITSG_gf_wysiwyg_ckeditor_settings = array( 
  302. 'enable_bold' => 'on',  
  303. 'enable_italic' => 'on',  
  304. 'enable_underline' => 'on',  
  305. 'enable_pastetext' => 'on',  
  306. 'enable_pastefromword' => 'on',  
  307. 'enable_numberedlist' => 'on',  
  308. 'enable_bulletedlist' => 'on',  
  309. 'enable_outdent' => 'on',  
  310. 'enable_indent' => 'on',  
  311. 'enable_link' => 'on',  
  312. 'enable_unlink' => 'on',  
  313. 'enable_format' => 'on',  
  314. 'enable_font' => 'on',  
  315. 'enable_fontsize' => 'on',  
  316. ); 
  317.  
  318. //check to see if present already 
  319. if(!get_option('ITSG_gf_wysiwyg_ckeditor_settings')) { 
  320. //option not found, add new 
  321. add_option('ITSG_gf_wysiwyg_ckeditor_settings', $ITSG_gf_wysiwyg_ckeditor_settings); 
  322. } // END itsg_gf_wysiwyg_ckeditor_plugin_defaults 
  323.  
  324. /** 
  325. * Check if GF is installed 
  326. */ 
  327. private static function is_gravityforms_installed() 
  328. return class_exists('GFAPI'); 
  329. } // END is_gravityforms_installed 
  330.  
  331. /** 
  332. * Check if Gravity Forms - Data Persistence Reloaded is installed 
  333. */ 
  334. private static function is_dpr_installed()  
  335. return function_exists('ri_gfdp_ajax'); 
  336. } // END is_dpr_installed 
  337.  
  338. /** 
  339. * Get plugin url 
  340. */ 
  341. private static function get_base_url() { 
  342. return plugins_url(null, __FILE__); 
  343.  
  344.  
  345.