add: php composer management
This commit is contained in:
120
src/HackeamosWP/MediaSizes.php
Normal file
120
src/HackeamosWP/MediaSizes.php
Normal file
@@ -0,0 +1,120 @@
|
||||
<?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 reasonable media sizes
|
||||
*
|
||||
* @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
|
||||
*/
|
||||
class MediaSizes extends SingletonPattern {
|
||||
|
||||
/**
|
||||
* Set media sizes
|
||||
*
|
||||
* @param array $mediaSizes {
|
||||
* @type array $name=>$mediaSize {
|
||||
* @name string $name The array key, internal name of
|
||||
* media size only letters, numbers
|
||||
* - and _ are allowed.
|
||||
* @type string $ize_w Witdh of the media
|
||||
* @type string $ize_h Height of the media
|
||||
* @type string $crop Whether cropping or not the media
|
||||
* @type string $caption Optional. A human readable name
|
||||
* for representing the size on UI.
|
||||
* Defaults to the $name replacing -
|
||||
* and _ by spaces and capitalizing
|
||||
* each resulting word
|
||||
* }
|
||||
* }
|
||||
*/
|
||||
static function setMediaSizes(array $mediaSizes)
|
||||
{
|
||||
/**
|
||||
* Builtin sizes should be:
|
||||
* - thumbnail,
|
||||
* - medium,
|
||||
* - medium_large,
|
||||
* - large
|
||||
*/
|
||||
$builtinMediaSizeNames=array_merge(
|
||||
get_intermediate_image_sizes(),
|
||||
[
|
||||
'post-thumbnail'
|
||||
]
|
||||
);
|
||||
/**
|
||||
* Loop into user media sizes and set them up
|
||||
*/
|
||||
foreach ($mediaSizes as $name => $sizes) {
|
||||
if ($name == 'post-thumbnail') {
|
||||
add_action(
|
||||
'after_setup_theme',
|
||||
function () {
|
||||
add_theme_support('post-thumbnails');
|
||||
},
|
||||
10,
|
||||
1
|
||||
);
|
||||
}
|
||||
if (isset($sizes['caption'])) {
|
||||
$caption=$sizes['caption'];
|
||||
}
|
||||
if (in_array($name, $builtinMediaSizeNames)) {
|
||||
if (isset($sizes['size_w']) and isset($sizes['size_h'])) {
|
||||
foreach ($sizes as $property => $value) {
|
||||
update_option(
|
||||
$name . '_' . $property,
|
||||
$value
|
||||
);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
add_action(
|
||||
'after_setup_theme',
|
||||
function () use ($name, $sizes) {
|
||||
add_image_size(
|
||||
$name,
|
||||
$sizes['size_w'],
|
||||
$sizes['size_h'],
|
||||
$sizes['crop']
|
||||
);
|
||||
},
|
||||
10,
|
||||
1
|
||||
);
|
||||
if (!isset($caption)) {
|
||||
$caption=implode(
|
||||
' ',
|
||||
array_map(
|
||||
'ucfirst',
|
||||
preg_split('/[-_]/', $name)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
if (isset($caption)) {
|
||||
add_filter(
|
||||
'image_size_names_choose',
|
||||
function ($sizes) use ($caption, $name) {
|
||||
$sizes[$name]=$caption;
|
||||
return $sizes;
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
84
src/SingletonPattern.php
Normal file
84
src/SingletonPattern.php
Normal file
@@ -0,0 +1,84 @@
|
||||
<?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;
|
||||
|
||||
/**
|
||||
* The Singleton class defines the `GetInstance` method that serves as an
|
||||
* alternative to constructor and lets clients access the same instance of this
|
||||
* class over and over.
|
||||
*
|
||||
* @package HackeamosOrg/SingletonPattern
|
||||
* @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
|
||||
* @see https://refactoring.guru/pt-br/design-patterns/singleton/php/example
|
||||
*/
|
||||
class SingletonPattern
|
||||
{
|
||||
/**
|
||||
* The Singleton's instance is stored in a static field. This field is an
|
||||
* array, because we'll allow our Singleton to have subclasses. Each item in
|
||||
* this array will be an instance of a specific Singleton's subclass. You'll
|
||||
* see how this works in a moment.
|
||||
*/
|
||||
private static $_instances = [];
|
||||
|
||||
/**
|
||||
* The Singleton's constructor should always be private to prevent direct
|
||||
* construction calls with the `new` operator.
|
||||
*/
|
||||
protected function __construct()
|
||||
{
|
||||
// nothing here
|
||||
}
|
||||
|
||||
/**
|
||||
* Singletons should not be cloneable.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
protected function __clone()
|
||||
{
|
||||
//nothing here
|
||||
}
|
||||
|
||||
/**
|
||||
* Singletons should not be restorable from strings.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
public function __wakeup()
|
||||
{
|
||||
throw new \Exception("Cannot unserialize a singleton.");
|
||||
}
|
||||
|
||||
/**
|
||||
* This is the static method that controls the access to the singleton
|
||||
* instance. On the first run, it creates a singleton object and places it
|
||||
* into the static field. On subsequent runs, it returns the client existing
|
||||
* object stored in the static field.
|
||||
*
|
||||
* This implementation lets you subclass the Singleton class while keeping
|
||||
* just one instance of each subclass around.
|
||||
*
|
||||
* @return SingletonPattern Unique instance of the (sub)class
|
||||
*/
|
||||
public static function getInstance(): SingletonPattern
|
||||
{
|
||||
$cls = static::class;
|
||||
if (!isset(self::$instances[$cls])) {
|
||||
self::$instances[$cls] = new static();
|
||||
}
|
||||
|
||||
return self::$instances[$cls];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user