PluginProbe
Disable Comments & Delete All Comments / trunk
Disable Comments & Delete All Comments vtrunk
1.3.3 1.3.2 trunk 1.0.0 1.0.4 1.0.5 1.0.8 1.0.9 1.1.1 1.1.3 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 1.3.0 1.3.1
comments-plus / libs / factory / forms / controls / google-font.php

google-font.php in Disable Comments & Delete All Comments trunk, at libs/factory/forms/controls/google-font.php

153 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Dropdown List Control
5 *
6 * Main options:
7 * name => a name of the control
8 * value => a value to show in the control
9 * default => a default value of the control if the "value" option is not specified
10 * items => a callback to return items or an array of items to select
11 *
12 * @author Alex Kovalev <alex.kovalevv@gmail.com>
13 * @copyright (c) 2018, Webcraftic Ltd
14 *
15 * @package core
16 * @since 1.0.0
17 */
18 class Wbcr_FactoryForms480_GoogleFontControl extends Wbcr_FactoryForms480_FontControl {
19
20 public $type = 'google-font';
21 const APIKEY = 'AIzaSyB-3vazYv7Q-5QZA04bmSKFrWcw_VhC40w';
22
23 public function __construct($options, $form, $provider = null)
24 {
25 parent::__construct($options, $form, $provider);
26 $this->addCssClass('factory-font');
27
28 $option_google_font_data = array(
29 'name' => $this->options['name'] . '__google_font_data',
30 'cssClass' => 'factory-google-font-data'
31 );
32
33 $this->google_font_data = new Wbcr_FactoryForms480_HiddenControl($option_google_font_data, $form, $provider);
34 $this->inner_controls[] = $this->google_font_data;
35 }
36
37 /**
38 * @return array|mixed
39 */
40 public function getDefaultFonts()
41 {
42
43 $cache_fonts = get_transient('wbcr_factory_google_fonts');
44
45 if( !empty($cache_fonts) ) {
46 return $cache_fonts;
47 }
48
49 $google_fonts = $this->getGoogleFonts();
50
51 $fonts = array(
52 array('inherit', __('(use default website font)', 'wbcr_factory_forms_480'))
53 );
54
55 $fontsCommon = array(
56 'group',
57 __('Standard:', 'wbcr_factory_forms_480'),
58 array(
59
60 array('Arial, "Helvetica Neue", Helvetica, sans-serif', 'Arial'),
61 array('"Helvetica Neue", Helvetica, Arial, sans-serif', 'Helvetica'),
62 array('Tahoma, Verdana, Segoe, sans-serif', 'Tahoma'),
63 array('Verdana, Geneva, sans-serif', 'Verdana'),
64
65 )
66 );
67
68 $fontsGoogleFonts = array('group', __('Google Fonts:', 'wbcr_factory_forms_480'), array());
69
70 foreach($google_fonts->items as $item) {
71
72 $alt_font = $item->category;
73 if( in_array($alt_font, array('handwriting', 'display')) ) {
74 $alt_font = 'serif';
75 }
76
77 $listItem = array(
78 'title' => $item->family,
79 'value' => $item->family . ', ' . $item->category,
80 'hint' => '<em>Google Font</em>',
81 'data' => array(
82 'google-font' => true,
83 'family' => $item->family,
84 'variants' => $item->variants,
85 'subsets' => $item->subsets
86 )
87 );
88
89 $fontsGoogleFonts[2][] = $listItem;
90 }
91
92 $fonts[] = $fontsCommon;
93 $fonts[] = $fontsGoogleFonts;
94
95 set_transient('wbcr_factory_google_fonts', $fonts, 60 * 60 * 6);
96
97 return $fonts;
98 }
99
100 /**
101 * @return array|mixed|object
102 */
103 protected function getGoogleFonts()
104 {
105
106 $body = get_transient('wbcr_factory_google_fonts_raw');
107 if( !empty($body) ) {
108 return $body;
109 }
110
111 $response = wp_remote_get(sprintf('https://www.googleapis.com/webfonts/v1/webfonts?key=%s', self::APIKEY));
112
113 $this->error = false;
114 $this->defailed_error = false;
115
116 if( is_wp_error($response) ) {
117
118 $this->error = __('Unable to retrieve the list of Google Fonts.', 'wbcr_factory_forms_480');
119 $this->defailed_error = $response->get_error_message();
120
121 return $body;
122 }
123
124 if( !isset($response['body']) ) {
125
126 $this->error = __('Invalide response from the Google Fonts API.', 'wbcr_factory_forms_480');
127 $this->defailed_error = $response['body'];
128
129 return $body;
130 }
131
132 $body = json_decode($response['body']);
133
134 if( empty($body->items) ) {
135
136 $this->error = __('Unexpected error. The list of Google Fonts are empty.', 'wbcr_factory_forms_480');
137
138 return $body;
139 }
140
141 set_transient('wbcr_factory_google_fonts_raw', $body, 60 * 60 * 6);
142
143 return $body;
144 }
145
146 public function afterControlsHtml()
147 {
148 ?>
149 <?php $this->google_font_data->html() ?>
150 <?php
151 }
152 }
153