| 1 |
<?php |
| 2 |
/** |
| 3 |
* class-groups-uie.php |
| 4 |
* |
| 5 |
* Copyright (c) "kento" Karim Rahimpur www.itthinx.com |
| 6 |
* |
| 7 |
* This code is released under the GNU General Public License. |
| 8 |
* See COPYRIGHT.txt and LICENSE.txt. |
| 9 |
* |
| 10 |
* This code is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* This header and all notices must be kept intact. |
| 16 |
* |
| 17 |
* @author Karim Rahimpur |
| 18 |
* @package groups |
| 19 |
* @since groups 1.3.14 |
| 20 |
*/ |
| 21 |
|
| 22 |
if ( !defined( 'ABSPATH' ) ) { |
| 23 |
exit; |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* User Interface Extensions. |
| 28 |
* |
| 29 |
* This class may yet be subject to changes in method signatures. External |
| 30 |
* dependency is not advised until the private access restriction is removed. |
| 31 |
* |
| 32 |
* @access private |
| 33 |
*/ |
| 34 |
class Groups_UIE { |
| 35 |
|
| 36 |
/** |
| 37 |
* Extension used for select |
| 38 |
* @var string |
| 39 |
*/ |
| 40 |
private static $select = 'selectize'; |
| 41 |
|
| 42 |
/** |
| 43 |
* Setup. |
| 44 |
*/ |
| 45 |
public static function init() { |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Extension chooser - determines what UI extension is used for an element. |
| 50 |
* |
| 51 |
* @param string $element choices: select |
| 52 |
* @param string $extension choices: chosen, selectize |
| 53 |
*/ |
| 54 |
public static function set_extension( $element, $extension ) { |
| 55 |
switch( $element ) { |
| 56 |
case 'select' : |
| 57 |
self::$select = $extension; |
| 58 |
break; |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Enqueue scripts and styles. |
| 64 |
*/ |
| 65 |
public static function enqueue( $element = null ) { |
| 66 |
global $groups_version; |
| 67 |
switch( $element ) { |
| 68 |
case 'select' : |
| 69 |
switch ( self::$select ) { |
| 70 |
case 'chosen' : |
| 71 |
if ( !wp_script_is( 'chosen' ) ) { |
| 72 |
wp_enqueue_script( 'chosen', GROUPS_PLUGIN_URL . 'js/chosen/chosen.jquery.min.js', array( 'jquery' ), $groups_version, false ); |
| 73 |
} |
| 74 |
if ( !wp_style_is( 'chosen' ) ) { |
| 75 |
wp_enqueue_style( 'chosen', GROUPS_PLUGIN_URL . 'css/chosen/chosen.min.css', array(), $groups_version ); |
| 76 |
} |
| 77 |
break; |
| 78 |
case 'selectize' : |
| 79 |
if ( !wp_script_is( 'selectize' ) ) { |
| 80 |
wp_enqueue_script( 'selectize', GROUPS_PLUGIN_URL . 'js/selectize/selectize.min.js', array( 'jquery' ), $groups_version, false ); |
| 81 |
} |
| 82 |
if ( !wp_style_is( 'selectize' ) ) { |
| 83 |
wp_enqueue_style( 'selectize', GROUPS_PLUGIN_URL . 'css/selectize/selectize.bootstrap2.css', array(), $groups_version ); |
| 84 |
} |
| 85 |
break; |
| 86 |
} |
| 87 |
break; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Render select script and style. |
| 93 |
* @param string $selector identifying the select, default: select.groups-uie |
| 94 |
* @param boolean $script render the script, default: true |
| 95 |
* @param boolean $on_document_ready whether to trigger on document ready, default: true |
| 96 |
* @return string HTML |
| 97 |
*/ |
| 98 |
public static function render_select( $selector = 'select.groups-uie', $script = true, $on_document_ready = true ) { |
| 99 |
$output = ''; |
| 100 |
if ( $script ) { |
| 101 |
$output .= '<script type="text/javascript">'; |
| 102 |
$output .= 'if (typeof jQuery !== "undefined"){'; |
| 103 |
if ( $on_document_ready ) { |
| 104 |
$output .= 'jQuery("document").ready(function(){'; |
| 105 |
} |
| 106 |
switch( self::$select ) { |
| 107 |
case 'chosen' : |
| 108 |
$output .= sprintf( 'jQuery("%s").chosen({width:"100%%",search_contains:true});', $selector ); |
| 109 |
break; |
| 110 |
case 'selectize' : |
| 111 |
$output .= sprintf( 'jQuery("%s").selectize({plugins: ["remove_button"]});', $selector ); |
| 112 |
break; |
| 113 |
} |
| 114 |
if ( $on_document_ready ) { |
| 115 |
$output .= '});'; |
| 116 |
} |
| 117 |
$output .= '}'; // typeof jQuery |
| 118 |
$output .= '</script>'; |
| 119 |
} |
| 120 |
return $output; |
| 121 |
} |
| 122 |
|
| 123 |
public static function render_add_titles( $selector ) { |
| 124 |
$output = '<script type="text/javascript">'; |
| 125 |
$output .= 'if ( typeof jQuery !== "undefined" ) {'; |
| 126 |
$output .= sprintf( 'jQuery("%s").each(', $selector ); |
| 127 |
$output .= 'function(){'; |
| 128 |
$output .= 'var title = jQuery(this).html().replace( /(<\/[^>]+>)/igm , "$1 ");'; |
| 129 |
$output .= 'jQuery(this).attr("title", this.innerText || jQuery(jQuery.parseHTML(title)).text().replace(/\s+/igm, " ") );'; |
| 130 |
$output .= '}'; |
| 131 |
$output .= ');'; |
| 132 |
$output .= '}'; |
| 133 |
$output .= '</script>'; |
| 134 |
return $output; |
| 135 |
} |
| 136 |
} |
| 137 |
Groups_UIE::init(); |
| 138 |
|