Skip to content

Commit cc3dabb

Browse files
committed
move gfonts_preview to WP uploads folder
1 parent 0cf258a commit cc3dabb

File tree

3 files changed

+86
-52
lines changed

3 files changed

+86
-52
lines changed

bibleget-io.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,17 @@ function BibleGet_on_uninstall() {
143143
//echo 'cacheNotFlushed';
144144
}
145145
*/
146+
if (get_filesystem_method() === 'direct') {
147+
$gfontsDir = str_replace('\\','/', wp_upload_dir()["basedir"] ) . "/gfonts_preview/";
148+
$creds = request_filesystem_credentials(site_url() . '/wp-admin/', '', false, false, array());
149+
/* initialize the API */
150+
if (WP_Filesystem($creds)) {
151+
global $wp_filesystem;
152+
if ($wp_filesystem->is_dir($gfontsDir)) {
153+
$wp_filesystem->rmdir($gfontsDir,true);
154+
}
155+
}
156+
}
146157
}
147158

148159

@@ -666,10 +677,10 @@ function bibleGetGutenbergScripts($hook) {
666677
wp_enqueue_style('fontawesome', '//maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css', false, '4.7.0');
667678
}
668679
}
669-
if( file_exists( plugin_dir_path( __FILE__ ) . 'css/gfonts_preview/gfonts_preview.css' ) ){
670-
wp_enqueue_style( 'bibleget-fontselect-preview',
671-
plugins_url ('css/gfonts_preview/gfonts_preview.css', __FILE__ )
672-
);
680+
$gfontsPreviewCSS = str_replace('\\','/', wp_upload_dir()["basedir"] ) . "/gfonts_preview/css/gfonts_preview.css";
681+
$gfontsPreviewURL = wp_upload_dir()["baseurl"] . "/gfonts_preview/css/gfonts_preview.css";
682+
if( file_exists( $gfontsPreviewCSS ) ){
683+
wp_enqueue_style( 'bibleget-fontselect-preview', $gfontsPreviewURL );
673684
}
674685
}
675686

includes/BibleGetSettingsPage.php

