loaded = false; $this->file = $file; $this->current_db_version = get_option( self::DB_VERSION_OPTION_NAME, '0' ); } /** * Checks if the plugin is loaded. * * @return bool */ public function is_loaded() { return $this->loaded; } /** * Returns true if the $new_version is greater than the $current_version. Else returns false. * * @since 2.0.0 * * @param string $new_version * @param string $current_version * * @return bool */ public function version_is_old( $new_version, $current_version ) { return ( version_compare( $new_version, $current_version, '>' ) ) ? true : false; } /** * Loads the plugin into WordPress. */ public function load() { if ( $this->is_loaded() ) { return; } /** * If our DB version is out of date, run our migrations. */ if ( $this->version_is_old( self::DB_VERSION, $this->current_db_version ) ) { $this->run_migrations(); } $this->event_manager = new Event_Manager(); foreach ( $this->get_subscribers() as $subscriber ) { $this->event_manager->add_subscriber( $subscriber ); } /** * Register Shortcodes. * * @since 2.0.0 * * @link https://carlalexander.ca/designing-class-assemble-plugin/#comment-3260130577 */ foreach ( $this->get_shortcodes() as $shortcode ) { $this->register_shortcode( $shortcode ); } $this->loaded = true; } /** * Perform any outstanding migration then update the DB_VERSION_OPTION_NAME option with * the migration version. * * @since 2.0.0 */ public function run_migrations() { if ( wp_doing_ajax() ) { return; } foreach ( $this->get_migrations() as $migration ) { $migration_version = $migration->version(); if ( ! $this->version_is_old( $migration_version, $this->current_db_version ) ) { continue; } $migration->run(); update_option( self::DB_VERSION_OPTION_NAME, $migration_version, true ); } } /** * Register the given shortcode with the WordPress shortcode API. * * @since 2.0.0 * * @param Shortcode_Interface $shortcode */ private function register_shortcode( Shortcode_Interface $shortcode ) { add_shortcode( $shortcode::get_name(), array( $shortcode, 'generate_output' ) ); } /** * Get the plugin event subscribers (ie. actions and filters). * * @since 2.0.0 * * @return \Datafeedr\Api\Wuwei\Event\Subscriber_Interface[] */ private function get_subscribers() { return [ new Subscribers\TestAdminNotice(), ]; } /** * Get the plugin shortcodes. * * @since 2.0.0 * * @return Shortcode_Interface[] */ private function get_shortcodes() { return [ new Shortcodes\TestShortcodeMsg(), ]; } /** * Returns all DB migrations. * * @since 2.0.0 * * @return Migration_Interface[] */ public function get_migrations() { return [ new Migrations\Migration_20180120151532_Add_Networks_Table(), ]; } /** * Returns full path to the plugin's directory. * * @since 2.0.0 * * @return string */ public function path() { return plugin_dir_path( $this->file ); } /** * Returns full URL to the plugin's directory. * * @since 2.0.0 * * @return string */ public function url() { return plugin_dir_url( $this->file ); } }