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

Improved plugins template and new PHPCS rules #1

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
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
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,29 +97,33 @@ In this GitHub repository, plugins that address the same feature (cache, lazyloa

It’s easy to over-engineer naming conventions, so we stick to a pragmatic rule of thumb when in doubt:

1. Namespaces should be unique and consistent.
1. Namespaces should be unique and consistent, and written in CamelCase.
Screenfeed marked this conversation as resolved.
Show resolved Hide resolved
2. Function names should be descriptive.

We rely on one consistent namespace for all Helper Plugins, and descriptive subnamespaces for each plugin:

```php
namespace ImagifyPlugin\Helpers\{subnamespace(s)}
namespace ImagifyPluginHelpers\{subnamespace(s)}
Screenfeed marked this conversation as resolved.
Show resolved Hide resolved
```

Or more specifically:

```php
namespace ImagifyPlugin\Helpers\{feature}\{what_this_plugin_does}
namespace ImagifyPluginHelpers\{feature}\{what_this_plugin_does}
```

Example:
```php
namespace ImagifyPlugin\Helpers\optimization\auto;
namespace ImagifyPluginHelpers\Optimization\NoAutoOptimPDF;

add_action( 'init', __NAMESPACE__ . '\exclude_pdf' );

/**
* Function doc.
*/
function exclude_pdf() {
// Exclude PDF files from being auto-optimized upon upload.
}
add_action( 'init', __NAMESPACE__ . '\exclude_pdf' );
```

### 4.2. Naming placeholder URLs, domains, values
Expand All @@ -144,4 +148,4 @@ All plugins in this repository, like WordPress and Imagify, are licensed under [

## 6. Questions?

Shoot us a message at: [imagify.io/contact/](https://imagify.io/contact/)
Shoot us a message at: [imagify.io/contact/](https://imagify.io/contact/).
33 changes: 18 additions & 15 deletions _imagify-helper-plugin/imagify-helper-plugin.php
Original file line number Diff line number Diff line change
@@ -1,37 +1,40 @@
<?php
/**
* Plugin Name: Imagify | {What This Plugin Does}
* Description: {What this plugin does in one clear sentence.}
* Plugin URI: {GitHub repo URL of this plugin}
* Author: Imagify Support Team
* Author URI: http://imagify.io/
* License: GNU General Public License v2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
* Plugin Name: Imagify | {What This Plugin Does}
* Description: {What this plugin does in one clear sentence.}
* Plugin URI: {GitHub repo URL of this plugin}
* Version: {Plugin version}
* Requires PHP: {Required php version: minimum value is 5.3}
* Author: Imagify Support Team
* Author URI: http://imagify.io/
* License: GPLv2
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
*
* Copyright SAS WP MEDIA 2018
* Copyright 2020 WP Media
*
* @package WP-Media\ImagifyPluginHelpers\Boilerplate
*/

// EDIT THIS: Replace `boilerplate` with your custom subnamespace.
// Namespaces must be declared before any other declaration.
namespace ImagifyPlugin\Helpers\boilerplate;
namespace ImagifyPluginHelpers\Boilerplate;
Screenfeed marked this conversation as resolved.
Show resolved Hide resolved

// Standard plugin security, keep this line in place.
defined( 'ABSPATH' ) or die();
defined( 'ABSPATH' ) || die();
Screenfeed marked this conversation as resolved.
Show resolved Hide resolved

// Hooking into `imagify_loaded` is a safe way to make sure all Imagify features are available, however, it’s not required.
// Using other hooks directly will be just fine in most cases.
add_action( 'imagify_loaded', __NAMESPACE__ . '\do_stuff' );
/**
* Adds customizations once Imagify has loaded.
* HEADS UP: If you keep the deactivation hook further down this file,
* you will have to edit it to remove_filter() this function.
*
* @since {Plugin version}
* @author {Author Name}
*/
function do_stuff() {

// Do something here.
add_filter( 'example_filter', 'example_function' );

}
// Hooking into `imagify_loaded` is a safe way to make sure all Imagify
// features are available, however, it’s not required.
// Using other hooks directly will be just fine in most cases.
add_action( 'imagify_loaded', __NAMESPACE__ . '\do_stuff' );
38 changes: 21 additions & 17 deletions optimization/imagify-bulk-buffer-size/imagify-bulk-buffer-size.php
Original file line number Diff line number Diff line change
@@ -1,32 +1,36 @@
<?php
/**
* Plugin Name: Imagify | Change Bulk Buffer Size
* Description: Helps to avoid CPU issues during bulk optimization.
* Plugin URI: https://github.com/wp-media/imagify-helpers/tree/master/optimization/imagify-bulk-buffer-size/
* Author: Imagify Support Team
* Author URI: http://imagify.io/
* License: GNU General Public License v2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
* Plugin Name: Imagify | Change Bulk Buffer Size
* Description: Helps to avoid CPU issues during bulk optimization.
* Plugin URI: https://github.com/wp-media/imagify-helpers/tree/master/optimization/imagify-bulk-buffer-size/
* Version: 1.1
* Requires PHP: 5.3
* Author: Imagify Support Team
* Author URI: http://imagify.io/
* License: GPLv2
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
*
* Copyright SAS WP MEDIA 2018
* Copyright 2020 WP Media
*
* @package WP-Media\ImagifyPluginHelpers\BulkBufferSize
*/
namespace ImagifyPlugin\Helpers\optimization\bulk;

defined( 'ABSPATH' ) or die();
namespace ImagifyPluginHelpers\Optimization\BulkBufferSize;
Screenfeed marked this conversation as resolved.
Show resolved Hide resolved

defined( 'ABSPATH' ) || die();

add_filter( 'imagify_bulk_buffer_sizes', __NAMESPACE__ . '\buffer_sizes', 10, 3 );
Screenfeed marked this conversation as resolved.
Show resolved Hide resolved
/**
* Prevent automatic optimization for PDF.
* Allow only one optimization at the same time during bulk optimization, for all contexts.
*
* @since 1.0
* @author Grégory Viguier
* @author Caspar Hübinger
*
* @param array $buffer_sizes An array of number of parallel queries
* @return array Modified array
* @param array $buffer_sizes An array of number of parallel queries. Array keys are contexts, like 'wp' and 'custom-folders'.
* @return array Modified array.
*/
function buffer_sizes( $buffer_sizes ) {

$buffer = array_keys( $buffer_sizes );

return array_fill_keys( $buffer_sizes, 1 );
$contexts = array_keys( $buffer_sizes );
return array_fill_keys( $contexts, 1 );
}
Binary file modified optimization/imagify-bulk-buffer-size/imagify-bulk-buffer-size.zip
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,32 +1,37 @@
<?php
/**
* Plugin Name: Imagify | No Auto-Optimization for PDFs
* Description: Excludes PDF files from being auto-optimized once they’re uploaded.
* Plugin URI: https://github.com/wp-media/imagify-helpers/tree/master/optimization/imagify-no-auto-optimize-pdf/
* Author: Imagify Support Team
* Author URI: http://imagify.io/
* License: GNU General Public License v2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
* Plugin Name: Imagify | No Auto-Optimization for PDFs
* Description: Excludes PDF files from being auto-optimized once they’re uploaded.
* Plugin URI: https://github.com/wp-media/imagify-helpers/tree/master/optimization/imagify-no-auto-optimize-pdf/
* Version: 1.0.1
* Requires PHP: 5.3
* Author: Imagify Support Team
* Author URI: http://imagify.io/
* License: GPLv2
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
*
* Copyright SAS WP MEDIA 2018
* Copyright 2020 WP Media
*
* @package WP-Media\ImagifyPluginHelpers\NoAutoOptimPDF
*/
namespace ImagifyPlugin\Helpers\optimization\auto;

defined( 'ABSPATH' ) or die();
namespace ImagifyPluginHelpers\Optimization\NoAutoOptimPDF;
Screenfeed marked this conversation as resolved.
Show resolved Hide resolved

defined( 'ABSPATH' ) || die();

add_filter( 'imagify_auto_optimize_attachment', __NAMESPACE__ . '\no_optimize_pdf', 10, 3 );
add_filter( 'imagify_auto_optimize_attachment', __NAMESPACE__ . '\no_optimize_pdf', 10, 2 );
/**
* Prevent automatic optimization for PDF.
*
* @since 1.0
* @author Grégory Viguier
* @author Caspar Hübinger
*
* @param bool $optimize True to optimize, false otherwise.
* @param int $attachment_id Attachment ID.
* @param array $metadata An array of attachment meta data.
* @param bool $optimize True to optimize, false otherwise.
* @param int $attachment_id Attachment ID.
* @return bool
*/
function no_optimize_pdf( $optimize, $attachment_id, $metadata ) {
function no_optimize_pdf( $optimize, $attachment_id ) {
if ( ! $optimize ) {
return false;
}
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
<?php
/**
* Plugin Name: Imagify | Reset Optimization Status
* Description: Will “reset” Imagify’s optimization status in the database, so that previously optimized images will be considered not optimized. Physical image files will not actually be modified! How to use: 1. Activate plugin. 2. Reload plugin page once. 3. Deactivate plugin!
* Plugin URI: https://github.com/wp-media/imagify-helpers/tree/master/optimization/imagify-reset-optimization-status/
* Author: Imagify Support Team
* Author URI: http://imagify.io/
* License: GNU General Public License v2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
* Plugin Name: Imagify | Reset Optimization Status
* Description: Will “reset” Imagify’s optimization status in the database, so that previously optimized images will be considered not optimized. Physical image files will not actually be modified! How to use: 1. Activate plugin. 2. Reload plugin page once. 3. Deactivate plugin!
* Plugin URI: https://github.com/wp-media/imagify-helpers/tree/master/optimization/imagify-reset-optimization-status/
* Version: 1.0.1
* Requires PHP: 5.3
* Author: Imagify Support Team
* Author URI: http://imagify.io/
* License: GPLv2
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
*
* Copyright SAS WP MEDIA 2018
* Copyright 2020 WP Media
*
* @package WP-Media\ImagifyPluginHelpers\ResetOptimizationStatus
*/
namespace ImagifyPlugin\Helpers\optimization\status;

defined( 'ABSPATH' ) or die();
namespace ImagifyPluginHelpers\Optimization\ResetOptimizationStatus;
Screenfeed marked this conversation as resolved.
Show resolved Hide resolved

defined( 'ABSPATH' ) || die();

add_filter( 'init', __NAMESPACE__ . '\reset' );
/**
* “Reset” Imagify so that images uploaded to the Media library via FTP can be optimised.
*
* @since 1.0
* @author Grégory Viguier
* @author Caspar Hübinger
*
* @return void
*/
function reset() {

$deleted1 = delete_metadata( 'post', '', '_imagify_status', '', true );
$deleted2 = delete_metadata( 'post', '', '_imagify_optimization_level', '', true );
$deleted3 = delete_metadata( 'post', '', '_imagify_data', '', true );
Expand Down
Binary file not shown.
35 changes: 35 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0"?>
<ruleset name="Imagify Helpers">
<description>Helper plugins for Imagify.</description>

<arg name="extensions" value="php"/>

<file>.</file>

<!-- Check for cross-version support for PHP 5.3 and higher + WP 4.0 and higher. -->
<config name="testVersion" value="5.3-"/>
<config name="minimum_supported_wp_version" value="4.0"/>

<!-- Run against the PHPCompatibility ruleset dedicated to WP. -->
<rule ref="PHPCompatibilityWP">
<include-pattern>*\.php$</include-pattern>
</rule>

<!-- Run against the WordPress ruleset. -->
<rule ref="WordPress">
<!--<exclude name="Generic.Arrays.DisallowShortArraySyntax" />-->
<exclude name="Generic.Formatting.MultipleStatementAlignment.IncorrectWarning"/><!-- Excluded as long as it doesn't ignore comments. -->
<exclude name="Generic.Formatting.MultipleStatementAlignment.NotSameWarning"/><!-- Too many false positives. -->
<exclude name="Generic.Functions.FunctionCallArgumentSpacing.TooMuchSpaceAfterComma"/><!-- Completely broken. -->
<exclude name="Squiz.Commenting.FileComment.Missing"/><!-- No thanks. -->
<exclude name="Squiz.PHP.EmbeddedPhp.ContentBeforeOpen"/><!-- No. Definitively, no. -->
<exclude name="Squiz.PHP.EmbeddedPhp.ContentAfterEnd"/><!-- Same. -->
<exclude name="Squiz.PHP.CommentedOutCode.Found"/><!-- Completely broken. -->
<exclude name="WordPress.Arrays.MultipleStatementAlignment.DoubleArrowNotAligned"/><!-- Same. -->
<exclude name="WordPress.Arrays.MultipleStatementAlignment.LongIndexSpaceBeforeDoubleArrow"/><!-- Same. -->
<exclude name="WordPress.DB.DirectDatabaseQuery.DirectQuery"/><!-- LOL. Yeah, sure. -->
<exclude name="WordPress.DB.DirectDatabaseQuery.NoCaching"/><!-- Using static vars IS caching. -->
<exclude name="WordPress.WhiteSpace.OperatorSpacing.SpacingBefore"/><!-- Nope. Allow ternary tests to be aligned. -->
<exclude name="WordPress.WP.GlobalVariablesOverride.Prohibited"/><!-- And yet, we will if it is needed. -->
</rule>
</ruleset>
Original file line number Diff line number Diff line change
@@ -1,34 +1,48 @@
<?php
/**
* Plugin Name: Bulk Optimize only for Super Admins (Multisite)
* Description: Bumps up the default user capacity required for bulk-optimization to Super Admin on multisite.
* Plugin URI: https://github.com/wp-media/imagify-helpers/tree/master/settings/imagify-capacity/
* Author: Imagify Support Team
* Author URI: http://imagify.io/
* License: GNU General Public License v2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
* Plugin Name: Bulk Optimize only for Super Admins (Multisite)
* Description: Bumps up the default user capacity required for bulk-optimization to Super Admin on multisite.
* Plugin URI: https://github.com/wp-media/imagify-helpers/tree/master/settings/imagify-capacity/
* Version: 1.1
* Requires PHP: 5.3
* Author: Imagify Support Team
* Author URI: http://imagify.io/
* License: GPLv2
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
*
* Copyright SAS WP MEDIA 2018
* Copyright 2020 WP Media
*
* @package WP-Media\ImagifyPluginHelpers\BulkOptimUserCapacity
*/

// Namespaces must be declared before any other declaration.
namespace ImagifyPlugin\Helpers\settings\capacity;
namespace ImagifyPluginHelpers\Settings\BulkOptimUserCapacity;
Screenfeed marked this conversation as resolved.
Show resolved Hide resolved

// Standard plugin security, keep this line in place.
defined( 'ABSPATH' ) or die();
defined( 'ABSPATH' ) || die();

add_action( 'imagify_loaded', __NAMESPACE__ . '\init' );
/**
* Plugin init.
*
* @since 1.1
* @author Grégory Viguier
*/
function init() {
if ( is_multisite() ) {
add_action( 'imagify_capacity', __NAMESPACE__ . '\multisite_bulk_optimize', 10, 2 );
}
}

/**
* Bumps up capacity for bulk optimization to super admin on multisite.
*
* @since 1.0
* @author Caspar Hübinger
* @see https://github.com/wp-media/imagify-plugin/blob/c5c004f79e39595098e7269baa704bfd06f727de/inc/functions/common.php#L4-L67
* @see https://github.com/wp-media/imagify-plugin/blob/cd17020d26c12d24fbca59455e87255a3e6de2dc/classes/Context/AbstractContext.php#L275-L300
*
* @param string $capacity WordPress user capacity
* @param string $describer Contextual describer of context. Possible values are 'manage', 'bulk-optimize', 'manual-optimize', 'auto-optimize', and 'optimize-file'.
* @return string Imagify capacity according to context
* @param string $capacity WordPress user capacity.
* @param string $describer Contextual describer of context. Possible values are like 'manage', 'bulk-optimize', 'manual-optimize', 'auto-optimize'.
* @return string Imagify capacity according to context.
*/
function multisite_bulk_optimize( $capacity, $describer ) {

return is_multisite() && 'bulk-optimize' === $describer ? 'manage_network_options' : $capacity;
return 'bulk-optimize' === $describer ? 'manage_network_options' : $capacity;
}
add_action( 'imagify_capacity', __NAMESPACE__ . '\multisite_bulk_optimize', 10, 2 );
Binary file not shown.