| 1 |
<?php |
| 2 |
use Automattic\WooCommerce\EmailEditorVendor\Sabberworm\CSS\Property\Selector; |
| 3 |
|
| 4 |
/** |
| 5 |
* class-groups-uie.php |
| 6 |
* |
| 7 |
* Copyright (c) "kento" Karim Rahimpur www.itthinx.com |
| 8 |
* |
| 9 |
* This code is released under the GNU General Public License. |
| 10 |
* See COPYRIGHT.txt and LICENSE.txt. |
| 11 |
* |
| 12 |
* This code is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU General Public License for more details. |
| 16 |
* |
| 17 |
* This header and all notices must be kept intact. |
| 18 |
* |
| 19 |
* @author Karim Rahimpur |
| 20 |
* @package groups |
| 21 |
* @since groups 1.3.14 |
| 22 |
*/ |
| 23 |
|
| 24 |
if ( !defined( 'ABSPATH' ) ) { |
| 25 |
exit; |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* User Interface Extensions. |
| 30 |
* |
| 31 |
* This class may yet be subject to changes in method signatures. External |
| 32 |
* dependency is not advised until the private access restriction is removed. |
| 33 |
* |
| 34 |
* @access private |
| 35 |
*/ |
| 36 |
class Groups_UIE { |
| 37 |
|
| 38 |
/** |
| 39 |
* Extension used for select. |
| 40 |
* |
| 41 |
* @var string |
| 42 |
*/ |
| 43 |
private static $select = 'tom-select'; |
| 44 |
|
| 45 |
/** |
| 46 |
* Setup. |
| 47 |
*/ |
| 48 |
public static function init() { |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Extension chooser - determines what UI extension is used for an element. |
| 53 |
* |
| 54 |
* @param string $element choices: select |
| 55 |
* @param string $extension choices: tom-select, selectize |
| 56 |
*/ |
| 57 |
public static function set_extension( $element, $extension ) { |
| 58 |
switch ( $element ) { |
| 59 |
case 'select' : |
| 60 |
self::$select = sanitize_key( $extension ); |
| 61 |
break; |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Tell which select is being used. |
| 67 |
* |
| 68 |
* @since 4.0.0 |
| 69 |
* |
| 70 |
* @return mixed |
| 71 |
*/ |
| 72 |
public static function which_select() { |
| 73 |
/** |
| 74 |
* Allow to filter the select to use. |
| 75 |
* |
| 76 |
* @since 4.0.0 |
| 77 |
* |
| 78 |
* @param string $select which select |
| 79 |
* |
| 80 |
* @return string |
| 81 |
*/ |
| 82 |
return sanitize_key( apply_filters( 'groups_uie_which_select', self::$select ) ?? '' ); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Enqueue scripts and styles. |
| 87 |
*/ |
| 88 |
public static function enqueue( $element = null ) { |
| 89 |
global $groups_version; |
| 90 |
switch ( $element ) { |
| 91 |
case 'select' : |
| 92 |
switch ( self::which_select() ) { |
| 93 |
case 'selectize': |
| 94 |
if ( !wp_script_is( 'groups-selectize' ) ) { |
| 95 |
wp_enqueue_script( 'groups-selectize', GROUPS_PLUGIN_URL . 'js/selectize/selectize.min.js', array( 'jquery' ), $groups_version, false ); |
| 96 |
} |
| 97 |
if ( !wp_style_is( 'groups-selectize' ) ) { |
| 98 |
wp_enqueue_style( 'groups-selectize', GROUPS_PLUGIN_URL . 'css/selectize/selectize.css', array(), $groups_version ); |
| 99 |
} |
| 100 |
if ( !wp_style_is( 'groups-uie' ) ) { |
| 101 |
wp_enqueue_style( 'groups-uie', GROUPS_PLUGIN_URL . 'css/groups-uie.css', array(), $groups_version ); |
| 102 |
} |
| 103 |
break; |
| 104 |
default: |
| 105 |
if ( !wp_script_is( 'groups-tom-select' ) ) { |
| 106 |
// Tom Select does not require jQuery but we add it as a dependency for remnant code that assumes its presence. |
| 107 |
wp_enqueue_script( 'groups-tom-select', GROUPS_PLUGIN_URL . 'js/tom-select/tom-select.complete.min.js', array( 'jquery' ), $groups_version, false ); |
| 108 |
} |
| 109 |
if ( !wp_style_is( 'groups-tom-select' ) ) { |
| 110 |
wp_enqueue_style( 'groups-tom-select', GROUPS_PLUGIN_URL . 'css/tom-select/tom-select.groups.css', array(), $groups_version ); |
| 111 |
} |
| 112 |
if ( !wp_style_is( 'groups-uie' ) ) { |
| 113 |
wp_enqueue_style( 'groups-uie', GROUPS_PLUGIN_URL . 'css/groups-uie.css', array(), $groups_version ); |
| 114 |
} |
| 115 |
break; |
| 116 |
} |
| 117 |
break; |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Render select script and style. |
| 123 |
* |
| 124 |
* @param string $selector identifying the select, default: select.groups-uie |
| 125 |
* @param boolean $script render the script, default: true |
| 126 |
* @param boolean $on_document_ready whether to trigger on document ready, default: true |
| 127 |
* @param boolean $create allow to create items, default: false (only with selectize) |
| 128 |
* |
| 129 |
* @return string HTML |
| 130 |
*/ |
| 131 |
public static function render_select( $selector = 'select.groups-uie', $script = true, $on_document_ready = true, $create = false ) { |
| 132 |
$output = ''; |
| 133 |
if ( $script ) { |
| 134 |
|
| 135 |
$call_output = ''; |
| 136 |
switch ( self::$select ) { |
| 137 |
case 'selectize': |
| 138 |
$call_output .= 'if ( typeof jQuery !== "undefined" && typeof jQuery.fn.selectize === "function" ) {'; |
| 139 |
$call_output .= sprintf( |
| 140 |
'jQuery("%s").selectize({%splugins: ["remove_button"],wrapperClass:"groups-selectize"});', |
| 141 |
$selector, |
| 142 |
$create ? 'create:true,' : '' |
| 143 |
); |
| 144 |
$call_output .= '}'; |
| 145 |
break; |
| 146 |
default: |
| 147 |
/** |
| 148 |
* Allows to determine the maximum number of options displayed in the dropdown. |
| 149 |
* |
| 150 |
* Provide a number > 0, or null for no limit. |
| 151 |
* Defaults to null for unlimited options displayed. |
| 152 |
* This limits the number of options that are displayed, but not the total available options. |
| 153 |
* |
| 154 |
* @since 4.1.0 |
| 155 |
* |
| 156 |
* @param int|null $max_options |
| 157 |
*/ |
| 158 |
$max_options = apply_filters( 'groups_uie_render_select_display_limit', null ); |
| 159 |
if ( is_numeric( $max_options ) ) { |
| 160 |
$max_options = max( 1, intval( $max_options ) ); |
| 161 |
} else { |
| 162 |
$max_options = null; |
| 163 |
} |
| 164 |
$call_output .= 'if ( typeof jQuery !== "undefined" && typeof tomSelect === "function" ) {'; |
| 165 |
$call_output .= sprintf( |
| 166 |
'tomSelect("%s",{%splugins: ["remove_button","clear_button"],maxOptions:%s,wrapperClass:"groups-ts-wrapper",controlClass:"groups-ts-control",dropdownClass:"groups-ts-dropdown",dropdownContentClass:"groups-ts-dropdown-content"});', |
| 167 |
$selector, |
| 168 |
$create ? 'create:true,' : '', |
| 169 |
$max_options === null ? 'null' : $max_options |
| 170 |
); |
| 171 |
$call_output .= '}'; |
| 172 |
break; |
| 173 |
} |
| 174 |
|
| 175 |
// Our selectize options will be hidden unless the block editor's components panel allows to overflow. |
| 176 |
$output .= '<style type="text/css">'; |
| 177 |
$output .= '.components-panel { overflow: visible !important; }'; |
| 178 |
$output .= '</style>'; |
| 179 |
// Act immediately if DOMContentLoaded was already dispatched, otherwise defer to handler. |
| 180 |
$output .= '<script type="text/javascript">'; |
| 181 |
$output .= 'if ( document.readyState === "complete" || document.readyState === "interactive" ) {'; |
| 182 |
$output .= $call_output; |
| 183 |
$output .= '}'; |
| 184 |
if ( $on_document_ready ) { |
| 185 |
$output .= ' else {'; |
| 186 |
$output .= 'document.addEventListener( "DOMContentLoaded", function() {'; |
| 187 |
$output .= $call_output; |
| 188 |
$output .= '} );'; // document.... |
| 189 |
$output .= '}'; // else |
| 190 |
} |
| 191 |
$output .= '</script>'; |
| 192 |
} |
| 193 |
return $output; |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Render the title attribute for elements matching the selector. |
| 198 |
* |
| 199 |
* @param string $selector |
| 200 |
* |
| 201 |
* @return string |
| 202 |
*/ |
| 203 |
public static function render_add_titles( $selector ) { |
| 204 |
$output = '<script type="text/javascript">'; |
| 205 |
$output .= 'if ( typeof jQuery !== "undefined" ) {'; |
| 206 |
$output .= sprintf( 'jQuery("%s").each(', $selector ); |
| 207 |
$output .= 'function(){'; |
| 208 |
$output .= 'var title = jQuery(this).html().replace( /(<\/[^>]+>)/igm , "$1 ");'; |
| 209 |
$output .= 'jQuery(this).attr("title", this.innerText || jQuery(jQuery.parseHTML(title)).text().replace(/\s+/igm, " ") );'; |
| 210 |
$output .= '}'; |
| 211 |
$output .= ');'; |
| 212 |
$output .= '}'; |
| 213 |
$output .= '</script>'; |
| 214 |
return $output; |
| 215 |
} |
| 216 |
} |
| 217 |
Groups_UIE::init(); |
| 218 |
|