Example #1
0
function saveGoogleFontClasses($fontClasses)
{
    global $motopressCESettings;
    clearstatcache();
    $error = motopress_check_google_font_dir_permissions(true);
    if (!isset($error['error'])) {
        $prefix = $motopressCESettings['google_font_classes_prefix'];
        $oldFontClasses = get_option('motopress_google_font_classes', array());
        //remove unused files
        $removeClasses = array_diff_key($oldFontClasses, $fontClasses);
        foreach ($removeClasses as $removeClass) {
            if (isset($removeClass['file']) && file_exists($motopressCESettings['google_font_classes_dir'] . $removeClass['file'])) {
                if (is_writable($motopressCESettings['google_font_classes_dir'] . $removeClass['file'])) {
                    unlink($motopressCESettings['google_font_classes_dir'] . $removeClass['file']);
                    clearstatcache();
                }
            }
        }
        foreach ($fontClasses as $fontClassName => $fontClass) {
            if (isset($oldFontClasses[$fontClassName]) && $oldFontClasses[$fontClassName]['family'] === $fontClass['family'] && (isset($oldFontClasses[$fontClassName]['variants']) && isset($fontClass['variants']) && $oldFontClasses[$fontClassName]['variants'] == $fontClass['variants'] || !isset($oldFontClasses[$fontClassName]['variants']) && !isset($fontClass['variants'])) && (isset($oldFontClasses[$fontClassName]['subsets']) && isset($fontClass['subsets']) && $oldFontClasses[$fontClassName]['subsets'] == $fontClass['subsets'] || !isset($oldFontClasses[$fontClassName]['subsets']) && !isset($fontClass['subsets']))) {
                $fontClasses[$fontClassName] = $oldFontClasses[$fontClassName];
            } else {
                $importFamily = str_replace(' ', '+', $fontClass['family']);
                $importSubsets = '';
                $importVariants = '';
                if (isset($fontClass['subsets'])) {
                    $importSubsets = '&subset=' . join(',', $fontClass['subsets']);
                }
                if (isset($fontClass['variants'])) {
                    $importVariants = ':' . join(',', $fontClass['variants']);
                }
                $content = '@import url(\'http://fonts.googleapis.com/css?family=' . $importFamily . $importVariants . $importSubsets . '\');' . "\n";
                $content .= '.' . $prefix . $fontClassName . ' *{' . 'font-family: ' . $fontClass['family'] . ';' . '}' . "\n";
                if (isset($fontClass['variants'])) {
                    foreach ($fontClass['variants'] as $variant) {
                        $fontStyle = stripos($variant, 'italic') !== false ? 'font-style:italic !important;' : 'font-style:normal !important;';
                        $emFontStyle = 'font-style:italic !important;';
                        $weight = preg_replace('/\\D/', '', $variant);
                        if ($weight == '') {
                            $weight = '400';
                        }
                        if ($weight < 400) {
                            $strongFontWeight = ' font-weight: 400 !important;';
                        } else {
                            $strongFontWeight = ' font-weight: 700 !important;';
                        }
                        $fontWeight = 'font-weight:' . $weight . ' !important;';
                        $content .= '.' . $prefix . $fontClassName . '-' . $variant . ' *{' . 'font-family : ' . $fontClass['family'] . ';}' . '.' . $prefix . $fontClassName . '-' . $variant . ' *{' . $fontStyle . $fontWeight . '}' . '.' . $prefix . $fontClassName . '-' . $variant . ' strong{' . $strongFontWeight . '}' . '.' . $prefix . $fontClassName . '-' . $variant . ' em{' . $emFontStyle . '}' . "\n";
                    }
                }
                $fontClasses[$fontClassName]['css'] = $content;
                $fontClasses[$fontClassName]['fullname'] = $prefix . $fontClassName;
                $filename = $fontClassName . '.css';
                if (false !== file_put_contents($motopressCESettings['google_font_classes_dir'] . $filename, $content)) {
                    $fontClasses[$fontClassName]['file'] = $filename;
                } else {
                    unset($fontClasses[$fontClassName]);
                }
            }
        }
        update_option('motopress_google_font_classes', $fontClasses);
    }
}
function motopress_google_font_not_writable_notice()
{
    global $motopressCELang;
    $error = motopress_check_google_font_dir_permissions();
    if (isset($error['error'])) {
        echo '<div class="error"><p>' . $motopressCELang->CENoticeDefaultGoogleFontError . '</p><p>' . $error['error'] . '</p></div>';
    }
}