PluginProbe
Datafeedr API / 1.2.4
Datafeedr API v1.2.4
1.4.2 1.0.125 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.19 1.0.2 1.0.20 1.0.21 1.0.22 1.0.23 1.0.24 1.0.25 1.0.26 1.0.27 1.0.28 1.0.29 1.0.3 1.0.30 1.0.31 1.0.32 1.0.33 All 181 releases
datafeedr-api / src / Plugin.php

Plugin.php in Datafeedr API 1.2.4, at src/Plugin.php

244 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php namespace Datafeedr\Api;
2
3 use Datafeedr\Api\Wuwei\Event\Manager as Event_Manager;
4 use Datafeedr\Api\Wuwei\Migrations\Migration_Interface;
5 use Datafeedr\Api\Wuwei\Shortcode\Shortcode_Interface;
6
7 /**
8 * Class Plugin.
9 *
10 * @since 2.0.0
11 */
12 class Plugin {
13
14 /**
15 * Plugin version.
16 *
17 * @since 2.0.0
18 * @var string VERSION Plugin version.
19 */
20 const VERSION = '2.0.0dev1';
21
22 /**
23 * Database version.
24 *
25 * @since 2.0.0
26 * @var string DB_VERSION Database version.
27 */
28 const DB_VERSION = '20180120151532';
29
30 /**
31 * Database version option name.
32 *
33 * @since 2.0.0
34 * @var string DB_VERSION_OPTION_NAME
35 */
36 const DB_VERSION_OPTION_NAME = 'datafeedr_api_db_version';
37
38 /**
39 * The plugin event manager.
40 *
41 * @since 2.0.0
42 * @access public
43 * @var \Datafeedr\Api\Wuwei\Event\Manager
44 */
45 private $event_manager;
46
47 /**
48 * Flag to track if the plugin is loaded.
49 *
50 * @since 2.0.0
51 * @access public
52 * @var bool
53 */
54 private $loaded;
55
56 /**
57 * The full path to the main plugin file.
58 *
59 * Example: ~/wp-content/plugins/datafeedr-api/datafeedr-api-v2.php
60 *
61 * @since 2.0.0
62 * @access private
63 * @var string $file
64 */
65 private $file;
66
67 /**
68 * Is the user's current Database version.
69 *
70 * @since 2.0.0
71 * @access private
72 * @var string $current_db_version
73 */
74 private $current_db_version;
75
76 /**
77 * Plugin constructor.
78 *
79 * @param string $file
80 */
81 public function __construct( $file ) {
82 $this->loaded = false;
83 $this->file = $file;
84 $this->current_db_version = get_option( self::DB_VERSION_OPTION_NAME, '0' );
85 }
86
87 /**
88 * Checks if the plugin is loaded.
89 *
90 * @return bool
91 */
92 public function is_loaded() {
93 return $this->loaded;
94 }
95
96 /**
97 * Returns true if the $new_version is greater than the $current_version. Else returns false.
98 *
99 * @since 2.0.0
100 *
101 * @param string $new_version
102 * @param string $current_version
103 *
104 * @return bool
105 */
106 public function version_is_old( $new_version, $current_version ) {
107 return ( version_compare( $new_version, $current_version, '>' ) ) ? true : false;
108 }
109
110 /**
111 * Loads the plugin into WordPress.
112 */
113 public function load() {
114
115 if ( $this->is_loaded() ) {
116 return;
117 }
118
119 /**
120 * If our DB version is out of date, run our migrations.
121 */
122 if ( $this->version_is_old( self::DB_VERSION, $this->current_db_version ) ) {
123 $this->run_migrations();
124 }
125
126 $this->event_manager = new Event_Manager();
127
128 foreach ( $this->get_subscribers() as $subscriber ) {
129 $this->event_manager->add_subscriber( $subscriber );
130 }
131
132 /**
133 * Register Shortcodes.
134 *
135 * @since 2.0.0
136 *
137 * @link https://carlalexander.ca/designing-class-assemble-plugin/#comment-3260130577
138 */
139 foreach ( $this->get_shortcodes() as $shortcode ) {
140 $this->register_shortcode( $shortcode );
141 }
142
143 $this->loaded = true;
144 }
145
146 /**
147 * Perform any outstanding migration then update the DB_VERSION_OPTION_NAME option with
148 * the migration version.
149 *
150 * @since 2.0.0
151 */
152 public function run_migrations() {
153
154 if ( wp_doing_ajax() ) {
155 return;
156 }
157
158 foreach ( $this->get_migrations() as $migration ) {
159
160 $migration_version = $migration->version();
161
162 if ( ! $this->version_is_old( $migration_version, $this->current_db_version ) ) {
163 continue;
164 }
165
166 $migration->run();
167
168 update_option( self::DB_VERSION_OPTION_NAME, $migration_version, true );
169 }
170 }
171
172 /**
173 * Register the given shortcode with the WordPress shortcode API.
174 *
175 * @since 2.0.0
176 *
177 * @param Shortcode_Interface $shortcode
178 */
179 private function register_shortcode( Shortcode_Interface $shortcode ) {
180 add_shortcode( $shortcode::get_name(), array( $shortcode, 'generate_output' ) );
181 }
182
183 /**
184 * Get the plugin event subscribers (ie. actions and filters).
185 *
186 * @since 2.0.0
187 *
188 * @return \Datafeedr\Api\Wuwei\Event\Subscriber_Interface[]
189 */
190 private function get_subscribers() {
191 return [
192 new Subscribers\TestAdminNotice(),
193 ];
194 }
195
196 /**
197 * Get the plugin shortcodes.
198 *
199 * @since 2.0.0
200 *
201 * @return Shortcode_Interface[]
202 */
203 private function get_shortcodes() {
204 return [
205 new Shortcodes\TestShortcodeMsg(),
206 ];
207 }
208
209 /**
210 * Returns all DB migrations.
211 *
212 * @since 2.0.0
213 *
214 * @return Migration_Interface[]
215 */
216 public function get_migrations() {
217 return [
218 new Migrations\Migration_20180120151532_Add_Networks_Table(),
219 ];
220 }
221
222 /**
223 * Returns full path to the plugin's directory.
224 *
225 * @since 2.0.0
226 *
227 * @return string
228 */
229 public function path() {
230 return plugin_dir_path( $this->file );
231 }
232
233 /**
234 * Returns full URL to the plugin's directory.
235 *
236 * @since 2.0.0
237 *
238 * @return string
239 */
240 public function url() {
241 return plugin_dir_url( $this->file );
242 }
243 }
244