Changeset 762516
- Timestamp:
- 08/26/2013 04:33:05 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
web-fonts/trunk/modules/google-web-fonts/google-web-fonts.php
r523474 r762516 11 11 if(!class_exists('Google_Web_Fonts_Plugin')) { 12 12 class Google_Web_Fonts_Plugin { 13 13 14 14 /// KEYS 15 15 16 16 //// VERSION 17 17 const VERSION = '1.0.0-RC2'; 18 18 19 19 //// KEYS 20 20 const SETTINGS_KEY = '_google_web_fonts_settings'; 21 21 22 22 const FONT_DATA_KEY = '_google_web_fonts_fonts'; 23 23 const SELECTOR_DATA_KEY = '_google_web_fonts_selectors'; 24 24 const SORTABLE_FONT_TRANSIENT_BASE = '_google_web_fonts_fonts_'; 25 25 const SORTABLE_FONT_TRANSIENT_TIMEOUT = 600; 26 26 27 27 /// DATA STORAGE 28 28 public static $admin_page_hooks = array(); 29 29 private static $default_settings = array(); 30 30 private static $variants_map = array(); 31 31 32 32 public static function init() { 33 33 self::add_actions(); 34 34 self::add_filters(); 35 35 self::initialize_defaults(); 36 36 37 37 if(function_exists('register_web_fonts_provider')) { 38 38 register_web_fonts_provider('Google_Web_Fonts_Provider'); 39 39 } 40 40 } 41 41 42 42 private static function add_actions() { 43 43 if(is_admin()) { … … 49 49 add_action('wp_head', array(__CLASS__, 'display_styles'), 11); 50 50 } 51 51 52 52 add_action('wp_ajax_web_fonts_google_web_fonts_clear_key', array(__CLASS__, 'ajax_container')); 53 53 add_action('wp_ajax_web_fonts_google_web_fonts_set_key', array(__CLASS__, 'ajax_container')); 54 54 55 55 add_action('wp_ajax_web_fonts_google_web_fonts_get_fonts', array(__CLASS__, 'ajax_container')); 56 56 add_action('wp_ajax_web_fonts_google_web_fonts_set_font_status', array(__CLASS__, 'ajax_container')); 57 57 } 58 58 59 59 private static function add_filters() { 60 60 add_filter('web_fonts_manage_stylesheet_fonts_and_selectors', array(__CLASS__, 'add_stylesheet_fonts_and_selectors')); … … 84 84 self::$variants_map['italic'] = __('Normal'); 85 85 self::$variants_map['regular'] = __('Normal'); 86 87 } 88 86 87 } 88 89 89 /// AJAX CALLBACKS 90 90 91 91 public static function ajax_container() { 92 92 $data = self::trim_r(stripslashes_deep($_REQUEST)); 93 93 $action = str_replace('web_fonts_google_web_fonts_', '', $data['action']); 94 94 $method_name = "ajax_{$action}"; 95 95 96 96 if(isset($data['nonce']) && wp_verify_nonce($data['nonce'], 'google-web-fonts-action') && method_exists(__CLASS__, $method_name)) { 97 97 $results = self::$method_name($data); … … 99 99 $results = self::get_response(array(), __('Something went wrong. Please refresh the page and try again.'), true); 100 100 } 101 101 102 102 header('Content-Type: application/json'); 103 103 echo json_encode($results); 104 104 exit; 105 105 } 106 106 107 107 public static function ajax_clear_key($data) { 108 108 self::set_settings(array()); 109 109 110 110 return self::get_response(array(), __('The authentication key was successfully cleared.')); 111 111 } 112 112 113 113 public static function ajax_set_key($data) { 114 114 $fonts = self::get_fonts($data['key'], '', 'alpha', 0, 1, true); 115 115 116 116 if(is_wp_error($fonts)) { 117 117 $results = self::get_response(array(), __('Your API Key could not be validated. Please enter a valid key.'), true); 118 118 } else { 119 119 self::set_settings(array('api-key' => $data['key'])); 120 120 121 121 $results = self::get_response(array('key' => $data['key']), __('Your API Key has been validated and saved.'), false); 122 122 } 123 123 124 124 return $results; 125 125 } … … 128 128 $page = isset($data['page_number']) && is_numeric($data['page_number']) && $data['page_number'] >= 1 ? (intval($data['page_number']) - 1) : 0; 129 129 $page_limit = 12; 130 130 131 131 $search_keyword = $data['search_keyword']; 132 132 $search_sort = $data['search_sort']; 133 133 $settings = self::get_settings(); 134 134 135 135 $font_search_response = self::get_fonts($settings['api-key'], $search_keyword, $search_sort, $page, $page_limit); 136 136 137 137 if(is_wp_error($font_search_response)) { 138 138 $results = self::get_response(null, __('There was an issue retrieving the appropriate fonts. Please try again.'), true); … … 140 140 $results = self::get_response($font_search_response); 141 141 } 142 142 143 143 return $results; 144 144 } 145 145 146 146 public static function ajax_set_font_status($data) { 147 147 $enabled = ($data['enabled'] == 1); 148 148 $font_data = $data['font_data']; 149 149 $font_key = sanitize_title_with_dashes($font_data['family_name']); 150 150 151 151 $enabled_fonts = self::get_font_data(); 152 152 153 153 if($enabled) { 154 154 $enabled_fonts[$font_key] = $font_data; … … 156 156 unset($enabled_fonts[$font_key]); 157 157 } 158 158 159 159 self::set_font_data($enabled_fonts); 160 160 161 161 $results = self::get_response(array('enabled' => $enabled, 'font_data' => $font_data, 'enabled_fonts' => $enabled_fonts), sprintf(__('The selected font was %s.'), ($enabled ? __('enabled') : __('disabled')))); 162 162 163 163 return $results; 164 164 } 165 165 166 166 /// CALLBACKS 167 167 168 168 public static function add_stylesheet_fonts_and_selectors($data) { 169 169 $enabled_fonts = self::get_font_data(); 170 170 $enabled_selectors = self::get_selector_data(); 171 171 172 172 $font_selector_map = array(); 173 173 174 174 foreach($enabled_selectors as $enabled_selector_id => $enabled_selector) { 175 175 $data['selectors'][] = $prepared_selector = web_fonts_prepare_selector_item('google-web-fonts', $enabled_selector_id, $enabled_selector['tag'], $enabled_selector['fallback'], 'google-web-fonts-' . $enabled_selector['font-id']); 176 176 177 177 if(!empty($enabled_selector['font-id']) && isset($enabled_fonts[$enabled_selector['font-id']])) { 178 178 if(!is_array($font_selector_map[$enabled_selector['font-id']])) { … … 182 182 } 183 183 } 184 184 185 185 foreach($enabled_fonts as $enabled_font_id => $enabled_font) { 186 186 $data['fonts'][] = web_fonts_prepare_font_item('google-web-fonts', $enabled_font_id, $enabled_font['family_name'], $enabled_font['family'], 'Quick Brown Fox Jumped Over The Lazy Dog', $font_selector_map[$enabled_font_id], array('fontFamily' => $enabled_font['family'], 'fontStyle' => $enabled_font['style'], 'fontWeight' => $enabled_font['weight'])); 187 187 } 188 188 189 189 return $data; 190 190 } 191 191 192 192 public static function detect_submissions() { 193 193 $data = stripslashes_deep($_REQUEST); 194 194 } 195 195 196 196 public static function enqueue_administrative_resources($hook) { 197 197 // This is a kludy hack and I hate it, but oh well - have to do it … … 199 199 self::enqueue_frontend_resources(); 200 200 } 201 201 202 202 if(!in_array($hook, self::$admin_page_hooks)) { return; } 203 203 204 204 wp_enqueue_script('google-web-fonts-backend', plugins_url('resources/backend/google-web-fonts.js', __FILE__), array('jquery', 'jquery-form', 'thickbox'), self::VERSION); 205 205 wp_enqueue_style('google-web-fonts-backend', plugins_url('resources/backend/google-web-fonts.css', __FILE__), array('thickbox'), self::VERSION); 206 206 207 207 $strings = array( 208 208 'request_in_progress_message' => __('There is already a request in progress. Please wait until the request has completed before trying another action.'), … … 210 210 'enabled_fonts_title' => __('Enabled Fonts'), 211 211 ); 212 212 213 213 wp_localize_script('google-web-fonts-backend', 'Google_Web_Fonts_Config', $strings); 214 214 } 215 215 216 216 public static function enqueue_frontend_resources() { 217 217 $enabled_fonts = self::get_font_data(); 218 218 219 219 if(!empty($enabled_fonts)) { 220 220 $url_parts = array(); … … 228 228 } 229 229 } 230 230 231 231 public static function handle_stylesheet_fonts($fonts) { 232 232 $selectors_to_save = array(); 233 233 234 234 foreach($fonts as $font) { 235 235 if('google-web-fonts' == $font->provider && is_array($font->selectors)) { … … 243 243 } 244 244 } 245 245 246 246 self::set_selector_data($selectors_to_save); 247 247 248 248 return $fonts; 249 249 } 250 250 251 251 public static function handle_stylesheet_selectors($selectors) { 252 252 $selectors_to_save = array(); 253 253 254 254 foreach($selectors as $selector) { 255 255 $font = $selector->font; 256 256 257 257 if(is_object($font) && 0 === strpos($font->id, 'google-web-fonts-')) { 258 258 $selectors_to_save['google-web-fonts-' . $selector->tag] = array( … … 263 263 } 264 264 } 265 265 266 266 self::set_selector_data($selectors_to_save); 267 267 268 268 return $selectors; 269 269 } 270 270 271 271 /// DISPLAY CALLBACKS 272 272 273 273 public static function display_styles() { 274 $font_data = self::get_font_data(); 274 $font_data = self::get_font_data(); 275 275 $selector_data = self::get_selector_data(); 276 276 277 277 if(!empty($selector_data)) { 278 278 echo "\n<!-- Google Web Fonts Style Declarations -->\n"; … … 281 281 if(isset($font_data[$selector['font-id']])) { 282 282 $font = $font_data[$selector['font-id']]; 283 283 284 284 printf('%s{font-family: "%s"%s; font-style: %s; font-weight: %d}', $selector['tag'], esc_attr($font['family']), empty($selector['fallback']) ? '' : ',' . esc_attr($selector['fallback']), $font['style'], $font['weight']); 285 285 } … … 289 289 } 290 290 } 291 291 292 292 public static function display_settings_page() { 293 293 $data = stripslashes_deep($_REQUEST); 294 294 295 295 $settings = self::get_settings(); 296 296 $is_setup = self::is_setup(); 297 297 298 298 $base_url = add_query_arg(array('page' => $data['page']), admin_url('admin.php')); 299 299 $valid_tabs = $is_setup ? array('setup', 'fonts') : array('setup'); 300 300 $current_tab = in_array($data['tab'], $valid_tabs) ? $data['tab'] : ($is_setup ? 'fonts' : 'setup'); 301 301 302 302 include('views/backend/settings/_inc/nav.php'); 303 303 304 304 // Make this dynamic 305 305 switch($current_tab) { … … 314 314 315 315 /// FONTS 316 316 317 317 private static function get_font_data() { 318 318 $font_data = wp_cache_get(self::FONT_DATA_KEY); 319 319 320 320 if(!is_array($selector_data)) { 321 321 $font_data = get_option(self::FONT_DATA_KEY, array()); 322 322 wp_cache_set(self::FONT_DATA_KEY, $font_data, null, time() + CACHE_PERIOD); 323 323 } 324 324 325 325 return $font_data; 326 326 } 327 327 328 328 private static function set_font_data($font_data) { 329 329 if(is_array($font_data)) { … … 332 332 } 333 333 } 334 334 335 335 /// SELECTORS 336 336 337 337 private static function get_selector_data() { 338 338 $selector_data = wp_cache_get(self::SELECTOR_DATA_KEY); 339 339 340 340 if(!is_array($selector_data)) { 341 341 $selector_data = get_option(self::SELECTOR_DATA_KEY, array()); 342 342 wp_cache_set(self::SELECTOR_DATA_KEY, $selector_data, null, time() + CACHE_PERIOD); 343 343 } 344 344 345 345 return $selector_data; 346 346 } 347 347 348 348 private static function set_selector_data($selector_data) { 349 349 if(is_array($selector_data)) { … … 352 352 } 353 353 } 354 354 355 355 /// SETTINGS 356 356 357 357 private static function get_settings() { 358 358 $settings = wp_cache_get(self::SETTINGS_KEY); 359 359 360 360 if(!is_array($settings)) { 361 361 $settings = wp_parse_args(get_option(self::SETTINGS_KEY, self::$default_settings), self::$default_settings); 362 362 wp_cache_set(self::SETTINGS_KEY, $settings, null, time() + CACHE_PERIOD); 363 363 } 364 364 365 365 return $settings; 366 366 } 367 367 368 368 private static function set_settings($settings) { 369 369 if(is_array($settings)) { … … 373 373 } 374 374 } 375 375 376 376 /// UTILITY 377 377 378 378 private static function get_response($data = array(), $message = null, $error = false) { 379 379 return array_merge(array('error' => (bool)$error, 'message' => $message), (array)$data); 380 380 } 381 381 382 382 private static function is_setup() { 383 383 $settings = self::get_settings(); 384 384 385 385 return !empty($settings['api-key']); 386 386 } 387 387 388 388 private static function trim_r($data) { 389 389 if(is_array($data)) { … … 397 397 } 398 398 } 399 399 400 400 /// API 401 401 402 402 private static function SORTABLE_FONT_TRANSIENT_BASE($sort) { 403 403 return self::SORTABLE_FONT_TRANSIENT_BASE . $sort; 404 404 } 405 405 406 406 private static function get_api($api_key = null) { 407 407 require_once('lib/wp-google-web-fonts-api.php'); 408 408 409 409 return new WP_Google_Web_Fonts_API($api_key); 410 410 } 411 411 412 412 private static function get_fonts($api_key, $search_keyword = '', $search_sort = 'alpha', $page = 0, $page_limit = 12, $fresh = false) { 413 413 if(!self::is_valid_sort($search_sort)) { 414 414 $search_sort = 'alpha'; 415 415 } 416 416 417 417 $key = self::SORTABLE_FONT_TRANSIENT_BASE($search_sort); 418 418 419 419 if(!$fresh) { 420 420 $fonts = get_transient($key); 421 421 } 422 422 423 423 if(!is_array($fonts)) { 424 424 $api = self::get_api($api_key); 425 425 $fonts = $api->get_fonts($search_sort); 426 426 427 427 if(is_array($fonts)) { 428 428 $fonts = self::separate_fonts($fonts); … … 430 430 } 431 431 } 432 432 433 433 if(is_array($fonts)) { 434 434 435 435 if(!empty($search_keyword)) { 436 436 $filtered_fonts = array(); 437 437 438 438 foreach($fonts as $font) { 439 439 if(false !== strpos($font->family_name, $search_keyword)) { … … 441 441 } 442 442 } 443 443 444 444 $fonts = $filtered_fonts; 445 445 } 446 446 447 447 foreach($fonts as $font) { 448 448 $font->is_enabled = self::is_font_enabled($font); 449 449 } 450 450 451 451 // This has to stay here before we array_slice 452 452 $number_fonts = count($fonts); 453 453 454 454 $enabled_fonts = self::get_font_data(); 455 455 $fonts = array_slice($fonts, ($page * $page_limit), $page_limit); … … 469 469 'add_fragment' => '' 470 470 )); 471 471 472 472 return compact('enabled_fonts', 'fonts', 'number_fonts', 'pagination_links'); 473 473 } else { … … 475 475 } 476 476 } 477 477 478 478 private static function is_valid_sort($sort) { 479 479 return in_array($sort, array('alpha', 'date', 'popularity', 'style', 'trending')); 480 480 } 481 481 482 482 private static function separate_fonts($fonts) { 483 483 $separated_fonts = array(); 484 484 485 485 foreach($fonts as $font) { 486 486 $variants = $font->variants; 487 487 unset($font->variants); 488 488 489 489 foreach($variants as $variant) { 490 490 $separated_fonts[] = self::add_variant_properties($font, $variant); 491 491 } 492 492 } 493 493 494 494 return $separated_fonts; 495 495 } 496 496 497 497 private static function add_variant_properties($font, $variant) { 498 498 if('bold' === $variant || 'bolditalic' === $variant) { … … 503 503 $weight = preg_replace('/[^\d]/', '', $variant); 504 504 } 505 505 506 506 $a_font = clone $font; 507 507 $a_font->family_name = sprintf('%s %s', $a_font->family, isset(self::$variants_map[$variant]) ? self::$variants_map[$variant] : __('Normal')); … … 513 513 $a_font->weight = $weight; 514 514 $a_font->weight_string = self::get_weight_string($a_font->weight); 515 515 516 516 return $a_font; 517 517 } … … 519 519 private static function is_font_enabled($font) { 520 520 $enabled_fonts = self::get_font_data(); 521 522 return isset($enabled_fonts[$font-> family_name]);523 } 524 521 522 return isset($enabled_fonts[$font->id]); 523 } 524 525 525 private static function get_weight_string($weight) { 526 526 return isset(self::$variants_map[$weight]) ? self::$variants_map[$weight] : __('Normal'); 527 527 } 528 528 } 529 529 530 530 require_once('lib/provider.php'); 531 531 Google_Web_Fonts_Plugin::init();
Note: See TracChangeset
for help on using the changeset viewer.