-
Notifications
You must be signed in to change notification settings - Fork 0
/
PFM-fontManager.php
214 lines (197 loc) · 7.68 KB
/
PFM-fontManager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php
/**
* @package Parane Font Manager
* @version 1.6
* Plugin Name: Parane Font Manager
* Description: Parane Font Manager allows you to use any fonts you want in wordpress
* Author: Parham Parnian
* Version: 1.0
* Author URI:
* Text Domain: pfm_fontManager
* Domain Path: /languages/
*/
include_once __DIR__."/includes/AdminPage.php";
class PFM_FontManager {
private $adminPage;
function __construct()
{
$this->adminPage = new PFM_AdminPage();
add_action("admin_enqueue_scripts",array($this,"FontManagerAdminStyle"));
add_action("admin_menu",array($this,"FontManagerMenu"));
add_action("plugins_loaded",array($this, 'FontManagerLocal' ));
add_action('wp_ajax_FontManagerSaveFont', array($this,"FontManagerSaveFont"));
add_action('wp_ajax_FontManagerLoadFont', array($this,"FontManagerLoadFont"));
if(!is_admin()){
//wp_enqueue_style("pfm_fontManagerStyelSheet", plugin_dir_url(__FILE__)."assets/css/FontManager.css");
add_action( 'wp_enqueue_scripts',array($this ,'LoadCssInHead'));
}else{
add_filter('upload_mimes', array($this,'AddCustomUploadMimes'));
}
}
function FontManagerAdminStyle()
{
wp_enqueue_media();
wp_enqueue_style("pfm_bootstrap",plugin_dir_url(__FILE__)."assets/css/bootstrap.css");
wp_enqueue_style("pfm_fontManagerAdminStyle",plugin_dir_url(__FILE__)."assets/css/admin.css");
wp_enqueue_script('pfm_bootstrapJs',plugin_dir_url(__FILE__)."assets/js/bootstrap.min.js", array('jquery'));
wp_enqueue_script('pfm_uiJs',plugin_dir_url(__FILE__)."assets/js/ui.js");
$strings = array(
'fontName' => __('Font Name: ','pfm_fontManager'),
'addFontFile' => __('+ Add another font','pfm_fontManager'),
'customElementsLabel' => __('Custom Elements: ','pfm_fontManager'),
'customElmsHint' => __('Seprate Elements with line breaks, ','pfm_fontManager'),
'fontStyle' => __('Font Style: ','pfm_fontManager'),
'fontStetch' => __('Font Stretch: ','pfm_fontManager'),
'fontWeight' => __('Font Weight: ','pfm_fontManager'),
'elements' => __('Elements: ','pfm_fontManager'),
'elementsHint' => __('hold ctrl or shift (or drag with the mouse) to select more than one ','pfm_fontManager'),
'fontLoadingEror' => __('We got a problem while loading Fonts because: ','pfm_fontManager'),
'fontFile' => __('fontFile','pfm_fontManager'),
'mediaTitle' => __('Choose/Upload your font file','pfm_fontManager'),
'mediaBtn' => __('Choose Font','pfm_fontManager'),
'fontFileLabel' => __('Font Url: ','pfm_fontManager'),
'chooseFileBtn' => __('Choose Font','pfm_fontManager'),
'emptyFileInputErorr' => __('We cannot save your fonts because some of font urls was empty!','pfm_fontManager'),
'fontSavedSuccess' => __('Your Fonts Setting Saved Successfully!','pfm_fontManager'),
'fontSavingErorr' => __('We got a problem while Saving Fonts because: ','pfm_fontManager'),
'nounce' => wp_create_nonce('pfm-fontManager'),
);
$string = wp_localize_script('pfm_uiJs','pfm_strings',$strings);
wp_localize_script( 'pfm_ajaxScript', 'ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ), 'we_value' => 1234 ) );
}
function FontManagerLocal()
{
load_plugin_textdomain( 'pfm_fontManager', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
function FontManagerMenu()
{
add_theme_page(
__('Font Manager','pfm_fontManager')
,__('Font Manager','pfm_fontManager')
,'manage_options'
,'pfm_fontManager-admin'
,array($this->adminPage,'AdminView')
);
/*add_theme_page(
'debug'
,'debug'
,'manage_options'
,'debug'
,array($this,'debug')
);*/
}
function FontManagerSaveFont()
{
check_ajax_referer( 'pfm-fontManager', 'nnce' );
$fonts = $_POST['fonts'];
$sanitFonts = [];
foreach ($fonts as $font) {
$urls = $customs = $elements = [];
foreach ($font['src'] as $url) {
$urls[] = esc_url_raw($url);
}
foreach ($font['custom'] as $custom) {
$customs[] = sanitize_text_field($custom);
}
foreach ($font['elements'] as $element) {
$elements = sanitize_text_field($element);
}
$sanitFonts[] = [
'name' => sanitize_text_field($font['name']),
'style' => sanitize_text_field($font['style']),
'stretch' => sanitize_text_field($font['stretch']),
'weight' => sanitize_text_field($font['weight']),
'elements' => $elements,
'src' => $urls,
'custom' => $customs
];
}
$result = update_option("pfm_fonts",$sanitFonts,null);
}
function FontManagerLoadFont()
{
$fontsFromDb = get_option('pfm_fonts',array());
wp_send_json($fontsFromDb);
}
function AddCustomUploadMimes($existing_mimes) {
$existing_mimes['otf'] = 'application/x-font-otf';
$existing_mimes['woff'] = 'application/x-font-woff';
$existing_mimes['ttf'] = 'application/x-font-ttf';
$existing_mimes['svg'] = 'image/svg+xml';
$existing_mimes['eot'] = 'application/vnd.ms-fontobject';
return $existing_mimes;
}
function LoadCssInHead()
{
$css = $this->CreateCss();
wp_enqueue_style('pfm_FontsCSS',get_stylesheet_directory_uri() );
wp_add_inline_style('pfm_FontsCSS',$css);
}
function CreateCss()
{
$json = get_option('pfm_fonts',array());
$css = "";
foreach ($json as $font) {
$css .= "@font-face{";
$css .= "font-family:".$font['name'].";";
$css .= "font-stretch:".$font['stretch'].";";
$css .= "font-style:".$font['style'].";";
$css .="font-weight:".$font['weight'].";";
$css .="src:";
//$urlsCount = count($font['src']);
//$counter = 0;
foreach ($font['src'] as $url) {
$type = explode('.',$url)[1];
switch ($type) {
case 'eot':
$css .= "url('".$url."'),";
$css .= "url('".$url."?#iefix') format('embedded-opentype'),";
break;
case 'woff':
$css .= "url('".$url."') format('woff'),";
break;
case 'woff2':
$css .= "url('".$url."') format('woff2'),";
break;
case 'ttf':
$css .= "url('".$url."') format('truetype'),";
break;
case 'svg':
$css .= "url('".$url."#svgFontName') format('svg'),";
break;
default:
// code...
break;
}
}
$css = preg_replace("/\,$/","",$css).";";
$css .="}";
if($font['elements'] != ''){
if(is_array($font['elements'])){
$css .= implode(',',$font['elements'])."{";
}else {
$css .= $font['elements']."{";
}
$css .= "font-family:".$font['name']." !important;}";
}
if($font['custom'] != ''){
if(is_array($font['custom'])){
$css .= implode(',',$font['custom'])."{";
}else {
$css .= $font['custom']."{";
}
$css .= "font-family:".$font['name']." !important;}";
}
}
return $css;
}
function debug()
{
}
}
add_action("plugins_loaded","FontManagerInit");
function FontManagerInit()
{
$fontManager = new PFM_FontManager();
}
?>