| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
die( '-1' ); |
| 4 |
} |
| 5 |
|
| 6 |
if ( ! class_exists( 'Borderless_IF' ) ) { |
| 7 |
class Borderless_IF { |
| 8 |
var $paths = array(); |
| 9 |
var $svg_file; |
| 10 |
var $json_file; |
| 11 |
var $ip_name = 'unknown'; |
| 12 |
var $svg_config = array(); |
| 13 |
var $json_config = array(); |
| 14 |
static $ip_list = array(); |
| 15 |
|
| 16 |
function __construct() { |
| 17 |
$this->paths = wp_upload_dir(); |
| 18 |
$this->paths['fonts'] = 'borderless_icon_fonts'; |
| 19 |
$this->paths['temp'] = trailingslashit( $this->paths['fonts'] ) . 'borderless_temp'; |
| 20 |
$this->paths['fontdir'] = trailingslashit( $this->paths['basedir'] ) . $this->paths['fonts']; |
| 21 |
$this->paths['tempdir'] = trailingslashit( $this->paths['basedir'] ) . $this->paths['temp']; |
| 22 |
$this->paths['fonturl'] = set_url_scheme( trailingslashit( $this->paths['baseurl'] ) . $this->paths['fonts'] ); |
| 23 |
$this->paths['tempurl'] = trailingslashit( $this->paths['baseurl'] ) . trailingslashit( $this->paths['temp'] ); |
| 24 |
$this->paths['config'] = 'charmap.php'; |
| 25 |
|
| 26 |
add_action( 'wp_ajax_borderless_ajax_add_zipped_font', array( $this, 'add_zipped_font' ) ); |
| 27 |
add_action( 'wp_ajax_borderless_ajax_remove_zipped_font', array( $this, 'remove_zipped_font' ) ); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Loads admin scripts and styles |
| 32 |
*/ |
| 33 |
function admin_scripts() { |
| 34 |
$upload_paths = wp_upload_dir(); |
| 35 |
wp_enqueue_script( 'borderless-admin-media', plugin_dir_url( __FILE__ ) . 'assets/js/icon-manager.min.js', array( 'jquery' ) ); |
| 36 |
wp_enqueue_script( 'media-upload' ); |
| 37 |
wp_enqueue_media(); |
| 38 |
wp_enqueue_style( 'borderless-wpbakery-icon-manager-admin', plugin_dir_url( __FILE__ ) . 'assets/css/icon-manager.css' ); |
| 39 |
$custom_fonts = get_option( 'borderless_icon_fonts' ); |
| 40 |
if ( is_array( $custom_fonts ) ) { |
| 41 |
foreach ( $custom_fonts as $font => $info ) { |
| 42 |
if ( strpos( $info['style'], 'http://' ) !== false ) { |
| 43 |
wp_enqueue_style( 'borderless-' . $font, $info['style'], null, '1.0', 'all' ); |
| 44 |
} else { |
| 45 |
wp_enqueue_style( 'borderless-' . $font, trailingslashit( $upload_paths['baseurl'] . '/borderless_icon_fonts/' ) . $info['style'], null, '1.0', 'all' ); |
| 46 |
} |
| 47 |
} |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Creates an icon selector interface |
| 53 |
*/ |
| 54 |
public function get_icon_manager( $input_name, $icon ) { |
| 55 |
$font_manager = self::get_font_manager( $id ); |
| 56 |
$output = '<div class="my_param_block">'; |
| 57 |
$output .= '<input name="' . $input_name . '" class="textinput ' . $input_name . ' text_field" type="text" value="' . $icon . '"/>'; |
| 58 |
$output .= '</div>'; |
| 59 |
$output .= '<script type="text/javascript"> |
| 60 |
jQuery(document).ready(function(){ |
| 61 |
jQuery(".icon-manager-preview-icon-' . $id . '").html("<i class=\'' . $icon . '\'></i>"); |
| 62 |
jQuery(".icons-list-' . $id . ' li[data-icons=\'' . $icon . '\']").addClass("selected"); |
| 63 |
}); |
| 64 |
jQuery(".icons-list-' . $id . ' li").click(function() { |
| 65 |
jQuery(this).attr("class","selected").siblings().removeAttr("class"); |
| 66 |
var icon = jQuery(this).attr("data-icons"); |
| 67 |
jQuery("input[name=\'' . $input_name . '\']").val(icon); |
| 68 |
jQuery(".icon-manager-preview-icon-' . $id . '").html("<i class=\'"+icon+"\'></i>"); |
| 69 |
}); |
| 70 |
</script>'; |
| 71 |
$output .= $font_manager; |
| 72 |
|
| 73 |
return $output; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Generates the list of icons in the manager |
| 78 |
*/ |
| 79 |
public function get_font_manager( $id ) { |
| 80 |
$fonts = get_option( 'borderless_icon_fonts' ); |
| 81 |
$output = '<div class="icon-manager-preview"><div class="icon-manager-preview-icon preview-icon-' . $id . '"><i class=""></i></div><input class="icon-manager-search-icon" type="text" placeholder="Search for a suitable icon.." /></div>'; |
| 82 |
$output .= '<div id="icon-manager-search-icon-inner">'; |
| 83 |
$output .= '<ul class="icons-list borderless_icon icon-list-' . $id . '">'; |
| 84 |
|
| 85 |
foreach ( $fonts as $font => $info ) { |
| 86 |
$icon_set = array(); |
| 87 |
$icons = array(); |
| 88 |
$upload_dir = wp_upload_dir(); |
| 89 |
$path = trailingslashit( $upload_dir['basedir'] ); |
| 90 |
$file = $path . $info['include'] . '/' . $info['config']; |
| 91 |
|
| 92 |
include( $file ); |
| 93 |
if ( ! empty( $icons ) ) { |
| 94 |
$icon_set = array_merge( $icon_set, $icons ); |
| 95 |
} |
| 96 |
|
| 97 |
$set_name = ucfirst( $font ); |
| 98 |
|
| 99 |
if ( ! empty( $icon_set ) ) { |
| 100 |
$output .= '<p><strong>' . $set_name . '</strong></p>'; |
| 101 |
$output .= '<li title="no-icon" data-icons="none" data-icons-tag="none,blank" style="cursor: pointer;" id="' . $id . '"></li>'; |
| 102 |
foreach ( $icon_set as $icons ) { |
| 103 |
foreach ( $icons as $icon ) { |
| 104 |
$output .= '<li title="' . $icon['class'] . '" data-icons="' . $font . '-' . $icon['class'] . '" data-icons-tag="' . $icon['tags'] . '" id="' . $id . '">'; |
| 105 |
$output .= '<i class="icon-manager-icon ' . $font . '-' . $icon['class'] . '"></i><label class="icon">' . $icon['class'] . '</label></li>'; |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
$output .= '</ul>'; |
| 111 |
$output .= '<script type="text/javascript"> |
| 112 |
jQuery(document).ready(function(){ |
| 113 |
jQuery(".icon-manager-search-icon").keyup(function(){ |
| 114 |
var filter = jQuery(this).val(), count = 0; |
| 115 |
jQuery(".icons-list li").each(function(){ |
| 116 |
if ( jQuery(this).attr("data-icons-tag").search(new RegExp(filter, "i")) < 0 ) { |
| 117 |
jQuery(this).fadeOut(); |
| 118 |
} else { |
| 119 |
jQuery(this).show(); |
| 120 |
count++; |
| 121 |
} |
| 122 |
}); |
| 123 |
}); |
| 124 |
}); |
| 125 |
</script>'; |
| 126 |
$output .= '</div>'; |
| 127 |
|
| 128 |
return $output; |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Generates the Icon Pack Manager page |
| 133 |
*/ |
| 134 |
function icon_pack_manager() { |
| 135 |
?> |
| 136 |
<div class="wrap"> |
| 137 |
<h2> |
| 138 |
<?php esc_html_e( 'Icon Pack Manager', 'borderless' ); ?> |
| 139 |
<a href="#borderless_upload_icon" class="button button-primary borderless-upload-icon" |
| 140 |
data-target="iconfont_upload" |
| 141 |
data-title="<?php echo esc_html__( "Upload or Select Fontello Font Zip", "borderless" ); ?>" |
| 142 |
data-type="application/octet-stream, application/zip" |
| 143 |
data-button="<?php echo esc_html__( "Insert Fonts Zip File", "borderless" ); ?>" |
| 144 |
data-trigger="borderless_insert_zip" |
| 145 |
data-class="media-frame "> |
| 146 |
<?php esc_html_e( 'Add New Icon Pack', 'borderless' ); ?> |
| 147 |
</a> <span class="spinner"></span> |
| 148 |
</h2> |
| 149 |
<div id="msg"></div> |
| 150 |
<?php |
| 151 |
$fonts = get_option( 'borderless_icon_fonts' ); |
| 152 |
if ( is_array( $fonts ) ) : |
| 153 |
?> |
| 154 |
<div class="metabox-holder meta-search"> |
| 155 |
<div class="postbox borderless-postbox-search"> |
| 156 |
<h2 class="hndle ui-sortable-handle"><span>Search for Icons</span></h2> |
| 157 |
<div class="inside"> |
| 158 |
<div class="search-area"> |
| 159 |
<input class="search-icon" type="text" placeholder="<?php esc_attr_e( 'Start typing to search', 'borderless' ); ?>"/> |
| 160 |
<span class="search-count"></span> |
| 161 |
</div> |
| 162 |
</div> |
| 163 |
</div> |
| 164 |
</div> |
| 165 |
<?php self::get_ipm(); ?> |
| 166 |
</div> |
| 167 |
<?php else: ?> |
| 168 |
<div class="error"> |
| 169 |
<p> |
| 170 |
<?php esc_html_e( 'No icon pack uploaded. Please upload at least one icon pack to display here.', 'borderless' ); ?> |
| 171 |
</p> |
| 172 |
</div> |
| 173 |
<?php |
| 174 |
endif; |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Generates Icon Pack Preview and settings |
| 179 |
*/ |
| 180 |
static function get_ipm() { |
| 181 |
$fonts = get_option( 'borderless_icon_fonts' ); |
| 182 |
$n = count( $fonts ); |
| 183 |
foreach ( $fonts as $font => $info ) { |
| 184 |
$icon_set = array(); |
| 185 |
$icons = array(); |
| 186 |
$upload_dir = wp_upload_dir(); |
| 187 |
$path = trailingslashit( $upload_dir['basedir'] ); |
| 188 |
$file = $path . $info['include'] . '/' . $info['config']; |
| 189 |
$output = '<div class="icon_set-' . $font . ' metabox-holder">'; |
| 190 |
$output .= '<div class="postbox borderless-postbox-icon-manager">'; |
| 191 |
|
| 192 |
include( $file ); |
| 193 |
if ( ! empty( $icons ) ) { |
| 194 |
$icon_set = array_merge( $icon_set, $icons ); |
| 195 |
} |
| 196 |
|
| 197 |
if ( ! empty( $icon_set ) ) { |
| 198 |
foreach ( $icon_set as $icons ) { |
| 199 |
$count = count( $icons ); |
| 200 |
} |
| 201 |
|
| 202 |
$output .= '<h3 class="ip_name"><strong>' . esc_html( ucfirst( $font ) ) . '</strong>'; |
| 203 |
$output .= '<span class="fonts-count wp-core-ui wp-ui-notification count-' . esc_attr( $font ) . '">' . esc_html( $count ) . '</span>'; |
| 204 |
|
| 205 |
if ( $n != 1 ) { |
| 206 |
$output .= '<button class="button button-primary button-small borderless_del_icon" data-delete=' . esc_attr( $font ) . ' data-title="' . esc_attr__( 'Remove Pack', 'borderless' ) . '">' . esc_html__( 'Remove Pack', 'borderless' ) . '</button>'; |
| 207 |
} |
| 208 |
$output .= '</h3>'; |
| 209 |
$output .= '<div class="inside"><div class="icon_actions"></div>'; |
| 210 |
$output .= '<div class="icon_search"><ul class="icons-list borderless_icon">'; |
| 211 |
|
| 212 |
foreach ( $icon_set as $icons ) { |
| 213 |
foreach ( $icons as $icon ) { |
| 214 |
$output .= '<li data-icons="' . esc_attr( $icon['class'] ) . '" data-icons-tag="' . esc_attr( $icon['tags'] ) . '">'; |
| 215 |
$output .= '<i class="' . esc_attr( $font ) . '-' . esc_attr( $icon['class'] ) . '"></i><a class="tooltips" href="#"><span>' . esc_html( $icon['class'] ) . '</span></a></li>'; |
| 216 |
} |
| 217 |
} |
| 218 |
$output .= '</ul>'; |
| 219 |
$output .= '</div><!-- .icon_search-->'; |
| 220 |
$output .= '</div><!-- .inside-->'; |
| 221 |
$output .= '</div><!-- .postbox-->'; |
| 222 |
$output .= '</div><!-- .icon_set-' . esc_html( $font ) . ' -->'; |
| 223 |
echo $output; |
| 224 |
} |
| 225 |
} |
| 226 |
$script = '<script type="text/javascript"> |
| 227 |
jQuery(document).ready(function(){ |
| 228 |
jQuery(".search-icon").keyup(function(){ |
| 229 |
jQuery(".fonts-count").hide(); |
| 230 |
var filter = jQuery(this).val(), count = 0; |
| 231 |
jQuery(".icons-list li").each(function(){ |
| 232 |
if (jQuery(this).attr("data-icons-tag").search(new RegExp(filter, "i")) < 0) { |
| 233 |
jQuery(this).fadeOut(); |
| 234 |
} else { |
| 235 |
jQuery(this).show(); |
| 236 |
count++; |
| 237 |
} |
| 238 |
if(count === 0) { |
| 239 |
jQuery(".search-count").html("Can\'t find the icon? <a href=\'#borderless_upload_icon\' class=\'button button-primary borderless_upload_icon\' data-target=\'iconfont_upload\' data-title=\'Upload or Select Fontello Font Zip\' data-type=\'application/octet-stream, application/zip\' data-button=\'Insert Fonts Zip File\' data-trigger=\'borderless_insert_zip\' data-class=\'media-frame\'>Upload New Font Pack</a>"); |
| 240 |
} else { |
| 241 |
jQuery(".search-count").html(count + " Icons found."); |
| 242 |
} |
| 243 |
jQuery(".search-count").addClass("search-count-open"); |
| 244 |
if(filter === "") { |
| 245 |
jQuery(".fonts-count").show(); |
| 246 |
} |
| 247 |
}); |
| 248 |
}); |
| 249 |
}); |
| 250 |
</script>'; |
| 251 |
echo $script; |
| 252 |
} |
| 253 |
|
| 254 |
/** |
| 255 |
* Handles the Ajax call for adding a zipped font |
| 256 |
*/ |
| 257 |
function add_zipped_font() { |
| 258 |
$cap = apply_filters( 'avf_file_upload_capability', 'update_plugins' ); |
| 259 |
if ( ! current_user_can( $cap ) ) { |
| 260 |
die( esc_html__( "You do not have sufficient permissions to use this feature.", "borderless" ) ); |
| 261 |
} |
| 262 |
// Retrieve the file path of the zip file |
| 263 |
$attachment = $_POST['values']; |
| 264 |
$path = realpath( get_attached_file( sanitize_text_field( $attachment['id'] ) ) ); |
| 265 |
$unzipped = $this->zip_flatten( $path, array( '\.eot', '\.svg', '\.ttf', '\.woff', '\.json', '\.css' ) ); |
| 266 |
|
| 267 |
// If we managed to unzip the file and save it, extract the SVG file |
| 268 |
if ( $unzipped ) { |
| 269 |
$this->create_config(); |
| 270 |
} |
| 271 |
|
| 272 |
// If we did not get a name for the font, do not add it and remove the temp folder |
| 273 |
if ( $this->ip_name == 'unknown' ) { |
| 274 |
$this->delete_folder( $this->paths['tempdir'] ); |
| 275 |
die( esc_html__( 'Unable to retrieve the Icon Pack name from your uploaded folder.', 'borderless' ) ); |
| 276 |
} |
| 277 |
die( esc_html__( 'borderless_font_added:', 'borderless' ) . $this->ip_name ); |
| 278 |
} |
| 279 |
|
| 280 |
/** |
| 281 |
* Handles the Ajax call for removing a zipped font |
| 282 |
*/ |
| 283 |
function remove_zipped_font() { |
| 284 |
// Nonce verification for CSRF protection |
| 285 |
check_ajax_referer( 'borderless_remove_zipped_font_nonce', 'security' ); |
| 286 |
|
| 287 |
// Capability check to ensure only authorized users can delete |
| 288 |
$cap = apply_filters( 'avf_file_upload_capability', 'update_plugins' ); |
| 289 |
if ( ! current_user_can( $cap ) ) { |
| 290 |
die( esc_html__( "You do not have sufficient permissions to use this feature.", "borderless" ) ); |
| 291 |
} |
| 292 |
|
| 293 |
// Retrieve the font to be deleted |
| 294 |
$font = sanitize_text_field( $_POST['del_font'] ); |
| 295 |
$list = self::load_iconfont_list(); |
| 296 |
$delete = isset( $list[ $font ] ) ? $list[ $font ] : false; |
| 297 |
if ( $delete ) { |
| 298 |
$this->delete_folder( $delete['include'] ); |
| 299 |
$this->remove_font( $font ); |
| 300 |
die( esc_html__( 'borderless_font_removed', 'borderless' ) ); |
| 301 |
} |
| 302 |
die( esc_html__( 'Unable to remove Icon Pack', 'borderless' ) ); |
| 303 |
} |
| 304 |
|
| 305 |
/** |
| 306 |
* Unzips a font file to a flat folder and removes unneeded files |
| 307 |
*/ |
| 308 |
function zip_flatten( $zipfile, $filter ) { |
| 309 |
if ( is_dir( $this->paths['tempdir'] ) ) { |
| 310 |
$this->delete_folder( $this->paths['tempdir'] ); |
| 311 |
$tempdir = borderless_backend_create_folder( $this->paths['tempdir'], false ); |
| 312 |
} else { |
| 313 |
$tempdir = borderless_backend_create_folder( $this->paths['tempdir'], false ); |
| 314 |
} |
| 315 |
if ( ! $tempdir ) { |
| 316 |
die( esc_html__( 'Could not create the temporary folder.', 'borderless' ) ); |
| 317 |
} |
| 318 |
|
| 319 |
$zip = new ZipArchive; |
| 320 |
if ( $zip->open( $zipfile ) ) { |
| 321 |
for ( $i = 0; $i < $zip->numFiles; $i++ ) { |
| 322 |
$entry = $zip->getNameIndex( $i ); |
| 323 |
if ( ! empty( $filter ) ) { |
| 324 |
$delete = true; |
| 325 |
$matches = array(); |
| 326 |
foreach ( $filter as $regex ) { |
| 327 |
preg_match( "!" . $regex . "!", $entry, $matches ); |
| 328 |
if ( ! empty( $matches ) ) { |
| 329 |
$delete = false; |
| 330 |
break; |
| 331 |
} |
| 332 |
} |
| 333 |
} |
| 334 |
// Skip directories and non-matching files |
| 335 |
if ( substr( $entry, -1 ) == '/' || ! empty( $delete ) ) { |
| 336 |
continue; |
| 337 |
} |
| 338 |
$fp = $zip->getStream( $entry ); |
| 339 |
$ofp = fopen( $this->paths['tempdir'] . '/' . basename( $entry ), 'w' ); |
| 340 |
if ( ! $fp ) { |
| 341 |
die( esc_html__( 'Unable to unpack the Icon Pack.', 'borderless' ) ); |
| 342 |
} |
| 343 |
while ( ! feof( $fp ) ) { |
| 344 |
fwrite( $ofp, fread( $fp, 8192 ) ); |
| 345 |
} |
| 346 |
fclose( $fp ); |
| 347 |
fclose( $ofp ); |
| 348 |
} |
| 349 |
$zip->close(); |
| 350 |
} else { |
| 351 |
die( esc_html__( "The Icon Pack zip file is corrupted.", 'borderless' ) ); |
| 352 |
} |
| 353 |
|
| 354 |
return true; |
| 355 |
} |
| 356 |
|
| 357 |
/** |
| 358 |
* Reads JSON and SVG files, sanitizes them, and prepares the config |
| 359 |
*/ |
| 360 |
function create_config() { |
| 361 |
$this->json_file = $this->find_json(); |
| 362 |
$this->svg_file = $this->find_svg(); |
| 363 |
if ( empty( $this->json_file ) || empty( $this->svg_file ) ) { |
| 364 |
$this->delete_folder( $this->paths['tempdir'] ); |
| 365 |
die( esc_html__( 'SVG file or selection.json not found. Please check the integrity of the Icon Pack files.', 'borderless' ) ); |
| 366 |
} |
| 367 |
|
| 368 |
// Attempt to read the SVG file |
| 369 |
$response = wp_remote_fopen( trailingslashit( $this->paths['tempurl'] ) . $this->svg_file ); |
| 370 |
$json = file_get_contents( trailingslashit( $this->paths['tempdir'] ) . $this->json_file ); |
| 371 |
|
| 372 |
if ( empty( $response ) ) { |
| 373 |
$response = file_get_contents( trailingslashit( $this->paths['tempdir'] ) . $this->svg_file ); |
| 374 |
} |
| 375 |
|
| 376 |
if ( ! is_wp_error( $json ) && ! empty( $json ) ) { |
| 377 |
// Load and parse the SVG |
| 378 |
$xml = simplexml_load_string( $response ); |
| 379 |
$font_attr = $xml->defs->font->attributes(); |
| 380 |
$glyphs = $xml->defs->font->children(); |
| 381 |
|
| 382 |
// Sanitize the font name (ip_name) - changed to sanitize_file_name() for security |
| 383 |
$this->ip_name = (string) $font_attr['id']; |
| 384 |
$this->ip_name = sanitize_file_name( $this->ip_name ); |
| 385 |
|
| 386 |
if ( empty( $this->ip_name ) ) { |
| 387 |
$this->ip_name = 'unknown'; |
| 388 |
} |
| 389 |
|
| 390 |
$font_folder = trailingslashit( $this->paths['fontdir'] ) . $this->ip_name; |
| 391 |
if ( is_dir( $font_folder ) ) { |
| 392 |
$this->delete_folder( $this->paths['tempdir'] ); |
| 393 |
die( esc_html__( "There is already an Icon Pack with this name. Please upload with another name.", "borderless" ) ); |
| 394 |
} |
| 395 |
|
| 396 |
// Decode JSON content |
| 397 |
$file_contents = json_decode( $json ); |
| 398 |
if ( ! isset( $file_contents->IcoMoonType ) ) { |
| 399 |
$this->delete_folder( $this->paths['tempdir'] ); |
| 400 |
die( esc_html__( "This Icon Pack is not an Icomoon pack. Use only Icomoon Icon Pack files.", 'borderless' ) ); |
| 401 |
} |
| 402 |
|
| 403 |
// Iterate over icons to build $this->json_config |
| 404 |
$icons = $file_contents->icons; |
| 405 |
foreach ( $icons as $icon ) { |
| 406 |
// Sanitize each piece of data to prevent injection |
| 407 |
$raw_name = isset( $icon->properties->name ) ? $icon->properties->name : 'unknown'; |
| 408 |
$icon_name = sanitize_text_field( $raw_name ); |
| 409 |
|
| 410 |
// Create a "class" by removing spaces and sanitizing further |
| 411 |
$icon_class = str_replace( ' ', '', $icon_name ); |
| 412 |
$icon_class = preg_replace( '/[^A-Za-z0-9\-_]/', '', $icon_class ); |
| 413 |
|
| 414 |
$raw_tags = isset( $icon->icon->tags ) ? $icon->icon->tags : array(); |
| 415 |
$safe_tags = array_map( 'sanitize_text_field', $raw_tags ); |
| 416 |
$tags = implode( ",", $safe_tags ); |
| 417 |
|
| 418 |
$this->json_config[ $this->ip_name ][ $icon_name ] = array( |
| 419 |
"class" => $icon_class, |
| 420 |
"tags" => $tags |
| 421 |
); |
| 422 |
} |
| 423 |
|
| 424 |
if ( ! empty( $this->json_config ) && $this->ip_name != 'unknown' ) { |
| 425 |
$this->write_config(); |
| 426 |
$this->re_write_css(); |
| 427 |
$this->rename_files(); |
| 428 |
$this->rename_folder(); |
| 429 |
$this->add_font(); |
| 430 |
} |
| 431 |
} |
| 432 |
|
| 433 |
return false; |
| 434 |
} |
| 435 |
|
| 436 |
/** |
| 437 |
* Writes the charmap.php config file with sanitized data |
| 438 |
*/ |
| 439 |
function write_config() { |
| 440 |
$charmap = $this->paths['tempdir'] . '/' . $this->paths['config']; |
| 441 |
$handle = @fopen( $charmap, 'w' ); |
| 442 |
if ( $handle ) { |
| 443 |
fwrite( $handle, '<?php $icons = array();' ); |
| 444 |
|
| 445 |
// Safely build the PHP array with sanitized content |
| 446 |
foreach ( $this->json_config[ $this->ip_name ] as $icon => $info ) { |
| 447 |
// Escapes to prevent any code injection in the generated PHP |
| 448 |
$safe_icon = addslashes( $icon ); |
| 449 |
$safe_class = addslashes( $info["class"] ); |
| 450 |
$safe_tags = addslashes( $info["tags"] ); |
| 451 |
$escaped_ip_name = addslashes( $this->ip_name ); |
| 452 |
|
| 453 |
fwrite( |
| 454 |
$handle, |
| 455 |
"\r\n" . |
| 456 |
'$icons[\'' . $escaped_ip_name . '\'][\'' . $safe_icon . '\'] = array("class"=>\'' . $safe_class . '\',"tags"=>\'' . $safe_tags . '\');' |
| 457 |
); |
| 458 |
} |
| 459 |
fclose( $handle ); |
| 460 |
} else { |
| 461 |
$this->delete_folder( $this->paths['tempdir'] ); |
| 462 |
die( esc_html__( 'Error generating the configuration file.', 'borderless' ) ); |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
/** |
| 467 |
* Rewrites the CSS file to replace paths and standardize classes |
| 468 |
*/ |
| 469 |
function re_write_css() { |
| 470 |
$style = $this->paths['tempdir'] . '/style.css'; |
| 471 |
$file = @file_get_contents( $style ); |
| 472 |
if ( $file ) { |
| 473 |
$str = str_replace( 'fonts/', '', $file ); |
| 474 |
$str = str_replace( 'icon-', $this->ip_name . '-', $str ); |
| 475 |
$str = str_replace( '.icon {', '[class^="' . $this->ip_name . '-"], [class*=" ' . $this->ip_name . '-"] {', $str ); |
| 476 |
$str = str_replace( 'i {', '[class^="' . $this->ip_name . '-"], [class*=" ' . $this->ip_name . '-"] {', $str ); |
| 477 |
|
| 478 |
// Remove comments |
| 479 |
$str = preg_replace( '!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $str ); |
| 480 |
// Remove tabs, spaces, newlines, etc. |
| 481 |
$str = str_replace( array( "\r\n", "\r", "\n", "\t", ' ', ' ', ' ' ), '', $str ); |
| 482 |
|
| 483 |
@file_put_contents( $style, $str ); |
| 484 |
} else { |
| 485 |
die( esc_html__( "Error generating the CSS file. Make sure you are using Icomoon's Icon Pack.", 'borderless' ) ); |
| 486 |
} |
| 487 |
} |
| 488 |
|
| 489 |
/** |
| 490 |
* Renames font files to match the sanitized ip_name |
| 491 |
*/ |
| 492 |
function rename_files() { |
| 493 |
$extensions = array( 'eot', 'svg', 'ttf', 'woff', 'css' ); |
| 494 |
$folder = trailingslashit( $this->paths['tempdir'] ); |
| 495 |
foreach ( glob( $folder . '*' ) as $file ) { |
| 496 |
$path_parts = pathinfo( $file ); |
| 497 |
if ( strpos( $path_parts['filename'], '.dev' ) === false && in_array( $path_parts['extension'], $extensions ) ) { |
| 498 |
if ( $path_parts['filename'] !== $this->ip_name ) { |
| 499 |
rename( $file, trailingslashit( $path_parts['dirname'] ) . $this->ip_name . '.' . $path_parts['extension'] ); |
| 500 |
} |
| 501 |
} |
| 502 |
} |
| 503 |
} |
| 504 |
|
| 505 |
/** |
| 506 |
* Renames the temporary folder to a final folder |
| 507 |
*/ |
| 508 |
function rename_folder() { |
| 509 |
$new_name = trailingslashit( $this->paths['fontdir'] ) . $this->ip_name; |
| 510 |
// Delete folder if it already exists |
| 511 |
$this->delete_folder( $new_name ); |
| 512 |
if ( rename( $this->paths['tempdir'], $new_name ) ) { |
| 513 |
return true; |
| 514 |
} else { |
| 515 |
$this->delete_folder( $this->paths['tempdir'] ); |
| 516 |
die( esc_html__( "This Icon Pack could not be added.", "borderless" ) ); |
| 517 |
} |
| 518 |
} |
| 519 |
|
| 520 |
/** |
| 521 |
* Deletes a given folder and its contents |
| 522 |
*/ |
| 523 |
function delete_folder( $new_name ) { |
| 524 |
if ( is_dir( $new_name ) ) { |
| 525 |
$objects = scandir( $new_name ); |
| 526 |
foreach ( $objects as $object ) { |
| 527 |
if ( $object != "." && $object != ".." ) { |
| 528 |
@unlink( $new_name . "/" . $object ); |
| 529 |
} |
| 530 |
} |
| 531 |
reset( $objects ); |
| 532 |
@rmdir( $new_name ); |
| 533 |
} |
| 534 |
} |
| 535 |
|
| 536 |
/** |
| 537 |
* Adds new font info to the WP option 'borderless_icon_fonts' |
| 538 |
*/ |
| 539 |
function add_font() { |
| 540 |
$fonts = get_option( 'borderless_icon_fonts' ); |
| 541 |
if ( empty( $fonts ) ) { |
| 542 |
$fonts = array(); |
| 543 |
} |
| 544 |
$fonts[ $this->ip_name ] = array( |
| 545 |
'include' => sanitize_text_field( trailingslashit( $this->paths['fonts'] ) ) . $this->ip_name, |
| 546 |
'folder' => sanitize_text_field( trailingslashit( $this->paths['fonts'] ) ) . $this->ip_name, |
| 547 |
'style' => $this->ip_name . '/' . $this->ip_name . '.css', |
| 548 |
'config' => sanitize_text_field( $this->paths['config'] ) |
| 549 |
); |
| 550 |
update_option( 'borderless_icon_fonts', $fonts ); |
| 551 |
} |
| 552 |
|
| 553 |
/** |
| 554 |
* Removes specified font info from the WP option |
| 555 |
*/ |
| 556 |
function remove_font( $font ) { |
| 557 |
$fonts = get_option( 'borderless_icon_fonts' ); |
| 558 |
if ( isset( $fonts[ $font ] ) ) { |
| 559 |
unset( $fonts[ $font ] ); |
| 560 |
update_option( 'borderless_icon_fonts', $fonts ); |
| 561 |
} |
| 562 |
} |
| 563 |
|
| 564 |
/** |
| 565 |
* Finds the first JSON file in the temp folder |
| 566 |
*/ |
| 567 |
function find_json() { |
| 568 |
$files = scandir( $this->paths['tempdir'] ); |
| 569 |
foreach ( $files as $file ) { |
| 570 |
if ( strpos( strtolower( $file ), '.json' ) !== false && $file[0] != '.' ) { |
| 571 |
return $file; |
| 572 |
} |
| 573 |
} |
| 574 |
return null; |
| 575 |
} |
| 576 |
|
| 577 |
/** |
| 578 |
* Finds the first SVG file in the temp folder |
| 579 |
*/ |
| 580 |
function find_svg() { |
| 581 |
$files = scandir( $this->paths['tempdir'] ); |
| 582 |
foreach ( $files as $file ) { |
| 583 |
if ( strpos( strtolower( $file ), '.svg' ) !== false && $file[0] != '.' ) { |
| 584 |
return $file; |
| 585 |
} |
| 586 |
} |
| 587 |
return null; |
| 588 |
} |
| 589 |
|
| 590 |
/** |
| 591 |
* Loads the list of iconfonts from the WP option (cached locally) |
| 592 |
*/ |
| 593 |
static function load_iconfont_list() { |
| 594 |
if ( ! empty( self::$ip_list ) ) { |
| 595 |
return self::$ip_list; |
| 596 |
} |
| 597 |
$extra_fonts = get_option( 'borderless_icon_fonts' ); |
| 598 |
if ( empty( $extra_fonts ) ) { |
| 599 |
$extra_fonts = array(); |
| 600 |
} |
| 601 |
$font_configs = $extra_fonts; |
| 602 |
$upload_dir = wp_upload_dir(); |
| 603 |
$path = trailingslashit( $upload_dir['basedir'] ); |
| 604 |
$url = trailingslashit( $upload_dir['baseurl'] ); |
| 605 |
|
| 606 |
foreach ( $font_configs as $key => $config ) { |
| 607 |
if ( empty( $config['full_path'] ) ) { |
| 608 |
$font_configs[ $key ]['include'] = $path . $font_configs[ $key ]['include']; |
| 609 |
$font_configs[ $key ]['folder'] = $url . $font_configs[ $key ]['folder']; |
| 610 |
} |
| 611 |
} |
| 612 |
self::$ip_list = $font_configs; |
| 613 |
return $font_configs; |
| 614 |
} |
| 615 |
} // End class Borderless_IF |
| 616 |
|
| 617 |
/** |
| 618 |
* Creates a folder for the plugin framework |
| 619 |
*/ |
| 620 |
if ( ! function_exists( 'borderless_backend_create_folder' ) ) { |
| 621 |
function borderless_backend_create_folder( &$folder, $addindex = true ) { |
| 622 |
if ( is_dir( $folder ) && $addindex == false ) { |
| 623 |
return true; |
| 624 |
} |
| 625 |
$created = wp_mkdir_p( trailingslashit( $folder ) ); |
| 626 |
@chmod( $folder, 0777 ); |
| 627 |
if ( $addindex == false ) { |
| 628 |
return $created; |
| 629 |
} |
| 630 |
$index_file = trailingslashit( $folder ) . 'index.php'; |
| 631 |
if ( file_exists( $index_file ) ) { |
| 632 |
return $created; |
| 633 |
} |
| 634 |
$handle = @fopen( $index_file, 'w' ); |
| 635 |
if ( $handle ) { |
| 636 |
fwrite( $handle, "<?php\r\necho 'We are sorry, but you are not allowed to browse this directory.';\r\n?>" ); |
| 637 |
fclose( $handle ); |
| 638 |
} |
| 639 |
return $created; |
| 640 |
} |
| 641 |
} |
| 642 |
|
| 643 |
// Instantiate the main Icon Fonts class |
| 644 |
new Borderless_IF; |
| 645 |
} |
| 646 |
|
| 647 |
/** |
| 648 |
* Adds the Icon Pack Manager to the WP admin menu |
| 649 |
*/ |
| 650 |
function borderless_custom_icons_menu() { |
| 651 |
$Borderless_IF = new Borderless_IF; |
| 652 |
$Borderless_IF->icon_pack_manager(); |
| 653 |
} |
| 654 |
|
| 655 |
/** |
| 656 |
* Enqueues the custom icon fonts on the frontend |
| 657 |
*/ |
| 658 |
function borderless_custom_icons() { |
| 659 |
$upload_paths = wp_upload_dir(); |
| 660 |
$custom_fonts = get_option( 'borderless_icon_fonts' ); |
| 661 |
if ( is_array( $custom_fonts ) ) { |
| 662 |
foreach ( $custom_fonts as $font => $info ) { |
| 663 |
if ( strpos( $info['style'], 'http://' ) !== false ) { |
| 664 |
wp_enqueue_style( 'borderless-' . $font, $info['style'], null, '1.0', 'all' ); |
| 665 |
} else { |
| 666 |
wp_enqueue_style( 'borderless-' . $font, trailingslashit( $upload_paths['baseurl'] . '/borderless_icon_fonts/' ) . $info['style'], null, '1.0', 'all' ); |
| 667 |
} |
| 668 |
} |
| 669 |
} |
| 670 |
} |
| 671 |
add_action( 'wp_enqueue_scripts', 'borderless_custom_icons' ); |
| 672 |
|