1 | <?php |
||
21 | class Plugin { |
||
22 | |||
23 | |||
24 | /** |
||
25 | * The plugin version. |
||
26 | * |
||
27 | * @since 1.5.0 |
||
28 | * @const string |
||
29 | */ |
||
30 | CONST VERSION = '1.5.0'; |
||
31 | |||
32 | /** |
||
33 | * The plugin name. |
||
34 | * |
||
35 | * @since 1.5.0 |
||
36 | * @const string |
||
37 | */ |
||
38 | CONST NAME = 'WP PHP Console'; |
||
39 | |||
40 | /** |
||
41 | * This plugin's settings options. |
||
42 | * |
||
43 | * @since 1.0.0 |
||
44 | * @access protected |
||
45 | * @var array $options Array of this plugin settings options. |
||
46 | */ |
||
47 | protected $options = array(); |
||
48 | |||
49 | /** |
||
50 | * Instance of PHP Console connector object. |
||
51 | * |
||
52 | * @since 1.4.0 |
||
53 | * @access public |
||
54 | * @var PhpConsole\Connector $connector Instance. |
||
55 | */ |
||
56 | public $connector; |
||
57 | |||
58 | |||
59 | /** |
||
60 | * Load plugin and connect to PHP Console. |
||
61 | * |
||
62 | * @since 1.0.0 |
||
63 | */ |
||
64 | public function __construct() { |
||
65 | |||
66 | // Handle translations. |
||
67 | add_action( 'plugins_loaded', array( $this, 'set_locale' ) ); |
||
68 | |||
69 | // Set options. |
||
70 | $this->options = $this->get_options(); |
||
71 | |||
72 | // Load admin. |
||
73 | $this->set_admin(); |
||
74 | |||
75 | // Bail out if PHP Console can't be found. |
||
76 | if ( ! class_exists( 'PhpConsole\Connector' ) ) { |
||
77 | return; |
||
78 | } |
||
79 | |||
80 | // Connect to PHP Console. |
||
81 | add_action( 'init', array( $this, 'connect' ), -100 ); |
||
82 | |||
83 | // Delay further PHP Console initialisation |
||
84 | // to have more context during Remote PHP execution. |
||
85 | add_action( 'wp_loaded', array( $this, 'init' ), -100 ); |
||
86 | |||
87 | } |
||
88 | |||
89 | |||
90 | /** |
||
91 | * Set plugin text domain. |
||
92 | * |
||
93 | * @since 1.0.0 |
||
94 | */ |
||
95 | public function set_locale() { |
||
96 | |||
97 | load_plugin_textdomain( |
||
98 | 'wp-php-console', |
||
99 | false, |
||
100 | dirname( dirname( plugin_basename( __FILE__ ) ) ) . '/languages/' |
||
101 | ); |
||
102 | |||
103 | } |
||
104 | |||
105 | |||
106 | /** |
||
107 | * Load admin. |
||
108 | * |
||
109 | * @since 1.5.0 |
||
110 | */ |
||
111 | private function set_admin() { |
||
112 | |||
113 | if ( ! defined( 'DOING_AJAX' ) && is_admin() ) { |
||
114 | |||
115 | // Add a settings link to the plugins admin screen. |
||
116 | $plugin_name = str_replace( 'includes/class-', '', plugin_basename( __FILE__ ) ); |
||
117 | add_filter( "plugin_action_links_{$plugin_name}", function( $actions ) { |
||
118 | return array_merge( array( |
||
119 | '<a href="' . esc_url( admin_url( 'options-general.php?page=wp-php-console' ) ) . '">' . __( 'Settings', 'wp-php-console' ) . '</a>', |
||
120 | ), $actions ); |
||
121 | } ); |
||
122 | |||
123 | // Init settings. |
||
124 | require_once __DIR__ . '/class-wp-php-console-settings.php'; |
||
125 | new Settings( $this->options ); |
||
126 | } |
||
127 | |||
128 | } |
||
129 | |||
130 | |||
131 | /** |
||
132 | * Connect to PHP Console. |
||
133 | * |
||
134 | * @since 1.4.0 |
||
135 | */ |
||
136 | public function connect() { |
||
137 | |||
138 | session_start(); |
||
139 | |||
140 | if ( ! $this->connector instanceof PhpConsole\Connector ) { |
||
141 | $this->connector = PhpConsole\Connector::getInstance(); |
||
142 | } |
||
143 | |||
144 | // Apply PHP Console options. |
||
145 | $this->apply_options(); |
||
146 | |||
147 | } |
||
148 | |||
149 | |||
150 | /** |
||
151 | * Get WP PHP Console settings options. |
||
152 | * |
||
153 | * @since 1.4.0 |
||
154 | * @return array |
||
155 | */ |
||
156 | protected function get_options() { |
||
169 | |||
170 | |||
171 | /** |
||
172 | * Apply options. |
||
173 | * |
||
174 | * @since 1.4.0 |
||
175 | */ |
||
176 | private function apply_options() { |
||
208 | |||
209 | |||
210 | /** |
||
211 | * Initialize PHP Console. |
||
212 | * |
||
213 | * @since 1.0.0 |
||
214 | */ |
||
215 | public function init() { |
||
297 | |||
298 | |||
299 | /** |
||
300 | * Prints an exception message as WordPress admin notice. |
||
301 | * |
||
302 | * @since 1.4.0 |
||
303 | * @param \Exception $e Exception object |
||
304 | */ |
||
305 | public function print_notice_exception( \Exception $e ) { |
||
317 | |||
318 | |||
319 | |||
320 | /** |
||
321 | * Admin password notice. |
||
322 | * Prompts user to set a password for PHP Console upon plugin activation. |
||
323 | * |
||
324 | * @since 1.3.2 |
||
325 | */ |
||
326 | public function password_notice() { |
||
342 | |||
343 | |||
344 | } |
||
345 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.