Lines changed: 67 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ public function getBibleBookNamesInLang($lang = null)
181181
if ($lang === null) {
182182
$lang = $this->locale;
183183
}
184-
if (strlen($lang) == 2) {
184+
if (strlen($lang) === 2) {
185185
//we have a two-letter ISO code, we need to get the full language name in English
186186
if (extension_loaded('intl') === true) {
187187
$lang = Locale::getDisplayLanguage($lang, "en");
@@ -264,7 +264,7 @@ public function register_settings()
264264

265265
public function admin_print_styles($hook)
266266
{
267-
if ($hook == 'settings_page_bibleget-settings-admin') {
267+
if ($hook === 'settings_page_bibleget-settings-admin') {
268268
wp_enqueue_style('admin-css', plugins_url('../css/admin.css', __FILE__));
269269
}
270270
}
@@ -288,35 +288,40 @@ public function admin_print_scripts($hook)
288288
wp_localize_script('admin-js', 'bibleGetOptionsFromServer', $obj);
289289
wp_enqueue_script('admin-js');
290290

291-
if ($this->gfontsAPIkeyCheckResult == "SUCCESS") {
291+
if ($this->gfontsAPIkeyCheckResult === "SUCCESS") {
292292
//We only want the transient to be set from the bibleget settings page, so we wait until now
293293
// instead of doing it in the gfontsAPIkeyCheck (which is called on any admin interface)
294294
set_transient(md5($this->options['googlefontsapi_key']), $this->gfontsAPIkeyCheckResult, 90 * 24 * HOUR_IN_SECONDS); // 90 giorni
295295

296-
$plugin_path = "";
297296
// bibleGetWriteLog("about to initialize creation of admin page...");
298297
if (get_filesystem_method() === 'direct') {
299-
$creds = request_filesystem_credentials(site_url() . '/wp-admin/', '', false, false, array());
298+
$gfontsDir = str_replace('\\','/', wp_upload_dir()["basedir"] ) . "/gfonts_preview/";
299+
$creds = request_filesystem_credentials(site_url() . '/wp-admin/', '', false, false, array());
300300
/* initialize the API */
301301
if (WP_Filesystem($creds)) {
302302
global $wp_filesystem;
303-
$plugin_path = str_replace('\\','/', plugin_dir_path( __FILE__ ) );
304-
if (!$wp_filesystem->is_dir($plugin_path . '../gfonts_preview/')) {
303+
if (!$wp_filesystem->is_dir($gfontsDir)) {
305304
/* directory didn't exist, so let's create it */
306-
if ($wp_filesystem->mkdir($plugin_path . '../gfonts_preview/') === false) {
305+
if ($wp_filesystem->mkdir($gfontsDir) === false) {
307306
$this->gfontsAPI_errors[] = "Could not create directory gfonts_preview";
308-
}
309-
}
310-
if (!$wp_filesystem->is_dir($plugin_path . '../css/gfonts_preview/')) {
311-
/* directory didn't exist, so let's create it */
312-
if ($wp_filesystem->mkdir($plugin_path . '../css/gfonts_preview/') === false) {
313-
$this->gfontsAPI_errors[] = "Could not create directory css/gfonts_preview";
307+
} else {
308+
//let's make sure the necessary subfolders are also created
309+
if(!$wp_filesystem->is_dir($gfontsDir . "ttf/")) {
310+
if( $wp_filesystem->mkdir($gfontsDir . "ttf/") === false ) {
311+
$this->gfontsAPI_errors[] = "Could not create directory gfonts_preview/ttf";
312+
}
313+
}
314+
if(!$wp_filesystem->is_dir($gfontsDir . "css/")) {
315+
if( $wp_filesystem->mkdir($gfontsDir . "css/") === false ) {
316+
$this->gfontsAPI_errors[] = "Could not create directory gfonts_preview/css";
317+
}
318+
}
314319
}
315320
}
316321

317322
//let's also cache the results from the Google Fonts API in a local file so we don't have to keep calling
318323
if ($wp_filesystem->put_contents(
319-
$plugin_path . '../gfonts_preview/gfontsWeblist.json',
324+
$gfontsDir . 'gfontsWeblist.json',
320325
json_encode($this->gfonts_weblist),
321326
FS_CHMOD_FILE // predefined mode settings for WP files
322327
) === false) {
@@ -329,7 +334,19 @@ public function admin_print_scripts($hook)
329334
$this->gfontsAPI_errors[] = "You do not have direct access permissions to the wordpress filesystem";
330335
}
331336
if(count($this->gfontsAPI_errors) > 0 ){
332-
add_action( 'admin_notices', function(){ printf( '<div class="%1$s"><p>%2$s</p></div>', esc_attr( 'notice notice-error' ), esc_html( __( 'Impossible to write data to the BibleGet plugin directory, please check permissions!', 'bibleget-io' ) ) ); } );
337+
add_action(
338+
'admin_notices',
339+
function(){
340+
printf(
341+
'<div class="%1$s"><p>%2$s</p></div>',
342+
esc_attr( 'notice notice-error' ),
343+
esc_html(
344+
__( 'Impossible to write data to the BibleGet plugin directory, please check permissions!', 'bibleget-io' )
345+
. "\n" . implode("\n",$this->gfontsAPI_errors)
346+
)
347+
);
348+
}
349+
);
333350
}
334351
wp_enqueue_script('jquery-ui-progressbar');
335352
if (!wp_style_is('jquery-ui-css', 'registered') || !wp_style_is('jquery-ui-css', 'enqueued')) {
@@ -570,7 +587,7 @@ public function googlefontsapikey_callback()
570587
/* translators: refers to the outcome of the validity check of the Google Fonts API key */
571588
echo '<span style="color:DarkViolet;font-weight:bold;margin-left:12px;">' . __("CURL ERROR WHEN SENDING REQUEST", "bibleget-io") . '</span><br />';
572589
foreach ($this->gfontsAPI_errors as $er) {
573-
if ($er == 403) {
590+
if ($er === 403) {
574591
echo '<br /><i style="color:DarkViolet;margin-left:12px;">';
575592
echo __("This server's IP address has not been given access to the Google Fonts API using this key.", "bibleget-io");
576593
echo " " . __("Please verify that access has been given to the correct IP addresses.", "bibleget-io");
@@ -658,12 +675,12 @@ public function gfontsAPIkeyCheck() {
658675
}
659676
$response = curl_exec($ch);
660677
$status = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
661-
if ($response && !curl_errno($ch) && $status == 200) {
678+
if ($response && !curl_errno($ch) && $status === 200) {
662679
//let's see what was returned, and if it's what we're looking for
663680
$json_response = json_decode($response);
664681
if ($json_response !== null && json_last_error() === JSON_ERROR_NONE) {
665682
//So far so good, let's keep these results for other functions to access
666-
if (property_exists($json_response, "kind") && $json_response->kind == "webfonts#webfontList" && property_exists($json_response, "items")) {
683+
if (property_exists($json_response, "kind") && $json_response->kind === "webfonts#webfontList" && property_exists($json_response, "items")) {
667684
$this->gfonts_weblist = $json_response;
668685
$result = "SUCCESS";
669686
}
@@ -679,7 +696,7 @@ public function gfontsAPIkeyCheck() {
679696
$this->gfontsAPI_errors[] = curl_error($ch);
680697
}
681698
if ($status != 200) {
682-
if ($status == 403) {
699+
if ($status === 403) {
683700
$this->gfontsAPI_errors[] = $status;
684701
} else {
685702
/* translators: refers to the status of the http response during communication with the Google Fonts API */
@@ -722,17 +739,25 @@ public function store_gfonts_preview()
722739
$familyurlname = "";
723740
$familyfilename = "";
724741
$errorinfo = [];
725-
$gfontsDir = str_replace('\\','/', plugin_dir_path( __FILE__ ) ) . "../gfonts_preview/";
742+
$gfontsDir = str_replace('\\','/', wp_upload_dir()["basedir"] ) . "/gfonts_preview/";
743+
$gfontsWeblistFile = $gfontsDir . "gfontsWeblist.json";
726744
$gfontsWeblist = new stdClass();
727745
$returnInfo = new stdClass();
728746

729-
if (file_exists($gfontsDir . "gfontsWeblist.json")) {
730-
$gfontsWeblistFile = file_get_contents($gfontsDir . "gfontsWeblist.json");
731-
$gfontsWeblist = json_decode($gfontsWeblistFile);
747+
if (file_exists($gfontsWeblistFile)) {
748+
$gfontsWeblistFileContents = file_get_contents($gfontsWeblistFile);
749+
$gfontsWeblist = json_decode($gfontsWeblistFileContents);
732750
}
733751
if (
734-
isset($_POST["gfontsCount"], $_POST["batchLimit"], $_POST["startIdx"], $_POST["lastBatchLimit"], $_POST["numRuns"], $_POST["currentRun"])
735-
&& property_exists($gfontsWeblist, "items")) {
752+
isset(
753+
$_POST["gfontsCount"],
754+
$_POST["batchLimit"],
755+
$_POST["startIdx"],
756+
$_POST["lastBatchLimit"],
757+
$_POST["numRuns"],
758+
$_POST["currentRun"]
759+
) && property_exists($gfontsWeblist, "items"))
760+
{
736761
//$gfontsCount = intval($_POST["gfontsCount"]);
737762
$batchLimit = intval($_POST["batchLimit"]);
738763
$startIdx = intval($_POST["startIdx"]);
@@ -747,13 +772,11 @@ public function store_gfonts_preview()
747772
wp_die();
748773
}
749774

750-
$plugin_path = "";
751775
if (get_filesystem_method() === 'direct') {
752776
$creds = request_filesystem_credentials(site_url() . '/wp-admin/', '', false, false, array());
753777
/* initialize the API */
754778
if (WP_Filesystem($creds)) {
755779
global $wp_filesystem;
756-
$plugin_path = str_replace('\\','/', plugin_dir_path( __FILE__ ) );
757780

758781
foreach ($gfontsWeblist->items as $idx => $googlefont) {
759782
if ($idx >= $startIdx && $idx < ($startIdx + $batchLimit)) {
@@ -763,7 +786,7 @@ public function store_gfonts_preview()
763786
$errorinfo[] = "Now dealing with font-family " . $thisfamily;
764787
$fnttype = 'ttf'; //'woff', 'woff2', 'ttf'
765788

766-
if (!file_exists($gfontsDir . "{$familyfilename}.{$fnttype}")) { //$idx < $idxlimit &&
789+
if (!file_exists($gfontsDir . "ttf/{$familyfilename}.{$fnttype}")) { //$idx < $idxlimit &&
767790
$ch2 = curl_init("https://fonts.googleapis.com/css2?family={$familyurlname}&text={$familyfilename}");
768791
curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, TRUE);
769792
curl_setopt($ch2, CURLOPT_SSL_VERIFYHOST, 2);
@@ -779,12 +802,12 @@ public function store_gfonts_preview()
779802
$response2 = curl_exec($ch2);
780803
$status2 = (int) curl_getinfo($ch2, CURLINFO_HTTP_CODE);
781804
$returnInfo->httpStatus2 = $status2;
782-
if ($response2 && !curl_errno($ch2) && $status2 == 200) {
783-
if (preg_match('/url\((.*?)\)/', $response2, $match) == 1) {
805+
if ($response2 && !curl_errno($ch2) && $status2 === 200) {
806+
if (preg_match('/url\((.*?)\)/', $response2, $match) === 1) {
784807
$thisfonturl = $match[1];
785808
$errorinfo[] = "font retrieval url for {$thisfamily} = {$thisfonturl}";
786809

787-
// $ch3_headers = [];
810+
//$ch3_headers = [];
788811
$ch3 = curl_init($thisfonturl);
789812
curl_setopt($ch3, CURLOPT_SSL_VERIFYPEER, TRUE);
790813
curl_setopt($ch3, CURLOPT_SSL_VERIFYHOST, 2);
@@ -801,27 +824,28 @@ public function store_gfonts_preview()
801824
curl_setopt($ch3, CURLOPT_AUTOREFERER, TRUE);
802825
}
803826
$response3 = curl_exec($ch3);
804-
// $errorinfo[] = print_r($ch3_headers,TRUE);
827+
//$errorinfo[] = print_r($ch3_headers,TRUE);
805828
$status3 = (int) curl_getinfo($ch3, CURLINFO_HTTP_CODE);
806829
$returnInfo->httpStatus3 = $status3;
807-
if ($response3 && !curl_errno($ch3) && $status3 == 200) {
830+
if ($response3 && !curl_errno($ch3) && $status3 === 200) {
808831
if ($wp_filesystem) {
809-
//if(!file_exists($plugin_path . "../gfonts_preview/{$familyfilename}.{$fnttype}") ){
832+
//if(!file_exists($gfontsDir . "ttf/{$familyfilename}.{$fnttype}") ){
810833
if (!$wp_filesystem->put_contents(
811-
$gfontsDir . "{$familyfilename}.{$fnttype}",
834+
$gfontsDir . "ttf/{$familyfilename}.{$fnttype}",
812835
$response3,
813836
FS_CHMOD_FILE
814837
)) {
815-
$errorinfo[] = "Cannot write file " . $plugin_path . "../gfonts_preview/{$familyfilename}.{$fnttype} with wordpress filesystem api, sorry";
838+
$errorinfo[] = "Cannot write file " . $gfontsDir . "ttf/{$familyfilename}.{$fnttype} with wordpress filesystem api, sorry";
816839
} else {
817-
$gfont_stylesheet = preg_replace('/url\((.*?)\)/', 'url(' . esc_url(plugins_url("../gfonts_preview/{$familyfilename}.{$fnttype}", __FILE__)) . ')', $response2);
818-
if (!file_exists($plugin_path . "../css/gfonts_preview/{$familyfilename}.css")) {
840+
$uploadURL = wp_upload_dir()["baseurl"];
841+
$gfont_stylesheet = preg_replace('/url\((.*?)\)/', 'url(' . esc_url("{$uploadURL}/gfonts_preview/ttf/{$familyfilename}.{$fnttype}") . ')', $response2);
842+
if (!file_exists($gfontsDir . "css/{$familyfilename}.css")) {
819843
if (!$wp_filesystem->put_contents(
820-
$plugin_path . "../css/gfonts_preview/{$familyfilename}.css",
844+
$gfontsDir . "css/{$familyfilename}.css",
821845
$gfont_stylesheet,
822846
FS_CHMOD_FILE
823847
)) {
824-
$errorinfo[] = "Cannot write file " . $plugin_path . "../css/gfonts_preview/{$familyfilename}.css with wordpress filesystem api, sorry";
848+
$errorinfo[] = "Cannot write file " . $gfontsDir . "css/{$familyfilename}.css with wordpress filesystem api, sorry";
825849
}
826850
}
827851
}
@@ -862,17 +886,16 @@ public function store_gfonts_preview()
862886
}
863887

864888

865-
// echo print_r($errorinfo);
889+
//echo print_r($errorinfo);
866890
if (($startIdx + ($batchLimit - 1)) < ($totalFonts - 1)) {
867891
$returnInfo->state = "RUN_PROCESSED";
868892
$returnInfo->run = $currentRun;
869893
} else {
870894
$returnInfo->state = "COMPLETE";
871895

872896
//LAST STEP IS TO MINIFY ALL OF THE CSS FILES INTO ONE SINGLE FILE
873-
$cssdirectory = str_replace('\\','/', plugin_dir_path( __FILE__ ) ) . "../css/gfonts_preview";
897+
$cssdirectory = $gfontsDir . "css";
874898
$cssfiles = array_diff(scandir($cssdirectory), array('..', '.', 'gfonts_preview.css'));
875-
876899
$minifier = new MatthiasMullie\Minify\CSS($cssdirectory . "/" . (array_shift($cssfiles)));
877900
while (count($cssfiles) > 0) {
878901
$minifier->add($cssdirectory . "/" . (array_shift($cssfiles)));

includes/custom_controls.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,10 @@ public function enqueue() {
8989
plugins_url ( '../css/fontselect-control.css', __FILE__ ) // Define the path to the CSS file
9090
);
9191

92-
if( file_exists( plugin_dir_path( __FILE__ ) . '../css/gfonts_preview/gfonts_preview.css' ) ){
93-
wp_enqueue_style( 'bibleget-fontselect-preview',
94-
plugins_url ('../css/gfonts_preview/gfonts_preview.css', __FILE__ )
95-
);
92+
$gfontsDir = str_replace('\\','/', wp_upload_dir()["basedir"] ) . "/gfonts_preview/";
93+
$gfontsPreviewCSS = esc_url( wp_upload_dir()["baseurl"] . '/gfonts_preview/css/gfonts_preview.css' );
94+
if( file_exists( $gfontsDir . 'css/gfonts_preview.css' ) ){
95+
wp_enqueue_style( 'bibleget-fontselect-preview', $gfontsPreviewCSS );
9696
}
9797
else{
9898
echo '<!-- gfonts_preview.css not found -->';

0 commit comments

Comments
 (0)