| 1 |
<?php |
| 2 |
/*************************************************************************** |
| 3 |
* |
| 4 |
* ---------------------------------------------------------------------- |
| 5 |
* DO NOT EDIT THIS FILE |
| 6 |
* ---------------------------------------------------------------------- |
| 7 |
* |
| 8 |
* Copyright (C) Themify |
| 9 |
* |
| 10 |
* ---------------------------------------------------------------------- |
| 11 |
* |
| 12 |
***************************************************************************/ |
| 13 |
|
| 14 |
/* Database Functions |
| 15 |
/***************************************************************************/ |
| 16 |
|
| 17 |
/** |
| 18 |
* Save Data |
| 19 |
* @since 1.0.0 |
| 20 |
* @package themify |
| 21 |
*/ |
| 22 |
function themify_set_data(?array $data ):bool { |
| 23 |
if ( empty( $data )) { |
| 24 |
$data = array(); |
| 25 |
} |
| 26 |
else{ |
| 27 |
unset($data['save'],$data['page']); |
| 28 |
foreach ( $data as $name => $value ) { |
| 29 |
if ($value==='' || $value==='default' || $value==='[]') { |
| 30 |
unset( $data[$name] ); |
| 31 |
} |
| 32 |
} |
| 33 |
} |
| 34 |
if(update_option( 'themify_data', $data,false )){ |
| 35 |
themify_get_data(true); |
| 36 |
return true; |
| 37 |
} |
| 38 |
//check if it's error, because wp returns false on errors and when old value and new are the same |
| 39 |
$old=themify_get_data(); |
| 40 |
return $old===$data || maybe_serialize( $old ) === maybe_serialize( $data ); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Return cached data |
| 45 |
*/ |
| 46 |
function themify_get_data($reinit=false,$from=false):array { |
| 47 |
static $data=null; |
| 48 |
if ($data===null || $reinit!==false) { |
| 49 |
$skip_cache = defined( 'THEMIFY_SKIP_DATA_CACHE' ) && true === THEMIFY_SKIP_DATA_CACHE; |
| 50 |
if ( $skip_cache===false ) { |
| 51 |
if(defined('THEMIFY_SETTING_CACHE_DIR')){ |
| 52 |
$dir=trailingslashit(THEMIFY_SETTING_CACHE_DIR); |
| 53 |
} |
| 54 |
else{ |
| 55 |
$dir=__DIR__.DIRECTORY_SEPARATOR.'.data'.DIRECTORY_SEPARATOR; |
| 56 |
} |
| 57 |
if(is_multisite()){ |
| 58 |
$dir.=get_current_blog_id().DIRECTORY_SEPARATOR; |
| 59 |
} |
| 60 |
$prefix='themify_settings_'; |
| 61 |
$fname=$prefix.basename(dirname(__DIR__)).'_'.THEMIFY_VERSION.'_'.Themify_Enqueue_Assets::$themeVersion; |
| 62 |
$orig=$fname.'.php'; |
| 63 |
if($reinit!==true && $from!=='db' && is_file($dir.$orig)){ |
| 64 |
include $dir.$orig; |
| 65 |
if(isset($_arr)){ |
| 66 |
$data =$_arr; |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
if($data===null || $reinit===true){ |
| 71 |
$data=get_option( 'themify_data', array() ); |
| 72 |
if(empty($data)){ |
| 73 |
$data=array(); |
| 74 |
} |
| 75 |
themify_sanitize_data($data); |
| 76 |
if($reinit===false){ |
| 77 |
$data = apply_filters( 'themify_get_data', $data ); |
| 78 |
} |
| 79 |
if ( $skip_cache===false ) { |
| 80 |
$tmpName=$dir.uniqid($prefix,true).'.php'; |
| 81 |
if(is_file($dir.$orig) && !Themify_Filesystem::rename($dir.$orig,$tmpName)){ |
| 82 |
Themify_Filesystem::delete($dir.$orig); |
| 83 |
} |
| 84 |
clearstatcache(); |
| 85 |
if(Themify_Filesystem::mkdir($dir,true,0755) && is_writable($dir)){ |
| 86 |
$str="<?php if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly"; |
| 87 |
$str.=PHP_EOL.'$_arr='.var_export ($data,true).';'; |
| 88 |
//create a tmp file than rename, because the rename is atomic |
| 89 |
if (file_put_contents($tmpName, $str) && Themify_Filesystem::rename($tmpName,$dir.$orig) && ($handle = opendir($dir))) {//remove old caches |
| 90 |
while (false !== ($f = readdir($handle))) { |
| 91 |
if ($f !== '.' && $f !== '..' && $f!==$orig && strpos($f, $prefix) === 0 && pathinfo($f,PATHINFO_EXTENSION)=== 'php') { |
| 92 |
Themify_Filesystem::delete($dir . $f); |
| 93 |
} |
| 94 |
} |
| 95 |
closedir($handle); |
| 96 |
clearstatcache(); |
| 97 |
}else{ |
| 98 |
Themify_Filesystem::delete($tmpName); |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
return $data; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Abstract away normalizing the data |
| 109 |
*/ |
| 110 |
function themify_sanitize_data(array &$data ){ |
| 111 |
if ( !empty( $data )) { |
| 112 |
$html=array( 'setting-custom_css', 'setting-header_html', 'setting-footer_html', 'setting-footer_text_left', 'setting-footer_text_right', 'setting-homepage_welcome', 'setting-store_info_address' ); |
| 113 |
foreach( $data as $name => &$value ){ |
| 114 |
if ( is_object( $value ) ) { |
| 115 |
if ( false !== stripos( $name, 'field_ids' ) ) { |
| 116 |
$value = wp_json_encode( $value ); |
| 117 |
} else { |
| 118 |
continue; |
| 119 |
} |
| 120 |
} elseif ( ! is_string( $value ) ) { |
| 121 |
continue; |
| 122 |
} |
| 123 |
if ( in_array( $name,$html ,true ) |
| 124 |
|| ( false !== stripos( $name, 'setting-hooks' ) ) |
| 125 |
) { |
| 126 |
$value = str_replace( "\'", "'", $value ); |
| 127 |
} else { |
| 128 |
$value = stripslashes( $value ); |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
} |