Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
9 | class GL_Plugin_Check_v3 |
||
10 | { |
||
11 | const MIN_PHP_VERSION = '5.6.0'; |
||
12 | const MIN_WORDPRESS_VERSION = '4.7.0'; |
||
13 | |||
14 | /** |
||
15 | * @var string |
||
16 | */ |
||
17 | protected $file; |
||
18 | |||
19 | /** |
||
20 | * @var array |
||
21 | */ |
||
22 | protected $versions; |
||
23 | |||
24 | /** |
||
25 | * @param string $file |
||
26 | */ |
||
27 | public function __construct( $file, array $versions = array() ) |
||
28 | { |
||
29 | $this->file = realpath( $file ); |
||
30 | $this->versions = wp_parse_args( $versions, array( |
||
31 | 'php' => static::MIN_PHP_VERSION, |
||
32 | 'wordpress' => static::MIN_WORDPRESS_VERSION, |
||
33 | )); |
||
34 | } |
||
35 | |||
36 | /** |
||
37 | * @return bool |
||
38 | */ |
||
39 | public function canProceed() |
||
40 | { |
||
41 | if( $this->isValid() ) { |
||
42 | return true; |
||
43 | } |
||
44 | add_action( 'activated_plugin', array( $this, 'deactivate' )); |
||
45 | add_action( 'admin_notices', array( $this, 'deactivate' )); |
||
46 | return false; |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * @return bool |
||
51 | */ |
||
52 | public function isPhpValid() |
||
53 | { |
||
54 | return !version_compare( PHP_VERSION, $this->versions['php'], '<' ); |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * @return bool |
||
59 | */ |
||
60 | public function isValid() |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * @return bool |
||
67 | */ |
||
68 | public function isWpValid() |
||
69 | { |
||
70 | global $wp_version; |
||
71 | return !version_compare( $wp_version, $this->versions['wordpress'], '<' ); |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * @param string $plugin |
||
76 | * @return void |
||
77 | */ |
||
78 | public function deactivate( $plugin ) |
||
79 | { |
||
80 | if( $this->isValid() )return; |
||
81 | $pluginSlug = plugin_basename( $this->file ); |
||
82 | if( $plugin == $pluginSlug ) { |
||
83 | $this->redirect(); //exit |
||
84 | } |
||
85 | $pluginData = get_file_data( $this->file, array( 'name' => 'Plugin Name' ), 'plugin' ); |
||
86 | deactivate_plugins( $pluginSlug ); |
||
87 | $this->printNotice( $pluginData['name'] ); |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * @return array |
||
92 | */ |
||
93 | protected function getMessages() |
||
103 | ); |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * @param string $pluginName |
||
108 | * @return void |
||
109 | */ |
||
110 | protected function printNotice( $pluginName ) |
||
127 | ); |
||
128 | } |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * @return void |
||
133 | */ |
||
134 | protected function redirect() |
||
142 | } |
||
143 | } |
||
144 |