Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding multiple fixes #410

Merged
merged 2 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@ All notable changes to this project will be documented in this file.

This projects adheres to [Semantic Versioning](https://semver.org/) and [Keep a CHANGELOG](https://keepachangelog.com/).

## [8.0.3]

### Added
- Media support for svg and json.
- Optimization service class for better performance.

### Fixed
- Phpstan config fix for theme and plugin setup
- Project name variable will now be ucfirst on setup.
- Removed .git folder after the setup.

## [8.0.2]

### Fixed
Expand Down Expand Up @@ -554,6 +565,7 @@ Init setup

[Unreleased]: https://github.com/infinum/eightshift-libs/compare/main...HEAD

[8.0.3]: https://github.com/infinum/eightshift-libs/compare/8.0.2...8.0.3
[8.0.2]: https://github.com/infinum/eightshift-libs/compare/8.0.1...8.0.2
[8.0.1]: https://github.com/infinum/eightshift-libs/compare/8.0.0...8.0.1
[8.0.0]: https://github.com/infinum/eightshift-libs/compare/7.1.2...8.0.0
Expand Down
5 changes: 5 additions & 0 deletions src/Cli/AbstractCli.php
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,10 @@ public function renameUse(array $args = []): self
public function renameGeneric(string $keyName, array $args): self
{
if (isset($args[$keyName])) {
if ($keyName === self::ARG_PROJECT_NAME) {
$args[$keyName] = \ucfirst($args[$keyName]);
}

$this->fileContents = \str_replace(
$this->getArgTemplate($keyName),
$args[$keyName],
Expand Down Expand Up @@ -641,6 +645,7 @@ public function cleanUpInitialBoilerplate(string $destination): void
{
$this->cliLog('--------------------------------------------------', 'C');
$this->cliLog('Removing initial boilerplate setup files', 'C');
\shell_exec("cd {$destination} && rm -rf .git"); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.system_calls_shell_exec
\shell_exec("cd {$destination} && rm -rf .github"); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.system_calls_shell_exec
\shell_exec("cd {$destination} && rm CODE_OF_CONDUCT.md"); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.system_calls_shell_exec
\shell_exec("cd {$destination} && rm CHANGELOG.md"); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.system_calls_shell_exec
Expand Down
2 changes: 2 additions & 0 deletions src/Cli/Cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
use EightshiftLibs\Media\RegenerateWebPMediaCli;
use EightshiftLibs\Media\UseWebPMediaCli;
use EightshiftLibs\Misc\VersionCli;
use EightshiftLibs\ModifyAdminAppearance\OptimizationCli;
use EightshiftLibs\Plugin\PluginCli;
use EightshiftLibs\Readme\ReadmeCli;
use EightshiftLibs\Rest\Routes\LoadMore\LoadMoreRouteCli;
Expand Down Expand Up @@ -122,6 +123,7 @@ class Cli
MediaCli::class,
MenuCli::class,
ModifyAdminAppearanceCli::class,
OptimizationCli::class,
ReadmeCli::class,
FieldCli::class,
RouteCli::class,
Expand Down
2 changes: 1 addition & 1 deletion src/Helpers/ObjectHelperTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ trait ObjectHelperTrait
*
* @return boolean
*/
public function isValidXml(string $xml)
public static function isValidXml(string $xml)
{
\libxml_use_internal_errors(true);
$doc = new DOMDocument('1.0', 'utf-8');
Expand Down
6 changes: 6 additions & 0 deletions src/Init/plugin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Project Specific
/public
/vendor
/vendor-prefixed
/node_modules
.DS_Store
2 changes: 1 addition & 1 deletion src/Init/plugin/phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ parameters:
- '#^Variable \$renderContent might not be defined\.#'
- '#^Variable \$this might not be defined\.#'
- '#^Variable \$templatePath might not be defined\.#'
checkGenericClassInNonGenericObjectType: false
- identifier: missingType.generics
6 changes: 6 additions & 0 deletions src/Init/theme/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Project Specific
/public
/vendor
/vendor-prefixed
/node_modules
.DS_Store
2 changes: 1 addition & 1 deletion src/Init/theme/phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ parameters:
- '#^Variable \$renderContent might not be defined\.#'
- '#^Variable \$this might not be defined\.#'
- '#^Variable \$templatePath might not be defined\.#'
checkGenericClassInNonGenericObjectType: false
- identifier: missingType.generics
127 changes: 127 additions & 0 deletions src/Media/AbstractMedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
namespace EightshiftLibs\Media;

use EightshiftLibs\Services\ServiceInterface;
use EightshiftLibs\Helpers\Helpers;
use Exception;
use SimpleXMLElement;
use WP_Error;
use WP_Post;

/**
* Abstract class Media class.
Expand Down Expand Up @@ -73,6 +78,128 @@ public function deleteWebPMedia(int $attachmentId): void
$this->deleteWebPMediaAllSizes($attachmentId);
}

/**
* Enable additional uploads in media.
*
* @param array<object|string> $mimes Load all mimes types.
* @return array<object|string> Return original and updated.
*/
public function enableMimeTypes(array $mimes): array
{
$mimes['svg'] = 'image/svg+xml';
$mimes['json'] = 'application/json';
return $mimes;
}

/**
* Enable SVG preview in Media Library.
*
* @param array<mixed> $response Array of prepared attachment data.
* @param int|object $attachment Attachment ID or object.
* @return array<object>|false Array of attachment details, or void if the parameter does not correspond to an attachment.
*/
public function enableSvgMediaLibraryPreview($response, $attachment)
{
if ($response['type'] === 'image' && $response['subtype'] === 'svg+xml' && \class_exists('SimpleXMLElement')) {
try {
$path = \get_attached_file($attachment instanceof WP_Post ? $attachment->ID : $attachment);

if (\file_exists($path)) {
$svgContent = \file($path);
$svgContent = \implode(' ', $svgContent);

if (!Helpers::isValidXml($svgContent)) {
// Translators: %s represents the filename, eg. demo.json.
new WP_Error(\sprintf(\esc_html__('Error: File invalid: %s', 'eightshift-libs'), $path));
return false;
}

$svg = new SimpleXMLElement($svgContent);
$src = $response['url'];
$width = (int) $svg['width'];
$height = (int) $svg['height'];

// media gallery.
$response['image'] = \compact('src', 'width', 'height');
$response['thumb'] = \compact('src', 'width', 'height');

// media single.
$response['sizes']['full'] = [
'height' => $height,
'width' => $width,
'url' => $src,
'orientation' => $height > $width ? 'portrait' : 'landscape',
];
}
} catch (Exception $e) {
// Translators: %s represents the error text.
new WP_Error(\sprintf(\esc_html__('Error: %s', 'eightshift-libs'), $e));
}
}

return $response;
}

/**
* Check if SVG is valid on Add New Media Page.
*
* @param array<object|string> $response Response array.
* @return array<mixed>
*/
public function validateSvgOnUpload($response)
{
if ($response['type'] === 'image/svg+xml' && \class_exists('SimpleXMLElement')) {
$path = $response['tmp_name'];

$svgContent = \file($path);
$svgContent = \implode(' ', $svgContent);

if (\file_exists($path)) {
if (!Helpers::isValidXml($svgContent)) {
return [
'size' => $response,
'name' => $response['name'],
];
}
}
}
return $response;
iruzevic marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Enable SVG file upload.
*
* @param array<object> $filetypeExtData Array fot output data.
* @param string $file Full path to the file.
* @param string $filename The name of the file (may differ from $file due to $file being in a tmp directory).
* @return array<object|string>
*/
public function enableSvgUpload($filetypeExtData, $file, $filename): array
{
if (\substr($filename, -4) === '.svg') {
$filetypeExtData['ext'] = 'svg';
$filetypeExtData['type'] = 'image/svg+xml';
}
return $filetypeExtData;
}

/**
* Enable JSON file upload.
*
* @param array<object> $filetypeExtData Array fot output data.
* @param string $file Full path to the file.
* @param string $filename The name of the file (may differ from $file due to $file being in a tmp directory).
* @return array<object|string>
*/
public function enableJsonUpload($filetypeExtData, $file, $filename): array
{
if (\substr($filename, -5) === '.json') {
$filetypeExtData['ext'] = 'json';
$filetypeExtData['type'] = 'application/json';
}
return $filetypeExtData;
}

/**
* WebP Quality compression range 0-100.
*
Expand Down
5 changes: 5 additions & 0 deletions src/Media/MediaExample.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ class MediaExample extends AbstractMedia
public function register(): void
{
\add_action('after_setup_theme', [$this, 'addThemeSupport'], 20);
\add_filter('upload_mimes', [$this, 'enableMimeTypes']);
\add_filter('wp_prepare_attachment_for_js', [$this, 'enableSvgMediaLibraryPreview'], 10, 2);
\add_filter('wp_handle_upload_prefilter', [$this, 'validateSvgOnUpload']);
\add_filter('wp_check_filetype_and_ext', [$this, 'enableSvgUpload'], 10, 3);
\add_filter('wp_check_filetype_and_ext', [$this, 'enableJsonUpload'], 10, 3);

// WebP.
if (\extension_loaded('gd')) {
Expand Down
84 changes: 84 additions & 0 deletions src/Optimization/OptimizationCli.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

/**
* Class that registers WPCLI command for ModifyAdminAppearance.
*
* @package EightshiftLibs\ModifyAdminAppearance
*/

declare(strict_types=1);

namespace EightshiftLibs\ModifyAdminAppearance;

use EightshiftLibs\Cli\AbstractCli;
use EightshiftLibs\Cli\ParentGroups\CliCreate;
use EightshiftLibs\Helpers\Helpers;

/**
* Class OptimizationCli
*/
class OptimizationCli extends AbstractCli
{
/**
* Get WPCLI command parent name
*
* @return string
*/
public function getCommandParentName(): string
{
return CliCreate::COMMAND_NAME;
}

/**
* Get WPCLI command name
*
* @return string
*/
public function getCommandName(): string
{
return 'optimization';
}

/**
* Get WPCLI command doc
*
* @return array<string, array<int, array<string, bool|string>>|string>
*/
public function getDoc(): array
{
return [
'shortdesc' => 'Create optimization class.',
'longdesc' => $this->prepareLongDesc("
## USAGE

Used to create optimization service class remove unecesery WP core stuff for faster loading.

## EXAMPLES

# Create service class:
$ wp {$this->commandParentName} {$this->getCommandParentName()} {$this->getCommandName()}

## RESOURCES

Service class will be created from this example:
https://github.com/infinum/eightshift-libs/blob/develop/src/Optimization/OptimizationExample.php
"),
];
}

/* @phpstan-ignore-next-line */
public function __invoke(array $args, array $assocArgs)
{
$assocArgs = $this->prepareArgs($assocArgs);

$this->getIntroText($assocArgs);

$className = $this->getClassShortName();

// Read the template contents, and replace the placeholders with provided variables.
$this->getExampleTemplate(__DIR__, $className)
->renameClassName($className)
->renameGlobals($assocArgs)
->outputWrite(Helpers::getProjectPaths('srcDestination', 'Optimization'), "{$className}.php", $assocArgs);
}
}
Loading