PluginProbe
Easy Quotes / trunk
Easy Quotes vtrunk
trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.1.0 1.1.1 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 All 28 releases
easy-quotes / includes / font.php

font.php in Easy Quotes trunk, at includes/font.php

119 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 // Exit if accessed directly.
4 if (!defined('ABSPATH')) {
5 exit;
6 }
7
8 class Easy_Quotes_Font
9 {
10 private $fontFamily;
11 private $fontClassName;
12 private $fontPath;
13 private $fontURL;
14
15 function __construct($fontFamily)
16 {
17 $this->fontFamily = $fontFamily;
18 $this->fontClassName = $this->get_font_class_name();
19 $this->fontPath = $this->get_font_path();
20 $this->fontURL = $this->get_font_url();
21 }
22
23 /**
24 * Calls Javascript Function to add style @font-face url to document
25 *
26 * @return string fontClassName
27 */
28 public function add_font_style($widgetId = null) {
29 $jsdata = array(
30 'fontURL' => $this->fontURL,
31 'fontClassName' => $this->fontClassName,
32 'isCustomizer' => is_customize_preview(),
33 'widgetId' => $widgetId
34 );
35 ?>
36 <script>
37 var JSDATA = <?php echo wp_json_encode( $jsdata, JSON_HEX_TAG | JSON_HEX_AMP ) ?>;
38 createStyleForFont(JSDATA.fontURL, JSDATA.fontClassName, JSDATA.isCustomizer, JSDATA.widgetId);
39 </script>
40 <?php
41
42 return $this->fontClassName;
43 }
44
45 /**
46 * Return a "css classname" (the same name as the font filename)
47 *
48 * @return string font_class_name
49 */
50 private function get_font_class_name() {
51 return $this->fontFamily['family_slug'] . "-"
52 . $this->fontFamily['variant']['id'];
53 }
54
55 /**
56 * Returns the local directory structure to the file and filename.ttf
57 *
58 * @return string directory
59 */
60 private function get_font_path() {
61 return "fonts/"
62 . $this->fontFamily['family_slug'] . "/"
63 . $this->fontFamily['version'] . "/"
64 . $this->fontClassName . "."
65 . pathinfo($this->fontFamily['variant']['file'], PATHINFO_EXTENSION);
66 }
67
68 /**
69 * Returns the local URL to the Font
70 *
71 * @return string local url to font file
72 */
73 private function get_font_url() {
74 $font_local_path = EASY_QUOTES_PLUGIN_DIR . $this->fontPath;
75 if (!file_exists($font_local_path)) {
76
77 if (is_customize_preview())
78 return $this->get_font_google_url();
79
80 // if directory doesn't exist – create it
81 $font_local_directory = pathinfo($font_local_path, PATHINFO_DIRNAME);
82 if (!file_exists($font_local_directory)) {
83 wp_mkdir_p($font_local_directory);
84 }
85
86 $this->download_google_font();
87 }
88 return EASY_QUOTES_PLUGIN_URL . $this->fontPath;
89 }
90
91 /**
92 * Download the Google Font to a local Directory
93 *
94 * @return bool success
95 */
96 private function download_google_font() {
97 if ( !function_exists( 'download_url' ) ) {
98 require_once ABSPATH . 'wp-admin/includes/file.php';
99 }
100 $tmpFile = download_url($this->get_font_google_url());
101 $result = copy($tmpFile, EASY_QUOTES_PLUGIN_DIR . $this->fontPath);
102 wp_delete_file($tmpFile);
103 return $result;
104 }
105
106 /**
107 * Returns the URL of the Font on Google Fonts
108 *
109 * @return string url
110 */
111 private function get_font_google_url() {
112 return "https://fonts.gstatic.com/s/"
113 . $this->fontFamily['family_slug'] . "/"
114 . $this->fontFamily['version'] . "/"
115 . $this->fontFamily['variant']['file'];
116 }
117
118 }
119