| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Carbon_Fields\Field; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * WYSIWYG rich text area field class. | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 7 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 8 |  |  | class Rich_Text_Field extends Textarea_Field { | 
            
                                                                        
                            
            
                                    
            
            
                | 9 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 10 |  |  | 	 * All Rich Text Fields settings references. | 
            
                                                                        
                            
            
                                    
            
            
                | 11 |  |  | 	 * Used to prevent duplicated wp_editor initialization with the same settings. | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  | 	 * | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  | 	 * @var array | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  | 	protected static $settings_references = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  | 	 * WP Editor settings | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  | 	 * | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  | 	 * @link https://developer.wordpress.org/reference/classes/_wp_editors/parse_settings/ | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  | 	 * @var array | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  | 	protected $settings = array( | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  | 		'media_buttons' => true, | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  | 		'tinymce' => array( | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  | 			'resize' => true, | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  | 			'wp_autoresize_on' => true, | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  | 		), | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  | 	); | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  | 	 * MD5 Hash of the instance settings | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  | 	 * | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  | 	 * @var string | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  | 	protected $settings_hash = null; | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  | 	 * WP Editor settings reference | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  | 	 * | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  | 	 * @var string | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  | 	protected $settings_reference; | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  | 	* Set the editor settings | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  | 	* | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  | 	* @param  array $settings | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  | 	* @return self  $this | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  | 	*/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 	public function set_settings( $settings ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 		$this->settings = array_merge( $this->settings, $settings ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 		return $this; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 55 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  | 	 * Calc the settings hash | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  | 	 * | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  | 	 * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 	protected function calc_settings_hash() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 		return md5( json_encode( $this->settings ) ); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 64 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  | 	 * Get the editor settings reference | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  | 	 * | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  | 	 * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 	protected function get_settings_reference() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 		if ( is_null( $this->settings_hash ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 			$this->settings_hash = $this->calc_settings_hash(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 		return 'carbon_fields_settings_' . $this->settings_hash; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 77 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  | 	 * {@inheritDoc} | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 	public function activate() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 		parent::activate(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 		add_action( 'admin_footer', array( $this, 'editor_init' ) ); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 86 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  | 	 * Display the editor. | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  | 	 * | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  | 	 * Instead of enqueueing all required scripts and stylesheets and setting up TinyMCE, | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  | 	 * wp_editor() automatically enqueues and sets up everything. | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 	public function editor_init() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 		if( in_array( $this->get_settings_reference(), self::$settings_references ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 			return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 		self::$settings_references[] = $this->get_settings_reference(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 		?> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 		<div style="display:none;"> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 			<?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 			add_filter( 'user_can_richedit', '__return_true' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 			wp_editor( '', $this->get_settings_reference(), $this->settings ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 			remove_filter( 'user_can_richedit', '__return_true' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 			?> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 		</div> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 		<?php | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 109 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  | 	 * Display Upload Image Button | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  | 	 * | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 	public function upload_image_button_html() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 		$upload_image_button = '<a href="#" class="button insert-media add_media" data-editor="<%- id %>" title="Add Media"> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 			<span class="wp-media-buttons-icon"></span> Add Media | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 		</a>'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 		echo apply_filters( 'crb_upload_image_button_html', $upload_image_button, $this->base_name ); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 120 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  | 	 * Returns an array that holds the field data, suitable for JSON representation. | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  | 	 * | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  | 	 * @param bool $load  Should the value be loaded from the database or use the value from the current instance. | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  | 	 * @return array | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 	public function to_json( $load ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 		$field_data = parent::to_json( $load ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 		$media_buttons = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  | 		if ( $this->settings['media_buttons'] ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  | 			ob_start(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  | 			remove_action( 'media_buttons', 'media_buttons' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  | 			$this->upload_image_button_html(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 			do_action( 'media_buttons' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  | 			add_action( 'media_buttons', 'media_buttons' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  | 			$media_buttons = apply_filters( 'crb_media_buttons_html', ob_get_clean(), $this->base_name ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  | 		$field_data = array_merge( $field_data, array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  | 			'rich_editing' => user_can_richedit() && ! empty( $this->settings['tinymce'] ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  | 			'media_buttons' => $media_buttons, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  | 			'settings_reference' => $this->get_settings_reference(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  | 		) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  | 		return $field_data; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 152 |  |  | 	} | 
            
                                                        
            
                                    
            
            
                | 153 |  |  | } |