Skip to content

Commit

Permalink
Release for v1.0.3 (#15)
Browse files Browse the repository at this point in the history
* Initial commit

* Added phpcs

* Added lint config

* Updated config file

* Added gitignore

* Fixed phpcs errors

* Ignore composer.lock

* Added tests

* Added patchwork config

* Updated composer packages and script

* Updated method

* Updated namespace

* Updated namespace and methods

* Added test workflow

* Remove test cache

* Ignore test cache

* Updated README

* Updated README

* Updated README

* Updated README

* Updated test

* Updated README

* Updated namespace

* Added phpstan

* Fixed phpstan errors

* Updated composer.json

* Added phpstan config

* Added workflow file for phpstan

* Updated namespace

* use defined var

* Updated README

* Updated tests

* Make wp rocket referrer dynamic

* Updated data

* Updated tests

* Added git attribute file

* Fixed phpcs

* :fixes: #6 change imagify plugin path

* Fix install event for imagify

* Fix imagify urls

* Fix lint error

* fix test error

* Directly reference plugin array copy

* Updated test

* Keep optimize category at the top even when containing main plugin

* Added test

* phpcs fix

* Updated README.md

* Updated tests

* Added post install class

* Updated data

* Use static property

* Use correct path

* Output success feed correctly

* Updated to use translation functions

* Updated method name and colorize messages

* excluded post install script scanning for phpcs

* Added phpstan stub for composer event

* Updated test

* Format and remove useless variable

* Updated documentation

* Ignore phpstan-stubs

---------

Co-authored-by: Opeyemi Ibrahim <[email protected]>
  • Loading branch information
jeawhanlee and Khadreal authored Nov 28, 2024
1 parent 1b827ee commit 9e13846
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 20 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ patchwork.json export-ignore
phpcs.xml export-ignore
phpstan.neon.dist export-ignore
README.md export-ignore
phpstan-stubs export-ignore

33 changes: 31 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,38 @@ For Imagify, the link generated by the Imagify Partner package is used.

PS: The Imagify Partner package needs be installed along side. refer [here](https://github.com/wp-media/wp-imagify-partner/) for more information.


## Installations & Configurations

### Mozart
It is strongly advised that this package is loaded using [Mozart](https://github.com/coenjacobs/mozart). This is to avoid package conflicts where the same package is being used in another plugin and loaded from the same namespace. So please, take the pain to install mozart as a development dependency of your plugin:
`composer require coenjacobs/mozart --dev`

- After installation, now you are set to configure mozart: see [here](https://github.com/coenjacobs/mozart?tab=readme-ov-file#configuration) or simply refer to how it's done on wp-rocket [here](https://github.com/wp-media/wp-rocket/blob/df52190a44f6b07cd23dd1fa9f6790df0558ef4c/composer.json#L113), there's no shame.
- Lastly setup (post install & update) scripts as done [here](https://github.com/coenjacobs/mozart?tab=readme-ov-file#scripts)

### Package

Now for the package, we need to come back to composer.json file again.
- Remember the `extra` key we added while setting up mozart some minutes ago, Bingo! Add a new property in it like this: `"plugin_domain": "your_text_domain"`. Replace `your_text_domain` with your actual text domain. e.g for WP Rocket, it's `rocket`
- Now again, remember the (post install & update) script we also created, we'll add new properties there also like this:

```json
"post-install-cmd": [
"Mozart\\dep_namespace\\WPMedia\\PluginFamily\\PostInstall::apply_text_domain"
],
"post-update-cmd": [
"Mozart\\dep_namespace\\WPMedia\\PluginFamily\\PostInstall::apply_text_domain"
],
```
Replace `Mozart\\dep_namespace` with the `dep_namespace` you configured for mozart earlier.

PS: The example above doesn't not take into consideration the mozart scripts. so don't copy directly and overwrite your scripts.

- Finally, install using composer: `composer require wp-media/plugin-family`

## Usage Instructions

- Install using composer: `composer require wp-media/plugin-family`
- Import the model that holds the filtered data into your view class.

```php
Expand Down Expand Up @@ -70,7 +99,7 @@ class Subscriber implements SubscriberInterface, PluginFamilyInterface {
```
The methods ( `install_activate` & `display_error_notice` ) are required.

PS: This example is based on the assumption that your plugin utilizes EDA. If EDA is not available, you can iterate through the events returned by `PluginFamily::get_subscribed_events()` and use `add_action` accordingly.
PS: This example is based on the assumption that your plugin utilizes EDA. If EDA is not implemented, you can iterate through the events returned by `PluginFamily::get_subscribed_events()` and use `add_action` accordingly.

## Development & Testing

Expand Down
1 change: 1 addition & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

<!-- ** WHAT TO SCAN ** -->
<file>src</file>
<exclude-pattern>src/PostInstall.php</exclude-pattern>
<!-- ** HOW TO SCAN ** -->
<arg value="sp"/><!-- Show sniff and progress -->
<arg name="colors"/><!-- Show results with colors -->
Expand Down
11 changes: 11 additions & 0 deletions phpstan-stubs/ComposerScriptEvent.stub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Composer\Script;

/**
* Composer Event class stub.
*/
class Event {
public function getComposer() {}
public function getIO() {}
}
4 changes: 3 additions & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
includes:
- phar://phpstan.phar/conf/bleedingEdge.neon
- phar://phpstan.phar/conf/bleedingEdge.neon
parameters:
level: 5
bootstrapFiles:
- %currentWorkingDirectory%/phpstan-stubs/ComposerScriptEvent.stub.php
paths:
- %currentWorkingDirectory%/src
8 changes: 4 additions & 4 deletions src/Model/PluginFamily.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function filter_plugins_by_activation( array $plugins, string $main_plugi
if ( is_plugin_active( $plugin_path ) && $main_plugin . '.php' !== $plugin_path ) {
// set cta data of active plugins.
$plugins[ $cat ]['plugins'][ $plugin ]['cta'] = [
'text' => 'Activated',
'text' => __( 'Activated', '%domain%' ),
'url' => '#',
];

Expand Down Expand Up @@ -106,7 +106,7 @@ public function filter_plugins_by_activation( array $plugins, string $main_plugi

// Set Installation link.
$plugins[ $cat ]['plugins'][ $plugin ]['cta'] = [
'text' => 'Install',
'text' => __( 'Install', '%domain%' ),
'url' => $install_activate_url,
];

Expand All @@ -115,7 +115,7 @@ public function filter_plugins_by_activation( array $plugins, string $main_plugi
$url = 'https://wp-rocket.me/?utm_source=' . $wpr_referrer . '-coupon&utm_medium=plugin&utm_campaign=' . $wpr_referrer;

$plugins[ $cat ]['plugins'][ $plugin ]['cta'] = [
'text' => 'Get it Now',
'text' => __( 'Get it Now', '%domain%' ),
'url' => $url,
];

Expand All @@ -124,7 +124,7 @@ public function filter_plugins_by_activation( array $plugins, string $main_plugi

// Set activation text.
if ( file_exists( WP_PLUGIN_DIR . '/' . $plugin_path ) ) {
$plugins[ $cat ]['plugins'][ $plugin ]['cta']['text'] = 'Activate';
$plugins[ $cat ]['plugins'][ $plugin ]['cta']['text'] = __( 'Activate', '%domain%' );

if ( 'wp-rocket/wp-rocket' === $plugin ) {
$plugins[ $cat ]['plugins'][ $plugin ]['cta']['url'] = $install_activate_url;
Expand Down
26 changes: 13 additions & 13 deletions src/Model/wp_media_plugins.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,61 +5,61 @@

return [
'optimize_performance' => [
'title' => 'Optimize Performance',
'title' => __( 'Optimize Performance', '%domain%' ),
'plugins' => [
'wp-rocket/wp-rocket' => [
'logo' => [
'file' => 'logo-wp-rocket.svg',
'width' => '50%',
],
'title' => 'Speed Up Your Website, Instantly',
'desc' => 'WP Rocket is the easiest way to make your WordPress website faster and boost your Google PageSpeed score. Get more traffic, better engagement, and higher conversions effortlessly.',
'title' => __( 'Speed Up Your Website, Instantly', '%domain%' ),
'desc' => __( 'WP Rocket is the easiest way to make your WordPress website faster and boost your Google PageSpeed score. Get more traffic, better engagement, and higher conversions effortlessly.', '%domain%' ),
'link' => '',
],
'imagify/imagify' => [
'logo' => [
'file' => 'logo-imagify.svg',
'width' => '50%',
],
'title' => 'Speed Up Your Website With Lighter Images',
'desc' => 'Imagify is the easiest WordPress image optimizer. It automatically compresses images, converts them to WebP and AVIF formats, and lets you resize and optimize with just one click!',
'title' => __( 'Speed Up Your Website With Lighter Images', '%domain%' ),
'desc' => __( 'Imagify is the easiest WordPress image optimizer. It automatically compresses images, converts them to WebP and AVIF formats, and lets you resize and optimize with just one click!', '%domain%' ),
'link' => 'https://imagify.io/',
],
],
],
'boost_traffic' => [
'title' => 'Boost Traffic',
'title' => __( 'Boost Traffic', '%domain%' ),
'plugins' => [
'seo-by-rank-math/rank-math' => [
'logo' => [
'file' => 'logo-rank-math.svg',
'width' => '60%',
],
'title' => 'The Swiss Army Knife of SEO Tools',
'desc' => 'Rank Math SEO is the Best WordPress SEO plugin with the features of many SEO and AI SEO tools in a single package to help multiply your SEO traffic.',
'title' => __( 'The Swiss Army Knife of SEO Tools', '%domain%' ),
'desc' => __( 'Rank Math SEO is the Best WordPress SEO plugin with the features of many SEO and AI SEO tools in a single package to help multiply your SEO traffic.', '%domain%' ),
'link' => 'https://rankmath.com/wordpress/plugin/seo-suite/',
],
],
],
'protect_secure' => [
'title' => 'Protect & Secure',
'title' => __( 'Protect & Secure', '%domain%' ),
'plugins' => [
'backwpup/backwpup' => [
'logo' => [
'file' => 'logo-backwpup.svg',
'width' => '60%',
],
'title' => 'The Easiest Way to Protect Your Website',
'desc' => 'BackWPup is the most comprehensive and user-friendly backup & restore plugin for WordPress. Easily schedule automatic backups, securely store and restore with just a few clicks!',
'title' => __( 'The Easiest Way to Protect Your Website', '%domain%' ),
'desc' => __( 'BackWPup is the most comprehensive and user-friendly backup & restore plugin for WordPress. Easily schedule automatic backups, securely store and restore with just a few clicks!', '%domain%' ),
'link' => 'https://backwpup.com/',
],
'uk-cookie-consent/uk-cookie-consent' => [
'logo' => [
'file' => 'logo-termly.svg',
'width' => '50%',
],
'title' => 'GDPR/CCPA Cookie Consent Banner',
'desc' => 'One of the easiest, most comprehensive, and popular cookie consent plugins available. Google Gold Certified Partner to quickly comply with data privacy laws from around the world.',
'title' => __( 'GDPR/CCPA Cookie Consent Banner', '%domain%' ),
'desc' => __( 'One of the easiest, most comprehensive, and popular cookie consent plugins available. Google Gold Certified Partner to quickly comply with data privacy laws from around the world.', '%domain%' ),
'link' => 'https://termly.io/resources/articles/wordpress-cookies-guide/',
],
],
Expand Down
93 changes: 93 additions & 0 deletions src/PostInstall.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

namespace WPMedia\PluginFamily;

use Composer\Script\Event;

class PostInstall {

/**
* Array of files to update.
*
* @var array
*/
private static $files = [
'PluginFamily',
'wp_media_plugins',
];

/**
* Updates domain text for package after composer update or install.
*
* @param Event $event Composer event.
* @return void
*/
public static function apply_text_domain( Event $event ) {

$output = $event->getIO();
$composer = $event->getComposer();
$extra = $composer->getPackage()->getExtra();

if ( ! isset( $extra['plugin_domain'] ) ) {
$output->writeError( self::colorize( 'Plugin domain is not set in the composer extra configuration.', 'red' ) );
return;
}

foreach ( self::$files as $file ) {
// Construct file path.
$path = __DIR__ . '/Model/' . $file . '.php';

if ( ! file_exists( $path ) ) {
$output->writeError( self::colorize( 'Could not find file: ' . $path . ', Does it exist?', 'red' ) );
return;
}

// Get file contents.
$content = file_get_contents( $path );

if ( false === $content ) {
$output->writeError( self::colorize( 'Failed to read the file: ' . $path, 'red' ) );
return;
}

// Update file content.
$updated_content = str_replace( '%domain%', $extra['plugin_domain'], $content );
$result = file_put_contents( $path, $updated_content );

if ( false === $result ) {
$output->writeError( self::colorize( 'Failed to write the updated content to the file: ' . $path, 'red' ) );
return;
}
}

// Output success feed.
$output->write( self::colorize( 'Text domain has been updated.', 'green' ) );

// Path to this script.
$script = __FILE__;

// Delete script after execution.
register_shutdown_function( function () use ( $script ) {
if ( file_exists( $script ) ) {
unlink( $script );
}
});
}

/**
* This function colorizes a given string with a specified color for console output.
*
* @param string $message String message to pass.
* @param string $color Color on the console.
* @return string
*/
private static function colorize( string $message, string $color ): string {
$colors = [
'red' => "\033[31m",
'green' => "\033[32m",
'reset' => "\033[0m",
];

return $colors[$color] . $message . $colors['reset'];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public function tear_down() {
* @dataProvider configTestData
*/
public function testShouldReturnExpected( $config, $expected ) {
$this->stubTranslationFunctions();
$this->config = $config;

if ( 'both' === $config['order'] ) {
Expand Down

0 comments on commit 9e13846

Please sign in to comment.