Skip to content

Commit 3fc3660

Browse files
Add conditional WP-CLI initialization and hooks
- Only initialize CLI hooks when WP-CLI is available using defined('WP_CLI') check - Move CLI initialization from admin_init() to init() for broader availability - Register CLI commands using proper cli_init hook instead of immediate registration - Improve performance by avoiding unnecessary objects when CLI is not present - Ensure CLI commands work in all WordPress contexts (web, CLI, AJAX, etc.) This prevents CLI-related code from running in web requests while ensuring WP-CLI commands are properly available when needed.
1 parent 724cad2 commit 3fc3660

3 files changed

Lines changed: 51 additions & 0 deletions

File tree

src/class-tiny-cli.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
/*
3+
* Tiny Compress Images - WordPress plugin.
4+
* Copyright (C) 2015-2018 Tinify B.V.
5+
*
6+
* This program is free software; you can redistribute it and/or modify it
7+
* under the terms of the GNU General Public License as published by the Free
8+
* Software Foundation; either version 2 of the License, or (at your option)
9+
* any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14+
* more details.
15+
*
16+
* You should have received a copy of the GNU General Public License along
17+
* with this program; if not, write to the Free Software Foundation, Inc., 51
18+
* Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19+
*/
20+
21+
class Tiny_Cli {
22+
/**
23+
* Tiny_Plugin $settings
24+
*
25+
* @var Tiny_Settings
26+
*/
27+
private $tiny_settings;
28+
29+
public function __construct( $settings ) {
30+
$this->tiny_settings = $settings;
31+
32+
$this->add_hooks();
33+
}
34+
35+
/**
36+
* Registers hooks to set up CLI support
37+
*/
38+
public function add_hooks() {
39+
// Only add CLI hooks when WP-CLI is available
40+
if ( defined( 'WP_CLI' ) && WP_CLI ) {
41+
add_action( 'cli_init', array( $this, 'register' ) );
42+
}
43+
}
44+
45+
public function register() {
46+
\WP_CLI::add_command( 'tiny', $this );
47+
}
48+
}

src/class-tiny-plugin.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ public function init() {
6363
load_plugin_textdomain( self::NAME, false,
6464
dirname( plugin_basename( __FILE__ ) ) . '/languages'
6565
);
66+
67+
new Tiny_CLI( $this->settings );
6668
}
6769

6870
public function ajax_init() {

tiny-compress-images.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
require dirname( __FILE__ ) . '/src/class-tiny-settings.php';
2222
require dirname( __FILE__ ) . '/src/class-tiny-plugin.php';
2323
require dirname( __FILE__ ) . '/src/class-tiny-notices.php';
24+
require dirname( __FILE__ ) . '/src/class-tiny-cli.php';
2425
require dirname( __FILE__ ) . '/src/compatibility/wpml/class-tiny-wpml.php';
2526
require dirname( __FILE__ ) . '/src/compatibility/as3cf/class-tiny-as3cf.php';
2627

0 commit comments

Comments
 (0)