Advertisement
agordon86

CKEditor for Gravity Forms patch - gf_wysiwyg_ckeditor_plugi

Nov 14th, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 34.84 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: CKEditor WYSIWYG for Gravity Forms
  4. Description: Use the CKEditor WYSIWYG in your Gravity Forms
  5. Version: 1.9.3
  6. Author: Adrian Gordon
  7. Author URI: http://www.itsupportguides.com
  8. License: GPL2
  9. Text Domain: gravity-forms-wysiwyg-ckeditor
  10.  
  11. ------------------------------------------------------------------------
  12. Copyright 2015 Adrian Gordon
  13.  
  14. This program is free software; you can redistribute it and/or modify
  15. it under the terms of the GNU General Public License as published by
  16. the Free Software Foundation; either version 2 of the License, or
  17. (at your option) any later version.
  18.  
  19. This program is distributed in the hope that it will be useful,
  20. but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. GNU General Public License for more details.
  23.  
  24. You should have received a copy of the GNU General Public License
  25. along with this program; if not, write to the Free Software
  26. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  27.  
  28. */
  29.  
  30. if ( ! defined( 'ABSPATH' ) ) {
  31.     die();
  32. }
  33.  
  34. load_plugin_textdomain( 'gravity-forms-wysiwyg-ckeditor', false, dirname( plugin_basename( __FILE__ ) ) . '/lang' );
  35.  
  36. // include upload handler for image upload feature -- extends default classes
  37. if ( !class_exists( 'UploadHandler' ) && version_compare( phpversion(), '5.4', '>=' ) ) {
  38.     require_once( plugin_dir_path( __FILE__ ).'UploadHandler.php' );
  39. }
  40.  
  41. if ( class_exists( 'UploadHandler' ) ) {
  42.     class ITSG_GFCKEDITOR_UploadHandler extends UploadHandler {
  43.         public function post( $print_response = true ) {
  44.             $upload = $this->get_upload_data( $this->options['param_name'] );
  45.             // Parse the Content-Disposition header, if available:
  46.             $content_disposition_header = $this->get_server_var( 'HTTP_CONTENT_DISPOSITION' );
  47.             $file_name = $content_disposition_header ?
  48.             rawurldecode(preg_replace(
  49.             '/(^[^"]+")|("$)/',
  50.             '',
  51.             $content_disposition_header
  52.             )) : null;
  53.             // Parse the Content-Range header, which has the following form:
  54.             // Content-Range: bytes 0-524287/2000000
  55.             $content_range_header = $this->get_server_var( 'HTTP_CONTENT_RANGE' );
  56.             $content_range = $content_range_header ?
  57.             preg_split('/[^0-9]+/', $content_range_header ) : null;
  58.             $size =  $content_range ? $content_range[3] : null;
  59.             $files = array();
  60.             if ( $upload ) {
  61.                 if ( is_array( $upload['tmp_name'] ) ) {
  62.                     // param_name is an array identifier like "files[]",
  63.                     // $upload is a multi-dimensional array:
  64.                     foreach ( $upload['tmp_name'] as $index => $value ) {
  65.                         $files[] = $this->handle_file_upload (
  66.                             $upload['tmp_name'][$index],
  67.                             $file_name ? $file_name : $upload['name'][$index],
  68.                             $size ? $size : $upload['size'][$index],
  69.                             $upload['type'][$index],
  70.                             $upload['error'][$index],
  71.                             $index,
  72.                             $content_range
  73.                         );
  74.                     }
  75.                 } else {
  76.                     // param_name is a single object identifier like "file",
  77.                     // $upload is a one-dimensional array:
  78.                     $files[] = $this->handle_file_upload(
  79.                         isset( $upload['tmp_name'] ) ? $upload['tmp_name'] : null,
  80.                         $file_name ? $file_name : ( isset($upload['name'] ) ?
  81.                                 $upload['name'] : null ),
  82.                         $size ? $size : (isset($upload['size']) ?
  83.                                 $upload['size'] : $this->get_server_var( 'CONTENT_LENGTH' ) ),
  84.                         isset( $upload['type'] ) ?
  85.                                 $upload['type'] : $this->get_server_var( 'CONTENT_TYPE' ),
  86.                         isset( $upload['error'] )  ? $upload['error'] : null,
  87.                         null,
  88.                         $content_range
  89.                     );
  90.                 }
  91.             }
  92.             $CKEditorFuncNum = $this->options['CKEditorFuncNum'];
  93.             $download_url = $files[0]->url;
  94.             $response = "<script>
  95. var l = '".$download_url."';
  96. window.parent.CKEDITOR.tools.callFunction(
  97. '" . $CKEditorFuncNum . "',
  98. l,
  99. '".  $files[0]->error ."'
  100. );
  101. </script>";
  102.             return $this->generate_response( $response, $print_response );
  103.         }
  104.  
  105.         public function generate_response( $content, $print_response = true ) {
  106.             $this->response = $content;
  107.             $this->body( $content );
  108.             return $content;
  109.         }
  110.  
  111.         protected function trim_file_name( $file_path, $name, $size, $type, $error, $index, $content_range ) {
  112.             $name = apply_filters( 'itsg_gf_ckeditor_filename', $name, $file_path, $size, $type, $error, $index, $content_range );
  113.  
  114.             $exclude_characters = array(
  115.                 '\\',
  116.                 '/',
  117.                 ':',
  118.                 ';',
  119.                 '*',
  120.                 '?',
  121.                 '!',
  122.                 '"',
  123.                 '`',
  124.                 "'",
  125.                 '<',
  126.                 '>',
  127.                 '{',
  128.                 '}',
  129.                 '[',
  130.                 ']',
  131.                 ',',
  132.                 '|'
  133.                 );
  134.             $exclude_characters = (array)apply_filters( 'itsg_gf_ckeditor_filename_exclude_characters', $exclude_characters );
  135.             $replace_character = (string)apply_filters( 'itsg_gf_ckeditor_filename_replace_characters', '' );
  136.             $name = str_replace( $exclude_characters, $replace_character, $name );
  137.  
  138.             return $name;
  139.         }
  140.     } // END ITSG_GFCKEDITOR_UploadHandler
  141. }
  142.  
  143. add_action( 'admin_notices', array( 'ITSG_GF_WYSIWYG_CKEditor', 'admin_warnings' ), 20 );
  144. load_plugin_textdomain( 'itsg_gf_wysiwyg_ckeditor', false, dirname( plugin_basename( __FILE__ ) ) . '/lang' );
  145.  
  146. require_once( plugin_dir_path( __FILE__ ).'gf_wysiwyg_ckeditor_settings.php' );
  147.  
  148. if ( !class_exists( 'ITSG_GF_WYSIWYG_CKEditor' ) ) {
  149.     class ITSG_GF_WYSIWYG_CKEditor {
  150.     private static $name = 'CKEditor WYSIWYG for Gravity Forms';
  151.     private static $slug = 'gravity-forms-wysiwyg-ckeditor';
  152.  
  153.         /*
  154.          * Construct the plugin object
  155.          */
  156.         public function __construct() {
  157.             // register plugin functions through 'gform_loaded' -
  158.             // this delays the registration until Gravity Forms has loaded, ensuring it does not run before Gravity Forms is available.
  159.             add_action( 'gform_loaded', array( $this, 'register_actions' ) );
  160.         } // END __construct
  161.  
  162.         /*
  163.          * Register plugin functions
  164.          */
  165.         function register_actions() {
  166.         // register actions
  167.             if ( self::is_gravityforms_installed() ) {
  168.                 $ckeditor_settings = self::get_options();
  169.  
  170.                 // addon framework
  171.                 require_once( plugin_dir_path( __FILE__ ).'gf-wysiwyg-ckeditor-addon.php' );
  172.  
  173.                 //start plug in
  174.  
  175.                 add_action( 'wp_ajax_itsg_gf_wysiwyg_ckeditor_upload', array( $this, 'itsg_gf_wysiwyg_ckeditor_upload' ) );
  176.                 add_action( 'wp_ajax_nopriv_itsg_gf_wysiwyg_ckeditor_upload', array( $this, 'itsg_gf_wysiwyg_ckeditor_upload' ) );
  177.  
  178.                 add_filter( 'gform_save_field_value', array( $this, 'save_field_value' ), 10, 4 );
  179.                 add_action( 'gform_field_standard_settings', array( $this, 'ckeditor_field_settings' ), 10, 2 );
  180.                 add_filter( 'gform_tooltips', array( $this, 'ckeditor_field_tooltips' ) );
  181.                 add_action( 'gform_field_css_class', array( $this, 'ckeditor_field_css_class' ), 10, 3 );
  182.                 add_filter( 'gform_field_content',  array( $this, 'ckeditor_field_content' ), 10, 5 );
  183.                 add_filter( 'gform_counter_script', array( $this, 'ckeditor_counter_script_js' ), 10, 4 );
  184.                 add_filter( 'gform_merge_tag_filter', array( $this, 'decode_wysiwyg_frontend_confirmation' ), 10, 5 );
  185.                 add_filter( 'gform_entry_field_value', array( $this, 'decode_wysiwyg_backend_and_gravitypdf' ), 10, 4 );
  186.                 add_filter( 'plugin_action_links_' . plugin_basename(__FILE__), array( $this, 'plugin_action_links' ) );
  187.                
  188.                 add_filter( 'gform_field_validation', array( $this, 'ckeditor_dont_count_spaces' ), 10, 4 );
  189.  
  190.                 add_filter( 'gform_entries_column_filter', array( $this,  'entries_column_filter' ), 10, 5 );
  191.  
  192.                 if ( self::is_minimum_php_version() ) {
  193.                     require_once( plugin_dir_path( __FILE__ ).'gravitypdf/gravitypdf.php' );
  194.                 }
  195.  
  196.                 // patch to allow JS and CSS to load when loading forms through wp-ajax requests
  197.                 add_action( 'gform_enqueue_scripts', array( $this, 'enqueue_scripts' ), 90, 2 );
  198.  
  199.                 if ( 'gf_settings' == RGForms::get('page') ) {
  200.                     // add settings page
  201.                     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' );
  202.  
  203.                     if ( ( 'WYSIWYG+CKEditor' == RGForms::get('subview') || 'WYSIWYG CKEditor' == RGForms::get('subview') ) && !self::is_minimum_php_version() ) {
  204.                         add_action( 'admin_notices', array( $this, 'admin_warnings_minimum_php_version'), 20 );
  205.                     }
  206.  
  207.                 }
  208.  
  209.                 if ( $ckeditor_settings['enable_upload_image'] && self::is_minimum_php_version() ) {
  210.                     // handles the change upload path settings
  211.                     add_filter( 'gform_upload_path', array( $this, 'change_upload_path' ), 10, 2 );
  212.                 }
  213.             }
  214.         } // END register_actions
  215.        
  216.         public function ckeditor_dont_count_spaces( $result, $value, $form, $field ) {
  217.             if ( $this->is_wysiwyg_ckeditor( $field ) ) {
  218.                 if ( ! is_numeric( $field['maxLength'] ) ) {
  219.                     return $result;
  220.                 }
  221.  
  222.                 $value = strip_tags( $value );
  223.                 $value = preg_replace( '/\r|\n|\s|&nbsp;/', '' , $value ); // remove line breaks and spaces for the purpose of the character count
  224.  
  225.                 if ( GFCommon::safe_strlen( $value ) > $field['maxLength'] ) {
  226.                     $result['is_valid']  = false;
  227.                     $result['message'] = empty( $field['errorMessage'] ) ? esc_html_x( 'The text entered exceeds the maximum number of characters.', 'Same as Gravity Forms (slug: gravityforms) plugin','gravity-forms-wysiwyg-ckeditor' ) : $field['errorMessage'];
  228.                 } elseif ( ! ( $field['isRequired'] && GFCommon::safe_strlen( $value ) == 0 ) ) {
  229.                     $result['is_valid']  = true;
  230.                 }
  231.             }
  232.             return $result;
  233.         }
  234.  
  235.     /**
  236.      * BEGIN: patch to allow JS and CSS to load when loading forms through wp-ajax requests
  237.      *
  238.      */
  239.  
  240.         /*
  241.          * Enqueue JavaScript to footer
  242.          */
  243.         public function enqueue_scripts( $form, $is_ajax ) {
  244.             if ( $this->requires_scripts( $form, $is_ajax ) ) {
  245.                 $min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG || isset( $_GET['gform_debug'] ) ? '' : '.min';
  246.  
  247.                 wp_deregister_script( 'gform_textarea_counter' ); // deregister default textarea count script - the default script counts spaces
  248.                 wp_register_script( 'itsg_gf_ckeditor', plugins_url( "/js/itsg_gf_ckeditor{$min}.js", __FILE__ ),  array( 'jquery', 'gform_textarea_counter', 'ITSG_gf_wysiwyg_ckeditor_js', 'ITSG_gf_wysiwyg_ckeditor_jquery_adapter' ) );
  249.                 wp_enqueue_script( 'gform_textarea_counter', plugins_url( "/js/jquery.textareaCounter.plugin{$min}.js", __FILE__ ) );
  250.                 wp_enqueue_script( 'ITSG_gf_wysiwyg_ckeditor_js', plugins_url( "/ckeditor/ckeditor.js", __FILE__ ) );
  251.                 wp_enqueue_script( 'ITSG_gf_wysiwyg_ckeditor_jquery_adapter', plugins_url( "ckeditor/adapters/jquery.js", __FILE__ ) );
  252.  
  253.                 // Localize the script with new data
  254.                 $this->localize_scripts( $form, $is_ajax );
  255.  
  256.             }
  257.         } // END datepicker_js
  258.  
  259.         public function requires_scripts( $form, $is_ajax ) {
  260.             if ( is_admin() && defined( 'DOING_AJAX' ) && DOING_AJAX && ! GFCommon::is_form_editor() && is_array( $form ) ) {
  261.                 foreach ( $form['fields'] as $field ) {
  262.                     if ( $this->is_wysiwyg_ckeditor( $field ) ) {
  263.                         return true;
  264.                     }
  265.                 }
  266.             }
  267.  
  268.             return false;
  269.         } // END requires_scripts
  270.  
  271.         function localize_scripts( $form, $is_ajax ) {
  272.             // Localize the script with new data
  273.             $form_id = $form['id'];
  274.             $form_id = $form['id'];
  275.             $is_entry_detail = GFCommon::is_entry_detail();
  276.             $is_form_editor = GFCommon::is_form_editor();
  277.             $admin_url = admin_url( 'admin-ajax.php' );
  278.             $ckeditor_settings = ITSG_GF_WYSIWYG_CKEditor::get_options();
  279.  
  280.             $extra_plugins = '';
  281.             if ( ( ! $is_form_editor || ! $is_entry_detail ) || rgar( $ckeditor_settings, 'enable_oembed' ) ) {
  282.                 if ( ! $is_form_editor && ! $is_entry_detail ) {
  283.                     $extra_plugins .=  'wordcount,notification';
  284.                     if ( rgar( $ckeditor_settings, 'enable_oembed' ) ) {
  285.                     $extra_plugins .= ',oembed,widget,dialog';
  286.                     }
  287.                 }
  288.             }
  289.            
  290.             $remove_plugins = '';
  291.             if ( rgar( $ckeditor_settings, 'enable_remove_elementspath' ) ) {
  292.                 $remove_plugins .=  'elementspath';
  293.             }
  294.            
  295.             $ckeditor_fields = array();
  296.  
  297.             $toolbar_settings = array(
  298.                 'source' => array(
  299.                     ! rgar( $ckeditor_settings, 'enable_source' ) ?: 'Source'
  300.                 ),
  301.                 'basicstyles' => array(
  302.                     ! rgar( $ckeditor_settings, 'enable_bold' ) ?: 'Bold',
  303.                     ! rgar( $ckeditor_settings, 'enable_italic' ) ?: 'Italic',
  304.                     ! rgar( $ckeditor_settings, 'enable_underline' ) ?: 'Underline',
  305.                     ! rgar( $ckeditor_settings, 'enable_strike' ) ?: 'Strike',
  306.                     ! rgar( $ckeditor_settings, 'enable_subscript' ) ?: 'Subscript',
  307.                     ! rgar( $ckeditor_settings, 'enable_superscript' ) ?: 'Superscript',
  308.                     ! rgar( $ckeditor_settings, 'enable_removeformat' ) ?: '-',
  309.                     ! rgar( $ckeditor_settings, 'enable_removeformat' ) ?: 'RemoveFormat',
  310.                 ),
  311.                 'clipboard' => array(
  312.                     ! rgar( $ckeditor_settings, 'enable_cut' ) ?: 'Cut',
  313.                     ! rgar( $ckeditor_settings, 'enable_copy' ) ?: 'Copy',
  314.                     ! rgar( $ckeditor_settings, 'enable_paste' ) ?: 'Paste',
  315.                     ! rgar( $ckeditor_settings, 'enable_pastetext' ) ?: 'PasteText',
  316.                     ! rgar( $ckeditor_settings, 'enable_pastefromword' ) ?: 'PasteFromWord',
  317.                     ! rgar( $ckeditor_settings, 'enable_undo' ) ?: '-',
  318.                     ! rgar( $ckeditor_settings, 'enable_undo' ) ?: 'Undo',
  319.                     ! rgar( $ckeditor_settings, 'enable_redo' ) ?: 'Redo',
  320.                 ),
  321.                 'paragraph' => array(
  322.                     ! rgar( $ckeditor_settings, 'enable_numberedlist' ) ?: 'NumberedList',
  323.                     ! rgar( $ckeditor_settings, 'enable_bulletedlist' ) ?: 'BulletedList',
  324.                     ! rgar( $ckeditor_settings, 'enable_outdent' ) ?: 'Outdent',
  325.                     ! rgar( $ckeditor_settings, 'enable_indent' ) ?: 'Indent',
  326.                     ! rgar( $ckeditor_settings, 'enable_blockquote' ) ?: 'Blockquote',
  327.                     ! rgar( $ckeditor_settings, 'enable_creatediv' ) ?: 'CreateDiv',
  328.                     ! rgar( $ckeditor_settings, 'enable_justifyleft' ) ?: '-',
  329.                     ! rgar( $ckeditor_settings, 'enable_justifyleft' ) ?: 'JustifyLeft',
  330.                     ! rgar( $ckeditor_settings, 'enable_justifycenter' ) ?: 'JustifyCenter',
  331.                     ! rgar( $ckeditor_settings, 'enable_justifyright' ) ?: 'JustifyRight',
  332.                     ! rgar( $ckeditor_settings, 'enable_justifyblock' ) ?: 'JustifyBlock',
  333.                     ! rgar( $ckeditor_settings, 'enable_bidiltr' ) ?: '-',
  334.                     ! rgar( $ckeditor_settings, 'enable_bidiltr' ) ?: 'BidiLtr',
  335.                     ! rgar( $ckeditor_settings, 'enable_bidirtl' ) ?: 'BidiRtl',
  336.                     ! rgar( $ckeditor_settings, 'enable_language' ) ?: 'Language',
  337.                 ),
  338.                 'links' => array(
  339.                     ! rgar( $ckeditor_settings, 'enable_link' ) ?: 'Link',
  340.                     ! rgar( $ckeditor_settings, 'enable_unlink' ) ?: 'Unlink',
  341.                     ! rgar( $ckeditor_settings, 'enable_anchor' ) ?: 'Anchor',
  342.                     ! rgar( $ckeditor_settings, 'enable_oembed' ) ?: 'oembed',
  343.                 ),
  344.                 'document' => array(
  345.                     ! rgar( $ckeditor_settings, 'enable_preview' ) ?: 'Preview',
  346.                     ! rgar( $ckeditor_settings, 'enable_print' ) ?: 'Print',
  347.                 ),
  348.                 'editing' => array(
  349.                     ! rgar( $ckeditor_settings, 'enable_find' ) ?: 'Find',
  350.                     ! rgar( $ckeditor_settings, 'enable_replace' ) ?: 'Replace',
  351.                     ! rgar( $ckeditor_settings, 'enable_selectall' ) ?: '-',
  352.                     ! rgar( $ckeditor_settings, 'enable_selectall' ) ?: 'SelectAll',
  353.                     ! rgar( $ckeditor_settings, 'enable_scayt' ) ?: '-',
  354.                     ! rgar( $ckeditor_settings, 'enable_scayt' ) ?: 'Scayt',
  355.                 ),
  356.                 'insert' => array(
  357.                     ! ( rgar( $ckeditor_settings, 'enable_image' ) || ( rgar( $ckeditor_settings, 'enable_upload_image' ) && $this->is_minimum_php_version() ) ) ?: 'Image',
  358.                     ! rgar( $ckeditor_settings, 'enable_flash' ) ?: 'Flash',
  359.                     ! rgar( $ckeditor_settings, 'enable_table' ) ?: 'Table',
  360.                     ! rgar( $ckeditor_settings, 'enable_horizontalrule' ) ?: 'HorizontalRule',
  361.                     ! rgar( $ckeditor_settings, 'enable_smiley' ) ?: 'Smiley',
  362.                     ! rgar( $ckeditor_settings, 'enable_specialchar' ) ?: 'SpecialChar',
  363.                     ! rgar( $ckeditor_settings, 'enable_pagebreak' ) ?: 'PageBreak',
  364.                     ! rgar( $ckeditor_settings, 'enable_iframe' ) ?: 'Iframe',
  365.                 ),
  366.                 'styles' => array(
  367.                     ! rgar( $ckeditor_settings, 'enable_styles' ) ?: 'Styles',
  368.                     ! rgar( $ckeditor_settings, 'enable_format' ) ?: 'Format',
  369.                     ! rgar( $ckeditor_settings, 'enable_font' ) ?: 'Font',
  370.                     ! rgar( $ckeditor_settings, 'enable_fontsize' ) ?: 'FontSize',
  371.                 ),
  372.                 'colors' => array(
  373.                     ! rgar( $ckeditor_settings, 'enable_textcolor' ) ?: 'TextColor',
  374.                     ! rgar( $ckeditor_settings, 'enable_bgcolor' ) ?: 'BGColor',
  375.                 ),
  376.                 'tools' => array(
  377.                     ! rgar( $ckeditor_settings, 'enable_maximize' ) ?: 'Maximize',
  378.                     ! rgar( $ckeditor_settings, 'enable_showblocks' ) ?: 'ShowBlocks',
  379.                 ),
  380.                 'about' => array(
  381.                     ! rgar( $ckeditor_settings, 'enable_about' ) ?: 'About',
  382.                 )
  383.             );
  384.  
  385.             if ( is_array( $form['fields'] ) ) {
  386.                 foreach ( $form['fields'] as $field ) {
  387.                     if ( $this->is_wysiwyg_ckeditor( $field ) ) {
  388.                         $field_id = $field['id'];
  389.                         $ckeditor_fields[ $field_id ][ 'source' ] = $toolbar_settings[ 'source' ] ;
  390.                         $ckeditor_fields[ $field_id ][ 'basicstyles' ] =  $toolbar_settings[ 'basicstyles' ];
  391.                         $ckeditor_fields[ $field_id ][ 'clipboard' ] =  $toolbar_settings[ 'clipboard' ];
  392.                         $ckeditor_fields[ $field_id ][ 'paragraph' ] =  $toolbar_settings[ 'paragraph' ];
  393.                         $ckeditor_fields[ $field_id ][ 'links' ] =  $toolbar_settings[ 'links' ];
  394.                         $ckeditor_fields[ $field_id ][ 'document' ] =  $toolbar_settings[ 'document' ];
  395.                         $ckeditor_fields[ $field_id ][ 'editing' ] =  $toolbar_settings[ 'editing' ];
  396.                         $ckeditor_fields[ $field_id ][ 'insert' ] =  $toolbar_settings[ 'insert' ];
  397.                         $ckeditor_fields[ $field_id ][ 'styles' ] =  $toolbar_settings[ 'styles' ];
  398.                         $ckeditor_fields[ $field_id ][ 'colors' ] =  $toolbar_settings[ 'colors' ];
  399.                         $ckeditor_fields[ $field_id ][ 'tools' ] =  $toolbar_settings[ 'tools' ];
  400.                         $ckeditor_fields[ $field_id ][ 'about' ] =  $toolbar_settings[ 'about' ];
  401.                     }
  402.                 }
  403.             }
  404.  
  405.             // filter to modify global settings on a per form basis
  406.             //$ckeditor_fields = apply_filters( 'itsg_gf_ckeditor_fields', $ckeditor_fields, $form_id );
  407.  
  408.             $settings_array = array(
  409.                 'form_id' => GFCommon::is_entry_detail() ? $_GET['id'] : $form_id,
  410.                 'is_entry_detail' => $is_entry_detail ? $is_entry_detail : 0,
  411.                 'is_form_editor' => $is_form_editor ? $is_form_editor : 0,
  412.                 'is_admin' => is_admin() ? 1 : 0,
  413.                 'is_dpr_installed' => $this->is_dpr_installed() ? 1 : 0,
  414.                 'enable_upload_image' => rgar( $ckeditor_settings, 'enable_upload_image') ? 1 : 0,
  415.                 'is_minimum_php_version' => $this->is_minimum_php_version() ? 1 : 0,
  416.                 'ckeditor_fields' => $ckeditor_fields,
  417.                 'extra_plugins' => $extra_plugins,
  418.                 'remove_plugins' => $remove_plugins,
  419.                 'admin_url' => $admin_url,
  420.             );
  421.  
  422.             wp_localize_script( 'itsg_gf_ckeditor', 'itsg_gf_ckeditor_settings', $settings_array );
  423.  
  424.             // Enqueued script with localized data.
  425.             wp_enqueue_script( 'itsg_gf_ckeditor' );
  426.  
  427.         } // END localize_scripts
  428.  
  429.     /**
  430.      * END: patch to allow JS and CSS to load when loading forms through wp-ajax requests
  431.      *
  432.      */
  433.  
  434.         function entries_column_filter( $value, $form_id, $field_id, $entry, $query_string ) {
  435.             $form = GFAPI::get_form( $form_id );
  436.             foreach ( $form['fields'] as $field ) {
  437.                 if ( $field->id == $field_id && $this->is_wysiwyg_ckeditor( $field ) ) {
  438.                     return  substr( wp_kses_post( htmlspecialchars_decode( strip_tags( $value, '<strong><a><u><i>' ) ) ), 0, 140);
  439.                 }
  440.             }
  441.             return $value;
  442.         }
  443.  
  444.         function itsg_gf_wysiwyg_ckeditor_upload() {
  445.             $CKEditorFuncNum = isset( $_GET['CKEditorFuncNum'] ) ? $_GET['CKEditorFuncNum'] : null;
  446.             if ( is_null( $CKEditorFuncNum ) ) {
  447.                 die( "<script>
  448.                 window.parent.CKEDITOR.tools.callFunction(
  449.                 '',
  450.                 '',
  451.                 'ERROR: Failed to pass CKEditorFuncNum');
  452.                 </script>" );
  453.             }
  454.  
  455.             $form_id = isset( $_GET['form_id'] ) ? $_GET['form_id'] : null;
  456.  
  457.             if ( is_null( $form_id ) ) {
  458.                 die( "<script>
  459.                 window.parent.CKEDITOR.tools.callFunction('',
  460.                 '',
  461.                 'ERROR: Failed to get form_id');
  462.                 </script>" );
  463.             }
  464.  
  465.             // get target path - also responsible for creating directories if path doesnt exist
  466.             $target = GFFormsModel::get_file_upload_path( $form_id, null );
  467.             $target_path = pathinfo( $target['path'] );
  468.             $target_url = pathinfo( $target['url'] );
  469.  
  470.             // get Ajax Upload options
  471.             $ckeditor_settings = self::get_options();
  472.  
  473.             // calculate file size in KB from MB
  474.             $file_size = $ckeditor_settings['setting_upload_filesize'];
  475.             $file_size_kb = $file_size * 1024 * 1024;
  476.  
  477.             // push options to upload handler
  478.             $options = array(
  479.                 'paramName' => 'upload',
  480.                 'param_name' => 'upload',
  481.                 'CKEditorFuncNum' => $CKEditorFuncNum,
  482.                 'upload_dir' => $target_path['dirname'].'/',
  483.                 'upload_url' => $target_url['dirname'].'/',
  484.                 'image_versions' => array(
  485.                     '' => array(
  486.                     'max_width' => empty( $ckeditor_settings['setting_upload_filewidth'] ) ? null : $ckeditor_settings['setting_upload_filewidth'],
  487.                     'max_height' => empty( $ckeditor_settings['setting_upload_fileheight'] ) ? null : $ckeditor_settings['setting_upload_fileheight'],
  488.                     'jpeg_quality' => empty( $ckeditor_settings['setting_upload_filejpegquality'] ) ? null : $ckeditor_settings['setting_upload_filejpegquality']
  489.                     )
  490.                 ),
  491.                 'accept_file_types' => empty( $ckeditor_settings['setting_upload_filetype'] ) ? '/(\.|\/)(png|tif|jpeg|jpg|gif)$/i' : '/(\.|\/)('.$ckeditor_settings['setting_upload_filetype'].')$/i',
  492.                 'max_file_size' => empty( $file_size_kb ) ? null : $file_size_kb
  493.             );
  494.  
  495.             if ( class_exists( 'ITSG_GFCKEDITOR_UploadHandler' ) ) {
  496.                 // initialise the upload handler and pass the options
  497.                 $upload_handler = new ITSG_GFCKEDITOR_UploadHandler( $options );
  498.             }
  499.  
  500.             // terminate the function
  501.             die();
  502.         } // END itsg_gf_wysiwyg_ckeditor_upload
  503.  
  504.         /*
  505.          *   Changes the upload path for Gravity Form uploads.
  506.          *   Changes made by this function will be seen when the Gravity Forms function  GFFormsModel::get_file_upload_path() is called.
  507.          *   The default upload path applied by this function matches the default for Gravity forms:
  508.          *   /gravity_forms/{form_id}-{hashed_form_id}/{month}/{year}/
  509.          */
  510.         function change_upload_path( $path_info, $form_id ) {
  511.             $ckeditor_settings = self::get_options();
  512.             $file_dir = $ckeditor_settings['setting_upload_filedir'];
  513.  
  514.             if ( 0 != strlen( $file_dir ) ) {
  515.                 // Generate the yearly and monthly dirs
  516.                 $time            = current_time( 'mysql' );
  517.                 $y               = substr( $time, 0, 4 );
  518.                 $m               = substr( $time, 5, 2 );
  519.  
  520.                 // removing leading forward slash, if present
  521.                 if( '/' == $file_dir[0] ) {
  522.                     $file_dir = ltrim( $file_dir, '/' );
  523.                 }
  524.  
  525.                 // remove leading forward slash, if present
  526.                 if( '/' == substr( $file_dir, -1 ) ) {
  527.                     $file_dir = rtrim( $file_dir, '/' );
  528.                 }
  529.  
  530.                 // if {form_id} keyword used, replace with current form id
  531.                 if ( false !== strpos( $file_dir, '{form_id}' ) ) {
  532.                     $file_dir = str_replace( '{form_id}', $form_id, $file_dir );
  533.                 }
  534.  
  535.                 // if {hashed_form_id} keyword used, replace with hashed current form id
  536.                 if ( false !== strpos( $file_dir, '{hashed_form_id}' ) ) {
  537.                     $file_dir = str_replace( '{hashed_form_id}', wp_hash( $form_id), $file_dir );
  538.                 }
  539.  
  540.                 // if {year} keyword used, replace with current year
  541.                 if ( false !== strpos($file_dir,'{year}') ) {
  542.                     $file_dir = str_replace( '{year}', $y, $file_dir );
  543.                 }
  544.  
  545.                 // if {month} keyword used, replace with current month
  546.                 if ( false !== strpos( $file_dir, '{month}' ) ) {
  547.                     $file_dir = str_replace( '{month}', $m, $file_dir );
  548.                 }
  549.  
  550.                 // if {user_id} keyword used, replace with current user id
  551.                 if ( false !== strpos( $file_dir, '{user_id}' ) ) {
  552.                     if ( isset( $_POST['entry_user_id'] ) ) {
  553.                         $entry_user_id = $_POST['entry_user_id'];
  554.                         $file_dir = str_replace( '{user_id}', $entry_user_id, $file_dir );
  555.                     } else {
  556.                         $user_id = get_current_user_id() ? get_current_user_id() : '0';
  557.                         $file_dir = str_replace( '{user_id}', $user_id, $file_dir );
  558.                     }
  559.                 }
  560.  
  561.                 // if {hashed_user_id} keyword used, replace with hashed current user id
  562.                 if ( false !== strpos( $file_dir, '{hashed_user_id}' ) ) {
  563.                     if ( isset( $_POST['entry_user_id'] ) ) {
  564.                         $entry_user_id = $_POST['entry_user_id'];
  565.                         $hashed_entry_user_id = wp_hash( $entry_user_id );
  566.                         $file_dir = str_replace( '{hashed_user_id}', $hashed_entry_user_id, $file_dir );
  567.                     } else {
  568.                         $hashed_user_id = wp_hash( is_user_logged_in() ? get_current_user_id() : '0' );
  569.                         $file_dir = str_replace( '{hashed_user_id}', $hashed_user_id, $file_dir );
  570.                     }
  571.                 }
  572.  
  573.                 $upload_dir = wp_upload_dir(); // get WordPress upload directory information - returns an array
  574.  
  575.                 $path_info['path']  = $upload_dir['basedir'].'/'.$file_dir.'/';  // set the upload path
  576.                 $path_info['url']   = $upload_dir['baseurl'].'/'.$file_dir.'/';  // set the upload URL
  577.             }
  578.             return $path_info;
  579.         } // END change_upload_path
  580.  
  581.         /*
  582.          *   Converts php.ini memory limit string to bytes.
  583.          *   For example, 2MB would convert to 2097152
  584.          */
  585.         public static function return_bytes( $val ) {
  586.             $val = trim( $val );
  587.             $last = strtolower( $val[ strlen( $val ) -1 ] );
  588.  
  589.             switch( $last ) {
  590.                 case 'g':
  591.                     $val *= 1024;
  592.                 case 'm':
  593.                     $val *= 1024;
  594.                 case 'k':
  595.                     $val *= 1024;
  596.             }
  597.             return $val;
  598.         } // END return_bytes
  599.  
  600.         /*
  601.          *   Determines the maximum upload file size.
  602.          *   Retrieves three values from php.ini and returns the smallest.
  603.          */
  604.         public static function max_file_upload_in_bytes() {
  605.             //select maximum upload size
  606.             $max_upload = self::return_bytes( ini_get( 'upload_max_filesize' ) );
  607.             //select post limit
  608.             $max_post = self::return_bytes( ini_get( 'post_max_size' ) );
  609.             //select memory limit
  610.             $memory_limit = self::return_bytes( ini_get( 'memory_limit' ) );
  611.             // return the smallest of them, this defines the real limit
  612.             return min( $max_upload, $max_post, $memory_limit );
  613.         } // END max_file_upload_in_bytes
  614.  
  615.         /*
  616.          * Add 'Settings' link to plugin in WordPress installed plugins page
  617.          */
  618.         function plugin_action_links( $links ) {
  619.  
  620.             $action_links = array(
  621.                 'settings' => '<a href="' . admin_url( 'admin.php?page=gf_settings&subview=WYSIWYG+CKEditor' ) . '" >' . __( 'Settings', 'gravity-forms-wysiwyg-ckeditor' ) . '</a>',
  622.             );
  623.  
  624.             return array_merge( $action_links, $links );
  625.         } // END plugin_action_links
  626.  
  627.         /*
  628.          *   Handles the plugin options.
  629.          *   Default values are stored in an array.
  630.          */
  631.         public static function get_options() {
  632.             $defaults = array(
  633.                 'enable_in_form_editor' => 'on',
  634.                 'enable_bold' => 'on',
  635.                 'enable_italic' => 'on',
  636.                 'enable_underline' => 'on',
  637.                 'enable_pastetext' => 'on',
  638.                 'enable_pastefromword' => 'on',
  639.                 'enable_numberedlist' => 'on',
  640.                 'enable_bulletedlist' => 'on',
  641.                 'enable_outdent' => 'on',
  642.                 'enable_indent' => 'on',
  643.                 'enable_link' => 'on',
  644.                 'enable_unlink' => 'on',
  645.                 'enable_format' => 'on',
  646.                 'enable_font' => 'on',
  647.                 'enable_fontsize' => 'on',
  648.                 'setting_upload_filesize' => '2',
  649.                 'setting_upload_filetype' => 'png|tif|jpeg|jpg|gif',
  650.                 'setting_upload_filedir' => '/gravity_forms/{form_id}-{hashed_form_id}/{month}/{year}/',
  651.                 'setting_upload_filejpegquality' => '75',
  652.                 'setting_upload_filewidth' => '786',
  653.                 'setting_upload_fileheight' => '786',
  654.                 'enable_upload_image' => 'off'
  655.             );
  656.             $options = wp_parse_args( get_option( 'ITSG_gf_wysiwyg_ckeditor_settings' ), $defaults );
  657.             return $options;
  658.         } // END get_options
  659.  
  660.         /*
  661.          * Customises 'Paragraph Text' field output to
  662.          *  1. apply 'gform_wysiwyg_ckeditor' class to ckeditor fields in the wp-admin
  663.          *  2. include character limit details and CSS class for admin area
  664.          */
  665.         public function ckeditor_field_content( $content, $field, $value, $lead_id, $form_id ){
  666.             if ( $this->is_wysiwyg_ckeditor( $field ) ) {
  667.                 if ( is_admin() ){
  668.                     $content = str_replace( "class='", "class='gform_wysiwyg_ckeditor ", $content );
  669.                 } else {
  670.                     $label = rgar( $field, 'label' );
  671.                     $limit = ( '' == rgar( $field, 'maxLength' ) ? 'unlimited' : rgar( $field, 'maxLength' ) );
  672.                     $content = str_replace( "<textarea ", "<textarea data-maxlen='".$limit."' ", $content);
  673.                 }
  674.             }
  675.             return $content;
  676.         } // END ckeditor_field_content
  677.  
  678.         /*
  679.          * Customises character limit count down for NON-CKEditor fields to match what CKEditor provides
  680.          * - note that these fields DO count spaces, where as CKEDitor does NOT count spaces.
  681.          * Output: Characters: [number of characters in field]/[limit on field]
  682.          */
  683.         public function ckeditor_counter_script_js( $script, $form_id, $input_id, $max_length ){
  684.             $field_id_number = substr( $input_id, strrpos($input_id, '_') + 1);
  685.             $form = GFFormsModel::get_form_meta( $form_id );
  686.             $field = GFFormsModel::get_field( $form, $field_id_number );
  687.             if ( $this->is_wysiwyg_ckeditor( $field ) ) {
  688.                 return "";
  689.             } else {
  690.                 $script = "jQuery('#{$input_id}').textareaCount(" .
  691.                             "    {" .
  692.                             "    'maxCharacterSize': {$max_length}," .
  693.                             "    'originalStyle': 'ginput_counter'," .
  694.                             "    'displayFormat' : '" .  esc_js( __( 'Characters', 'gravity-forms-wysiwyg-ckeditor' ) ) . ": #input/$max_length'" .
  695.                             "    });";
  696.                 return $script;
  697.             }
  698.         } // END ckeditor_counter_script_js
  699.  
  700.         /*
  701.          * Applies CSS class to 'Paragraph text' fields when CKEditor is enabled
  702.          */
  703.         public function ckeditor_field_css_class( $classes, $field, $form ) {
  704.             if ( $this->is_wysiwyg_ckeditor( $field ) ) {
  705.                  $classes .= ' gform_wysiwyg_ckeditor';
  706.             }
  707.             return $classes;
  708.         } // END ckeditor_field_css_class
  709.  
  710.         /*
  711.          * Applies 'Enable WYSIWYG CKEditor' option to 'Paragraph Text' field
  712.          */
  713.         public function ckeditor_field_settings($position, $form_id) {
  714.             if ( 25 == $position ) {
  715.                 ?>
  716.                 <li class="wysiwyg_field_setting_wysiwyg_ckeditor field_setting" style="display:list-item;">
  717.                     <input type="checkbox" id="field_enable_wysiwyg_ckeditor"/>
  718.                     <label for="field_enable_wysiwyg_ckeditor" class="inline">
  719.                         <?php _e( 'Enable WYSIWYG (CKEditor)', 'gravity-forms-wysiwyg-ckeditor' ); ?>
  720.                     </label>
  721.                     <?php gform_tooltip( 'form_field_enable_wysiwyg_ckeditor' ) ?><br/>
  722.                 </li>
  723.             <?php
  724.             }
  725.         } // END ckeditor_field_settings
  726.  
  727.         /*
  728.          * Tooltip for field in form editor
  729.          */
  730.         public function ckeditor_field_tooltips( $tooltips ){
  731.             $tooltips['form_field_enable_wysiwyg_ckeditor'] = '<h6>'.__( 'Enable WYSIWYG', 'gravity-forms-wysiwyg-ckeditor' ).'</h6>'.__( 'Check this box to turn this field into a WYSIWYG editor, using CKEditor.', 'gravity-forms-wysiwyg-ckeditor' );
  732.             return $tooltips;
  733.         } // END ckeditor_field_tooltips
  734.  
  735.         /*
  736.          * Checks if field is CKEditor enabled
  737.          */
  738.         public function is_wysiwyg_ckeditor( $field ) {
  739.             $field_type = self::get_type( $field );
  740.             if ( 'post_content' == $field_type ||
  741.                 'textarea' == $field_type ||
  742.                 ( 'post_custom_field' == $field_type && 'textarea' == $field['inputType'] ) ) {
  743.                 if ( 'true' == $field->enable_wysiwyg_ckeditor && true !== $field->useRichTextEditor ) {
  744.                     return true;
  745.                 }
  746.             }
  747.             return false;
  748.         } // END is_wysiwyg_ckeditor
  749.  
  750.         /*
  751.          * Get field type
  752.          */
  753.         private static function get_type( $field ) {
  754.             $type = '';
  755.             if ( isset( $field['type'] ) ) {
  756.                 $type = $field['type'];
  757.                 if ( 'post_custom_field' == $type ) {
  758.                     if ( isset( $field['inputType'] ) ) {
  759.                         $type = $field['inputType'];
  760.                     }
  761.                 }
  762.                 return $type;
  763.             }
  764.         } // END get_type
  765.  
  766.         /*
  767.          * Modifies the value before saved to the database - removes line spaces
  768.          */
  769.         public function save_field_value( $value, $lead, $field, $form ) {
  770.             if ( $this->is_wysiwyg_ckeditor( $field ) ) {
  771.                 $value = rgpost( "input_{$field['id']}" );
  772.                 $value = preg_replace( "/\r|\n/", "", $value );
  773.             }
  774.             return $value;
  775.         } // END save_field_value
  776.  
  777.         /*
  778.          * Warning message if Gravity Forms is installed and enabled
  779.          */
  780.         public static function admin_warnings() {
  781.             if ( !self::is_gravityforms_installed() ) {
  782.                 printf(
  783.                     '<div class="error"><h3>%s</h3><p>%s</p><p>%s</p></div>',
  784.                         __( 'Warning', 'gravity-forms-wysiwyg-ckeditor' ),
  785.                         sprintf ( __( 'The plugin %s requires Gravity Forms to be installed.', 'gravity-forms-wysiwyg-ckeditor' ), '<strong>'.self::$name.'</strong>' ),
  786.                         sprintf ( esc_html__( 'Please %sdownload the latest version of Gravity Forms%s and try again.', 'gravity-forms-wysiwyg-ckeditor' ), '<a href="https://www.e-junkie.com/ecom/gb.php?cl=54585&c=ib&aff=299380" target="_blank" >', '</a>' )
  787.                 );
  788.             }
  789.         } // END admin_warnings
  790.  
  791.         /*
  792.          * Warning message if Gravity Forms is installed and enabled
  793.          */
  794.         public static function admin_warnings_minimum_php_version() {
  795.                 printf(
  796.                     '<div class="error"><h3>%s</h3><p>%s</p><p>%s</p></div>',
  797.                         __( 'Warning', 'gravity-forms-wysiwyg-ckeditor' ),
  798.                         sprintf( __( 'The <strong>image upload</strong> feature requires a minimum of PHP version 5.4.', 'gravity-forms-wysiwyg-ckeditor' ) ),
  799.                         sprintf( __( 'You are running an PHP version %s. Contact your web hosting provider to update.', 'gravity-forms-wysiwyg-ckeditor' ), phpversion() )
  800.                 );
  801.         } // END admin_warnings_minimum_php_version
  802.  
  803.         /*
  804.          * Check if GF is installed
  805.          */
  806.         private static function is_gravityforms_installed() {
  807.             if ( !function_exists( 'is_plugin_active' ) || !function_exists( 'is_plugin_active_for_network' ) ) {
  808.                 require_once( ABSPATH . '/wp-admin/includes/plugin.php' );
  809.             }
  810.             if (is_multisite()) {
  811.                 return ( is_plugin_active_for_network('gravityforms/gravityforms.php' ) || is_plugin_active( 'gravityforms/gravityforms.php' ) );
  812.             } else {
  813.                 return is_plugin_active( 'gravityforms/gravityforms.php' );
  814.             }
  815.         } // END is_gravityforms_installed
  816.  
  817.         /*
  818.          * Check if PHP version is at least 5.4
  819.          */
  820.         private static function is_minimum_php_version() {
  821.             return version_compare( phpversion(), '5.4', '>=' );
  822.         } // END is_minimum_php_version
  823.  
  824.         /*
  825.          * Check if Gravity Forms - Data Persistence Reloaded is installed
  826.          */
  827.         private function is_dpr_installed() {
  828.             return function_exists( 'ri_gfdp_ajax' );
  829.         } // END is_dpr_installed
  830.  
  831.         /*
  832.          * Get plugin url
  833.          */
  834.          private function get_base_url(){
  835.             return plugins_url( null, __FILE__ );
  836.         } // END get_base_url
  837.  
  838.         /*
  839.          * decodes the value before being displayed in the front end confirmation - for gravity wiz better pre-confirmation
  840.          */
  841.         public function decode_wysiwyg_frontend_confirmation( $value, $merge_tag, $modifier, $field, $raw_value ) {
  842.             if ( $this->is_wysiwyg_ckeditor( $field ) ) {
  843.                 return wp_kses_post( $raw_value );
  844.             }
  845.             return $value;
  846.         } // END decode_wysiwyg_frontend_confirmation
  847.  
  848.         /*
  849.          * decodes the value before being displayed in the entry editor and Gravity PDF 3.x
  850.          */
  851.         public function decode_wysiwyg_backend_and_gravitypdf( $value, $field, $lead, $form ) {
  852.             if ( $this->is_wysiwyg_ckeditor( $field ) ) {
  853.                 return  wp_kses_post( htmlspecialchars_decode( $value ) );
  854.             }
  855.             return $value;
  856.         } // END decode_wysiwyg_backend_and_gravitypdf
  857.  
  858.     }
  859.     $ITSG_GF_WYSIWYG_CKEditor = new ITSG_GF_WYSIWYG_CKEditor();
  860. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement