diff --git a/functions.php b/functions.php index 5c3d6eb..33da9df 100644 --- a/functions.php +++ b/functions.php @@ -26,7 +26,24 @@ require implode( ); use \HackeamosOrg\HackeamosWP\MediaSizes; +use \HackeamosOrg\HackeamosWP\DefaultSiteLogo; +use \HackeamosOrg\HackeamosWP\DefaultFavicon; +use \HackeamosOrg\HackeamosWP\FixThemeJsonFontSizes; +use \HackeamosOrg\HackeamosWP\CustomBlockStyles; +/** + * Set the logo to this theme's file when no option is available + */ +DefaultSiteLogo::setDefaultLogoUrl( + get_theme_file_uri('assets/images/logo.svg') +); + +/** + * Set the favicon from theme assets if none is found at database + */ +DefaultFavicon::setDefaultFaviconUrl( + get_theme_file_uri('assets/images/favicon.png') +); /** * Set site's media sizes @@ -59,9 +76,17 @@ MediaSizes::setMediaSizes( 'crop' => true ], 'post-thumbnail' => [ - # null sizes avoid actually changing size, but this will add support to featured-images + /* + * null sizes avoid actually changing size, + * but this will add support to featured-images + */ 'size_w' => null, 'size_h' => null ] ] -); \ No newline at end of file +); + +/** + * Load custom block styles + */ +CustomBlockStyles::enqueueStyles(); diff --git a/src/HackeamosWP/CustomBlockStyles.php b/src/HackeamosWP/CustomBlockStyles.php new file mode 100644 index 0000000..534e447 --- /dev/null +++ b/src/HackeamosWP/CustomBlockStyles.php @@ -0,0 +1,144 @@ + + * @license GPL-3 https://www.gnu.org/licenses/gpl-3.0.pt-br.html + * @link https://hackeamos.org/projetos/HackeamosWP + */ +namespace HackeamosOrg\HackeamosWP; +use \HackeamosOrg\SingletonPattern; + +/** + * Set reasonable media sizes + * + * @package HackeamosOrg/HackeamosWP/CustomBlockStyles + * @author Lucilio Correia + * @license GPL-3 https://www.gnu.org/licenses/gpl-3.0.pt-br.html + * @link https://hackeamos.org/projetos/HackeamosWP + */ +class CustomBlockStyles extends SingletonPattern { + /** + * The path for the custom stylesheets + */ + static $stylesheetDirectory=null; + + /** + * Set the custom stylesheet directory path + * + * @param string The new path + * + * @return none + */ + static function setStylesheetDir(string $path) + { + if (! is_dir($path)) { + throw new Exception( + sptintf( + 'direcotry not found: %', + $path + ), + 1 + ); + } + self::$stylesheetDirectory=$path; + } + + /** + * Get the custom stylesheet directory path + * + * @return none + */ + static function getStylesheetDir() + { + $path=self::$stylesheetDirectory; + if (empty($path)) { + return implode( + DIRECTORY_SEPARATOR, + [ + get_stylesheet_directory(), + 'assets', + 'styles', + 'blocks' + ] + ); + } + return $path; + } + + /** + * Enqueue custom styles for blocks + * + * Enqueue a list of stylesheets conditionally when respective blocks + * are in use. + * + * @return bool Whether the enqueue was or not successful + */ + static function enqueueStyles() + { + add_action( + 'after_setup_theme', + [ + get_class(), + 'enqueueFoundStylesheets' + ] + ); + } + /** + * Enqueue a list of custom styles for blocks + * + * This method is meant to be hooked on 'after_theme_setup' + * + * @return bool Whether the enqueue was or not successful + */ + static function enqueueFoundStylesheets() + { + $stylesheetDir=self::getStylesheetDir(); + $stylesheetList=array_map( + function ($relPath) use ($stylesheetDir) { + return implode( + DIRECTORY_SEPARATOR, + [ + $stylesheetDir, + $relPath + ] + ); + }, + array_filter( + scandir($stylesheetDir), + function ($dirEntry) use ($stylesheetDir) { + return substr($dirEntry, 0, 1) != '.'; + } + ) + ); + foreach ($stylesheetList as $stylesheet) { + $block_name=str_replace( + '--', + '/', + explode( + '.', + basename($stylesheet) + )[0] + ); + $args=[ + 'handle' => 'custom-style--' . str_replace( + '/', + '--', + $block_name + ), + 'src' => str_replace( + get_stylesheet_directory(), + get_stylesheet_directory_uri(), + $stylesheet + ) + ]; + wp_enqueue_block_style( + "$block_name", + $args + ); + } + } +} \ No newline at end of file diff --git a/src/HackeamosWP/DefaultFavicon.php b/src/HackeamosWP/DefaultFavicon.php new file mode 100644 index 0000000..934b46d --- /dev/null +++ b/src/HackeamosWP/DefaultFavicon.php @@ -0,0 +1,80 @@ + + * @license GPL-3 https://www.gnu.org/licenses/gpl-3.0.pt-br.html + * @link https://hackeamos.org/projetos/HackeamosWP + */ +namespace HackeamosOrg\HackeamosWP; +use \HackeamosOrg\SingletonPattern; + +/** + * Set a default favicon if none is uploaded + * + * @package HackeamosOrg/HackeamosWP/DefaultSiteLogo + * @author Lucilio Correia + * @license GPL-3 https://www.gnu.org/licenses/gpl-3.0.pt-br.html + * @link https://hackeamos.org/projetos/HackeamosWP + */ +class DefaultFavicon extends SingletonPattern +{ + /** + * Store default custom icon URL + * + * @todo check if it's safe for multisite network + */ + protected static $defaultFaviconUrl; + + /** + * Replace icon url when it is empty + * + * @param string $defaultFaviconUrl The URL desired for the default logo. + * + * @return none + * + * @todo Check if it is network (wpmu) safe + */ + static function setDefaultFaviconUrl(string $defaultFaviconUrl) + { + // do nothing if favicon is set + $faviconId=get_option('site_icon'); + if ($faviconId) { + return true; + } + // throw error if no URL was set on class calling + if (empty($defaultFaviconUrl)) { + throw new Exception("Missing default favicon URL", 1); + } + + // keep the custom logo url + self::$defaultFaviconUrl=$defaultFaviconUrl; + + // set a filter to replace logo HTML + add_filter( + 'get_site_icon_url', + [ + get_class(), + 'getFaviconUrl' + ], + 10, + 1 + ); + } + + /** + * Debug + * + * @param string $faviconUrl Value for output. + * + * @return none + */ + static function getFaviconUrl(string $faviconUrl) + { + $defaultFaviconUrl=self::$defaultFaviconUrl; + return $defaultFaviconUrl; + } +} diff --git a/src/HackeamosWP/DefaultSiteLogo.php b/src/HackeamosWP/DefaultSiteLogo.php new file mode 100644 index 0000000..73fcfad --- /dev/null +++ b/src/HackeamosWP/DefaultSiteLogo.php @@ -0,0 +1,108 @@ + + * @license GPL-3 https://www.gnu.org/licenses/gpl-3.0.pt-br.html + * @link https://hackeamos.org/projetos/HackeamosWP + */ +namespace HackeamosOrg\HackeamosWP; +use \HackeamosOrg\SingletonPattern; + +/** + * Set a default logo if none is uploaded + * + * @package HackeamosOrg/HackeamosWP/DefaultSiteLogo + * @author Lucilio Correia + * @license GPL-3 https://www.gnu.org/licenses/gpl-3.0.pt-br.html + * @link https://hackeamos.org/projetos/HackeamosWP + */ +class DefaultSiteLogo extends SingletonPattern +{ + /** + * Store default custom logo URL + * + * @todo check if it's safe for multisite network + */ + protected static $defaultLogoUrl; + + /** + * Store default custom logo Attributes + * + * @todo check if it's safe for multisite network + */ + protected static $defaultLogoAttrs; + + /** + * Replace logo url when it is wmpty + * + * @param string $defaultLogoUrl The URL desired for the default logo. + * + * @return none + * + * @todo Check if it is network (wpmu) safe + */ + static function setDefaultLogoUrl( + string $defaultLogoUrl + ) { + // do nothing if custom logo is set + $customLogoId=get_theme_mod('custom_logo'); + if ($customLogoId) { + return true; + } + + // throw error if no URL was set on class calling + if (empty($defaultLogoUrl)) { + throw new Exception("Missing default logo URL", 1); + } + + // keep the custom logo url + self::$defaultLogoUrl=$defaultLogoUrl; + + // set a filter to replace logo HTML + add_filter( + 'get_custom_logo', + [ + get_class(), + 'getCustomLogoHtml' + ], + 10, + 1 + ); + } + + /** + * Debug + * + * @param string $logoHtml Value for output. + * + * @return none + */ + static function getCustomLogoHtml(string $logoHtml) + { + // + $unlink_homepage_logo = (bool) get_theme_support( + 'custom-logo', + 'unlink-homepage-logo' + ); + // html to filter should be empty + if (empty($logoHtml)) { + $logoHtml=sprintf( + '%s%s', + get_home_url(), + $logoHtml + ); + } + } + return $logoHtml; + } +} diff --git a/src/HackeamosWP/FixThemeJsonFontSizes.php b/src/HackeamosWP/FixThemeJsonFontSizes.php new file mode 100644 index 0000000..08605c2 --- /dev/null +++ b/src/HackeamosWP/FixThemeJsonFontSizes.php @@ -0,0 +1,93 @@ + + * @license GPL-3 https://www.gnu.org/licenses/gpl-3.0.pt-br.html + * @link https://hackeamos.org/projetos/HackeamosWP + */ +namespace HackeamosOrg\HackeamosWP; +use \HackeamosOrg\SingletonPattern; + +/** + * Set a default favicon if none is uploaded + * + * @package HackeamosOrg/HackeamosWP/DefaultSiteLogo + * @author Lucilio Correia + * @license GPL-3 https://www.gnu.org/licenses/gpl-3.0.pt-br.html + * @link https://hackeamos.org/projetos/HackeamosWP + */ +class FixThemeJsonFontSizes extends SingletonPattern +{ + /** + * Store default custom icon URL + * + * @todo check if it's safe for multisite network + */ + protected static $themeFontSizes; + + /** + * Replace icon url when it is empty + * + * @return none + * + * @todo Check if it is network (wpmu) safe + */ + static function fix() + { + add_filter( + 'wp_theme_json_data_theme', + [ + get_class(), + 'saveThemeFontSizes' + ] + ); + add_filter( + 'wp_theme_json_data_default', + [ + get_class(), + 'overwriteDefaultFontSizes' + ] + ); + } + + /** + * Save font sizes declared on theme.json file + * + * @param WP_Theme_JSON_Data $themeJsonObject Theme json object + * representation. + * + * @return WP_Theme_JSON_Data Unmodified theme.json object. + * + * @todo Check if it is network (wpmu) safe + */ + static function saveThemeFontSizes($themeJsonObject) + { + $themeJson=$themeJsonObject->get_data(); + self::$themeFontSizes=$themeJson['settings']['typography']['fontSizes']; + return $themeJsonObject; + } + + /** + * Overwrite default values with theme.json's values + * + * @param WP_Theme_JSON_Data $themeJsonObject Theme json object + * representation. + * + * @return none + * + * @todo Check if it is network (wpmu) safe + */ + static function overwriteDefaultFontSizes($themeJsonObject) + { + $themeJson=$themeJsonObject->get_data(); + $fontSizes=self::$themeFontSizes; + $debug=self::$themeFontSizes; + die(var_export(compact('themeJson', 'fontSizes', 'debug'))); + return $themeJsonObject; + } + +}