diff --git a/phpcs.xml b/phpcs.xml index c31f2cb..d974843 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -7,6 +7,7 @@ src + src/PostInstall.php diff --git a/phpstan-stubs/ComposerScriptEvent.stub.php b/phpstan-stubs/ComposerScriptEvent.stub.php new file mode 100644 index 0000000..643872c --- /dev/null +++ b/phpstan-stubs/ComposerScriptEvent.stub.php @@ -0,0 +1,11 @@ + 'Activated', + 'text' => __( 'Activated', '%domain%' ), 'url' => '#', ]; @@ -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, ]; @@ -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, ]; @@ -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; diff --git a/src/Model/wp_media_plugins.php b/src/Model/wp_media_plugins.php index e1a1e29..6105644 100644 --- a/src/Model/wp_media_plugins.php +++ b/src/Model/wp_media_plugins.php @@ -5,15 +5,15 @@ 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' => [ @@ -21,36 +21,36 @@ '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' => [ @@ -58,8 +58,8 @@ '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/', ], ], diff --git a/src/PostInstall.php b/src/PostInstall.php new file mode 100644 index 0000000..a9590f9 --- /dev/null +++ b/src/PostInstall.php @@ -0,0 +1,93 @@ +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']; + } +} \ No newline at end of file diff --git a/tests/Unit/src/Model/PluginFamily/filterPluginsByActivation.php b/tests/Unit/src/Model/PluginFamily/filterPluginsByActivation.php index c8257e0..0c9c144 100644 --- a/tests/Unit/src/Model/PluginFamily/filterPluginsByActivation.php +++ b/tests/Unit/src/Model/PluginFamily/filterPluginsByActivation.php @@ -42,6 +42,7 @@ public function tear_down() { * @dataProvider configTestData */ public function testShouldReturnExpected( $config, $expected ) { + $this->stubTranslationFunctions(); $this->config = $config; if ( 'both' === $config['order'] ) {