add: php composer libraries

This commit is contained in:
2024-11-08 14:12:30 -03:00
parent 5e8c46bf3b
commit 0086d8202b
5 changed files with 452 additions and 2 deletions

View File

@@ -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
]
]
);
);
/**
* Load custom block styles
*/
CustomBlockStyles::enqueueStyles();

View File

@@ -0,0 +1,144 @@
<?php
/**
* Set media sizes in Wordpress
*
* PHP VERSION 8
*
* @package HackeamosOrg/HackeamosWP/CustomBlockStyles
* @author Lucilio Correia <lucilio@lucilio.net>
* @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 <lucilio@lucilio.net>
* @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
);
}
}
}

View File

@@ -0,0 +1,80 @@
<?php
/**
* Set site favicon if none is defined yet on database
*
* PHP VERSION 8
*
* @package HackeamosOrg/HackeamosWP/MediaSizes
* @author Lucilio Correia <lucilio@lucilio.net>
* @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 <lucilio@lucilio.net>
* @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;
}
}

View File

@@ -0,0 +1,108 @@
<?php
/**
* Set media sizes in Wordpress
*
* PHP VERSION 8
*
* @package HackeamosOrg/HackeamosWP/MediaSizes
* @author Lucilio Correia <lucilio@lucilio.net>
* @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 <lucilio@lucilio.net>
* @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(
'<img src="%s" alt="%s"',
self::$defaultLogoUrl,
get_bloginfo('name', 'display')
);
if (! $unlink_homepage_logo) {
$logoHtml=sprintf(
'<a href="%s">%s</a>',
get_home_url(),
$logoHtml
);
}
}
return $logoHtml;
}
}

View File

@@ -0,0 +1,93 @@
<?php
/**
* Set site favicon if none is defined yet on database
*
* PHP VERSION 8
*
* @package HackeamosOrg/HackeamosWP/MediaSizes
* @author Lucilio Correia <lucilio@lucilio.net>
* @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 <lucilio@lucilio.net>
* @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;
}
}