diff --git a/includes/class-admin.php b/includes/class-admin.php index c4601a1f..fd2b9d1a 100644 --- a/includes/class-admin.php +++ b/includes/class-admin.php @@ -63,8 +63,8 @@ public static function meta_boxes( $object, $box ) { /** * Add comment-type as column in WP-Admin * - * @param array $column the column to implement - * @param int $comment_id the comment id + * @param array $column The column to implement. + * @param int $comment_id The comment id. */ public static function manage_comments_custom_column( $column, $comment_id ) { if ( 'comment_type' !== $column ) { @@ -74,7 +74,11 @@ public static function manage_comments_custom_column( $column, $comment_id ) { } /** - * Add bulk option to bulk comment handler + * Add bulk option to bulk comment handler. + * + * @param array $bulk_actions The bulk actions. + * + * @return array The bulk actions. */ public static function bulk_comment_actions( $bulk_actions ) { $bulk_actions['refresh_webmention'] = __( 'Refresh Webmention', 'webmention' ); @@ -82,8 +86,13 @@ public static function bulk_comment_actions( $bulk_actions ) { } /** - * Add bulk action handler to comments + * Add bulk action handler to comments. + * + * @param string $redirect_to The redirect URL. + * @param string $doaction The action to perform. + * @param array $comment_ids The comment IDs. * + * @return string The redirect URL. */ public static function bulk_comment_action_handler( $redirect_to, $doaction, $comment_ids ) { if ( 'refresh_webmention' !== $doaction ) { @@ -98,12 +107,12 @@ public static function bulk_comment_action_handler( $redirect_to, $doaction, $co } /** - * Add an action link + * Add an action link. * - * @param array $links the settings links - * @param string $file the plugin filename + * @param array $links The settings links. + * @param string $file The plugin filename. * - * @return array the filtered array + * @return array The filtered array. */ public static function plugin_action_links( $links, $file ) { if ( stripos( $file, 'webmention' ) === false || ! function_exists( 'admin_url' ) ) { @@ -120,12 +129,12 @@ public static function plugin_action_links( $links, $file ) { } /** - * Add a plugin meta link + * Add a plugin meta link. * - * @param array $links the settings links - * @param string $file the plugin filename + * @param array $links The settings links. + * @param string $file The plugin filename. * - * @return array the filtered array + * @return array The filtered array. */ public static function plugin_row_meta( $links, $file ) { if ( stripos( $file, 'webmention' ) === false || ! function_exists( 'admin_url' ) ) { @@ -175,9 +184,11 @@ public static function comment_types_dropdown( $types ) { } /** - * Add comment-type as column in WP-Admin + * Add comment-type as column in WP-Admin. + * + * @param array $columns The list of column names. * - * @param array $columns the list of column names + * @return array The filtered columns. */ public static function comment_columns( $columns ) { $columns['comment_type'] = esc_html__( 'Comment-Type', 'webmention' ); @@ -185,6 +196,14 @@ public static function comment_columns( $columns ) { return $columns; } + /** + * Add comment-type as column in WP-Admin. + * + * @param array $actions The actions. + * @param object $comment The comment object. + * + * @return array The filtered actions. + */ public static function comment_row_actions( $actions, $comment ) { $query = array( '_wpnonce' => wp_create_nonce( "approve-comment_$comment->comment_ID" ), @@ -213,6 +232,11 @@ public static function comment_row_actions( $actions, $comment ) { return $actions; } + /** + * Add a webmention approve domain. + * + * @param string $host The host. + */ public static function add_webmention_approve_domain( $host ) { $approvelist = get_webmention_approve_domains(); $approvelist[] = $host; @@ -221,6 +245,11 @@ public static function add_webmention_approve_domain( $host ) { update_option( 'webmention_approve_domains', $approvelist ); } + /** + * Transition to approve list. + * + * @param object $comment The comment object. + */ public static function transition_to_approvelist( $comment ) { if ( ! current_user_can( 'moderate_comments' ) ) { return; @@ -267,9 +296,7 @@ public static function admin_menu() { * Load settings page */ public static function settings_page() { - require_once __DIR__ . '/class-db.php'; - \Webmention\DB::update_database(); - \Webmention\remove_semantic_linkbacks(); + Upgrade::maybe_upgrade(); add_thickbox(); wp_enqueue_script( 'plugin-install' ); @@ -326,6 +353,9 @@ public static function add_help_tab() { ); } + /** + * Register settings. + */ public static function register_settings() { register_setting( 'webmention', @@ -450,7 +480,7 @@ public static function register_settings() { } /** - * Add recommended privacy content + * Add recommended privacy content. */ public static function add_privacy_policy_content() { $content = diff --git a/includes/class-autoloader.php b/includes/class-autoloader.php new file mode 100644 index 00000000..73e16efb --- /dev/null +++ b/includes/class-autoloader.php @@ -0,0 +1,106 @@ +prefix = $prefix; + $this->prefix_length = \strlen( $prefix ); + $this->path = \rtrim( $path . '/' ); + } + + /** + * Registers Autoloader's autoload function. + * + * @throws \Exception When autoload_function cannot be registered. + * + * @param string $prefix Namespace prefix all classes have in common. + * @param string $path Path to the files to be loaded. + */ + public static function register_path( $prefix, $path ) { + $loader = new self( $prefix, $path ); + \spl_autoload_register( array( $loader, 'load' ) ); + } + + /** + * Loads a class if its namespace starts with `$this->prefix`. + * + * @param string $class_name The class to be loaded. + */ + public function load( $class_name ) { + if ( \strpos( $class_name, $this->prefix . self::NS_SEPARATOR ) !== 0 ) { + return; + } + + // Strip prefix from the start (ala PSR-4). + $class_name = \substr( $class_name, $this->prefix_length + 1 ); + $class_name = \strtolower( $class_name ); + $dir = ''; + + $last_ns_pos = \strripos( $class_name, self::NS_SEPARATOR ); + if ( false !== $last_ns_pos ) { + $namespace = \substr( $class_name, 0, $last_ns_pos ); + $namespace = \str_replace( '_', '-', $namespace ); + $class_name = \substr( $class_name, $last_ns_pos + 1 ); + $dir = \str_replace( self::NS_SEPARATOR, DIRECTORY_SEPARATOR, $namespace ) . DIRECTORY_SEPARATOR; + } + + $path = $this->path . $dir . 'class-' . \str_replace( '_', '-', $class_name ) . '.php'; + + if ( ! \file_exists( $path ) ) { + $path = $this->path . $dir . 'interface-' . \str_replace( '_', '-', $class_name ) . '.php'; + } + + if ( ! \file_exists( $path ) ) { + $path = $this->path . $dir . 'trait-' . \str_replace( '_', '-', $class_name ) . '.php'; + } + + if ( \file_exists( $path ) ) { + require_once $path; + } + } +} diff --git a/includes/class-cli.php b/includes/class-cli.php index 22e7d663..ea6e1af6 100644 --- a/includes/class-cli.php +++ b/includes/class-cli.php @@ -315,9 +315,7 @@ public function generate( $args, $assoc_args ) { * @return void */ public function db_migration( $args, $assoc_args ) { - require_once __DIR__ . '/class-db.php'; - - DB::update_database(); + Upgrade::maybe_upgrade(); WP_CLI::success( __( 'DB Migration finished', 'webmention' ) ); } diff --git a/includes/class-comment.php b/includes/class-comment.php index 72f09df8..1a1e525c 100644 --- a/includes/class-comment.php +++ b/includes/class-comment.php @@ -20,6 +20,12 @@ public static function init() { add_filter( 'template_include', array( static::class, 'comment_template_include' ) ); add_filter( 'get_comment_link', array( static::class, 'remote_comment_link' ), 11, 2 ); + + // Default Comment Status. + add_filter( 'get_default_comment_status', 'webmention_get_default_comment_status', 11, 3 ); + + add_action( 'comment_form_after', 'webmention_comment_form', 11 ); + add_action( 'comment_form_comments_closed', 'webmention_comment_form' ); } /** @@ -251,7 +257,7 @@ public static function comment_template_include( $template ) { // replace template if ( isset( $wp_query->query['replytocom'] ) ) { - return apply_filters( 'webmention_comment_template', __DIR__ . '/../templates/webmention-comment.php' ); + return apply_filters( 'webmention_comment_template', WEBMENTION_PLUGIN_DIR . '/templates/webmention-comment.php' ); } return $template; diff --git a/includes/class-db.php b/includes/class-upgrade.php similarity index 61% rename from includes/class-db.php rename to includes/class-upgrade.php index fe8299d9..88894530 100644 --- a/includes/class-db.php +++ b/includes/class-upgrade.php @@ -1,19 +1,29 @@ query( $wpdb->prepare( "INSERT IGNORE INTO `$wpdb->options` ( `option_name`, `option_value`, `autoload` ) VALUES (%s, %s, 'no') /* LOCK */", 'webmention_migration_lock', \time() ) ); // phpcs:ignore WordPress.DB + + if ( ! $lock_result ) { + $lock_result = \get_option( 'webmention_migration_lock' ); + } + + return $lock_result; + } + + /** + * Unlocks the database migration process. + */ + public static function unlock() { + \delete_option( 'webmention_migration_lock' ); + } + + /** + * Whether the database migration process is locked. + * + * @return boolean + */ + public static function is_locked() { + $lock = \get_option( 'webmention_migration_lock' ); + + if ( ! $lock ) { + return false; + } + + $lock = (int) $lock; + + if ( $lock < \time() - 1800 ) { + self::unlock(); + return false; + } + + return true; + } + /** * Updates the database structure if necessary. */ - public static function update_database() { + public static function maybe_upgrade() { if ( self::is_latest_version() ) { return; } + if ( self::is_locked() ) { + return; + } + + self::lock(); + $version_from_db = self::get_version(); if ( version_compare( $version_from_db, '1.0.0', '<' ) ) { @@ -48,7 +111,17 @@ public static function update_database() { self::migrate_to_1_0_1(); } - update_option( 'webmention_db_version', self::$target_version ); + /** + * Fires when the system has to be migrated. + * + * @param string $version_from_db The version from which to migrate. + * @param string $target_version The target version to migrate to. + */ + \do_action( 'webmention_migrate', $version_from_db, WEBMENTION_VERSION ); + + \update_option( 'webmention_db_version', WEBMENTION_VERSION ); + + self::unlock(); } /** diff --git a/includes/class-webmention.php b/includes/class-webmention.php new file mode 100644 index 00000000..167d7cb5 --- /dev/null +++ b/includes/class-webmention.php @@ -0,0 +1,203 @@ +initialized ) { + return; + } + + $this->register_constants(); + + $this->register_hooks(); + $this->register_admin_hooks(); + + $this->add_post_type_support(); + + // Load language files. + load_plugin_textdomain( self::TEXT_DOMAIN, false, dirname( plugin_basename( __FILE__ ) ) . '/languages' ); + + $this->initialized = true; + } + + /** + * Get the plugin version. + * + * @return string + */ + public function get_version() { + return WEBMENTION_VERSION; + } + + /** + * Register hooks. + */ + public function register_hooks() { + \add_action( 'init', array( Tools::class, 'init' ) ); + \add_action( 'init', array( Comment::class, 'init' ) ); + \add_action( 'init', array( Comment_Walker::class, 'init' ) ); + + // load local avatar support. + \add_action( 'init', array( Avatar::class, 'init' ) ); + + // load HTTP 410 support. + \add_action( 'init', array( HTTP_Gone::class, 'init' ) ); + + // initialize Webmention Sender. + \add_action( 'init', array( Sender::class, 'init' ) ); + + // initialize Webmention Receiver. + \add_action( 'init', array( Receiver::class, 'init' ) ); + + // initialize Webmention Discovery. + \add_action( 'init', array( Discovery::class, 'init' ) ); + + if ( site_supports_blocks() ) { + // initialize Webmention Bloks. + \add_action( 'init', array( Block::class, 'init' ) ); + } + + // load local avatar store. + if ( 1 === (int) get_option( 'webmention_avatar_store_enable', 0 ) ) { + \add_action( 'init', array( Avatar_Store::class, 'init' ) ); + } + + // initialize Webmention Vouch + if ( WEBMENTION_VOUCH ) { + \add_action( 'init', array( Vouch::class, 'init' ) ); + } + + \add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); + + // remove the "webmentions_disabled" meta value if the post is updated + \add_action( 'updated_postmeta', array( $this, 'updated_postmeta' ), 10, 4 ); + } + + /** + * Register admin hooks. + */ + public function register_admin_hooks() { + add_action( 'admin_init', array( Admin::class, 'admin_init' ) ); + add_action( 'admin_menu', array( Admin::class, 'admin_menu' ) ); + } + + /** + * Add support for Webmentions to custom post types. + */ + public function add_post_type_support() { + // Add support for Webmentions to custom post types. + $post_types = get_option( 'webmention_support_post_types', $this->default_post_types ) ? get_option( 'webmention_support_post_types', $this->default_post_types ) : array(); + + foreach ( $post_types as $post_type ) { + add_post_type_support( $post_type, 'webmentions' ); + } + } + + /** + * Register constants. + */ + public function register_constants() { + \defined( 'WEBMENTION_ALWAYS_SHOW_HEADERS' ) || \define( 'WEBMENTION_ALWAYS_SHOW_HEADERS', 0 ); + \defined( 'WEBMENTION_COMMENT_APPROVE' ) || \define( 'WEBMENTION_COMMENT_APPROVE', 0 ); + \defined( 'WEBMENTION_COMMENT_TYPE' ) || \define( 'WEBMENTION_COMMENT_TYPE', 'webmention' ); + \defined( 'WEBMENTION_GRAVATAR_CACHE_TIME' ) || \define( 'WEBMENTION_GRAVATAR_CACHE_TIME', WEEK_IN_SECONDS ); + + \defined( 'WEBMENTION_AVATAR_QUALITY' ) || \define( 'WEBMENTION_AVATAR_QUALITY', null ); + \defined( 'WEBMENTION_AVATAR_SIZE' ) || \define( 'WEBMENTION_AVATAR_SIZE', 256 ); + + \define( 'WEBMENTION_PROCESS_TYPE_ASYNC', 'async' ); + \define( 'WEBMENTION_PROCESS_TYPE_SYNC', 'sync' ); + + \defined( 'WEBMENTION_PROCESS_TYPE' ) || \define( 'WEBMENTION_PROCESS_TYPE', WEBMENTION_PROCESS_TYPE_SYNC ); + + \defined( 'WEBMENTION_VOUCH' ) || \define( 'WEBMENTION_VOUCH', false ); + + // Mentions with content less than this length will be rendered in full. + \defined( 'MAX_INLINE_MENTION_LENGTH' ) || \define( 'MAX_INLINE_MENTION_LENGTH', 300 ); + } + + /** + * Enqueue scripts. + */ + public function enqueue_scripts() { + if ( is_singular() ) { + wp_enqueue_style( self::TEXT_DOMAIN, WEBMENTION_PLUGIN_URL . 'assets/css/webmention.css', array(), $this->get_version() ); + } + } + + /** + * Delete the webmentions_disabled meta value if the post is updated. + * + * @param int $meta_id The meta ID. + * @param int $object_id The object ID. + * @param string $meta_key The meta key. + * @param mixed $meta_value The meta value. + */ + public function updated_postmeta( $meta_id, $object_id, $meta_key, $meta_value ) { + if ( 'webmentions_disabled' === $meta_key && empty( $meta_value ) ) { + \delete_post_meta( $object_id, 'webmentions_disabled' ); + } + } +} diff --git a/tests/test-db.php b/tests/test-upgrade.php similarity index 81% rename from tests/test-db.php rename to tests/test-upgrade.php index e37bccb1..c3600f6a 100644 --- a/tests/test-db.php +++ b/tests/test-upgrade.php @@ -1,7 +1,7 @@ assertEquals( \Webmention\DB::get_version(), 0 ); + $this->assertEquals( \Webmention\Upgrade::get_version(), 0 ); - \Webmention\DB::update_database(); + \Webmention\Upgrade::maybe_upgrade(); - $this->assertEquals( \Webmention\DB::get_version(), '1.0.1' ); + $this->assertEquals( \Webmention\Upgrade::get_version(), WEBMENTION_VERSION ); wp_cache_flush(); diff --git a/tests/test-webmention.php b/tests/test-webmention.php index 9d8ff359..afdc05a8 100644 --- a/tests/test-webmention.php +++ b/tests/test-webmention.php @@ -1,8 +1,21 @@ init(); - $this->assertEquals( true, true ); + $this->assertEquals( WEBMENTION_ALWAYS_SHOW_HEADERS, 0 ); + $this->assertEquals( WEBMENTION_COMMENT_APPROVE, 0 ); + $this->assertEquals( WEBMENTION_COMMENT_TYPE, 'webmention' ); + $this->assertEquals( WEBMENTION_GRAVATAR_CACHE_TIME, WEEK_IN_SECONDS ); } + + public function test_register_hooks() { + \Webmention\Webmention::get_instance()->init(); + + // test if some hooks are registered + $this->assertEquals( has_action( 'init', array( \Webmention\Comment::class, 'init' ) ), 10 ); + $this->assertEquals( has_action( 'admin_menu', array( \Webmention\Admin::class, 'admin_menu' ) ), 10 ); + $this->assertEquals( has_action( 'admin_init', array( \Webmention\Admin::class, 'admin_init' ) ), 10 ); + } + } diff --git a/webmention.php b/webmention.php index 2fe19fb4..f4182354 100755 --- a/webmention.php +++ b/webmention.php @@ -14,183 +14,44 @@ namespace Webmention; -use WP_CLI; +\define( 'WEBMENTION_VERSION', '5.3.3' ); -define( 'WEBMENTION_VERSION', '5.3.3' ); +\define( 'WEBMENTION_PLUGIN_DIR', \plugin_dir_path( __FILE__ ) ); +\define( 'WEBMENTION_PLUGIN_BASENAME', \plugin_basename( __FILE__ ) ); +\define( 'WEBMENTION_PLUGIN_FILE', \plugin_dir_path( __FILE__ ) . '/' . \basename( __FILE__ ) ); +\define( 'WEBMENTION_PLUGIN_URL', \plugin_dir_url( __FILE__ ) ); -defined( 'WEBMENTION_ALWAYS_SHOW_HEADERS' ) || define( 'WEBMENTION_ALWAYS_SHOW_HEADERS', 0 ); -defined( 'WEBMENTION_COMMENT_APPROVE' ) || define( 'WEBMENTION_COMMENT_APPROVE', 0 ); -defined( 'WEBMENTION_COMMENT_TYPE' ) || define( 'WEBMENTION_COMMENT_TYPE', 'webmention' ); -defined( 'WEBMENTION_GRAVATAR_CACHE_TIME' ) || define( 'WEBMENTION_GRAVATAR_CACHE_TIME', WEEK_IN_SECONDS ); +require_once WEBMENTION_PLUGIN_DIR . '/includes/class-autoloader.php'; +require_once WEBMENTION_PLUGIN_DIR . '/includes/functions.php'; -defined( 'WEBMENTION_AVATAR_QUALITY' ) || define( 'WEBMENTION_AVATAR_QUALITY', null ); -defined( 'WEBMENTION_AVATAR_SIZE' ) || define( 'WEBMENTION_AVATAR_SIZE', 256 ); - -define( 'WEBMENTION_PROCESS_TYPE_ASYNC', 'async' ); -define( 'WEBMENTION_PROCESS_TYPE_SYNC', 'sync' ); - -defined( 'WEBMENTION_PROCESS_TYPE' ) || define( 'WEBMENTION_PROCESS_TYPE', WEBMENTION_PROCESS_TYPE_SYNC ); - -defined( 'WEBMENTION_VOUCH' ) || define( 'WEBMENTION_VOUCH', false ); - -// Mentions with content less than this length will be rendered in full. -defined( 'MAX_INLINE_MENTION_LENGTH' ) || define( 'MAX_INLINE_MENTION_LENGTH', 300 ); +if ( \WP_DEBUG ) { + require_once WEBMENTION_PLUGIN_DIR . '/includes/debug.php'; +} -\define( 'WEBMENTION_PLUGIN_DIR', plugin_dir_path( __FILE__ ) ); -\define( 'WEBMENTION_PLUGIN_BASENAME', plugin_basename( __FILE__ ) ); -\define( 'WEBMENTION_PLUGIN_FILE', plugin_dir_path( __FILE__ ) . '/' . basename( __FILE__ ) ); -\define( 'WEBMENTION_PLUGIN_URL', plugin_dir_url( __FILE__ ) ); +// Register the autoloader. +Autoloader::register_path( __NAMESPACE__, WEBMENTION_PLUGIN_DIR . '/includes' ); -// initialize admin settings. -require_once __DIR__ . '/includes/class-admin.php'; -add_action( 'admin_init', array( '\Webmention\Admin', 'admin_init' ) ); -add_action( 'admin_menu', array( '\Webmention\Admin', 'admin_menu' ) ); +// Initialize the plugin. +$webmention = Webmention::get_instance(); +$webmention->init(); /** * Plugin Version Number used for caching. */ function version() { - if ( defined( 'WEBMENTION_VERSION' ) ) { - return WEBMENTION_VERSION; - } - - $meta = get_plugin_meta( array( 'Version' => 'Version' ) ); - - return $meta['Version']; + return Webmention::get_instance()->get_version(); } -/** - * Initialize Webmention Plugin - */ -function init() { - // Add support for Webmentions to custom post types. - $post_types = get_option( 'webmention_support_post_types', array( 'post', 'page' ) ) ? get_option( 'webmention_support_post_types', array( 'post', 'page' ) ) : array(); - - foreach ( $post_types as $post_type ) { - add_post_type_support( $post_type, 'webmentions' ); - } - if ( WP_DEBUG ) { - require_once __DIR__ . '/includes/debug.php'; - } - - require_once __DIR__ . '/includes/class-tools.php'; - add_action( 'init', array( '\Webmention\Tools', 'init' ) ); - - // Request Handler. - require_once __DIR__ . '/includes/class-request.php'; - require_once __DIR__ . '/includes/class-response.php'; - - // Comment Handler Classes. - require_once __DIR__ . '/includes/class-comment-type.php'; - require_once __DIR__ . '/includes/class-comment.php'; - add_action( 'init', array( '\Webmention\Comment', 'init' ) ); - - require_once __DIR__ . '/includes/class-comment-walker.php'; - add_action( 'init', array( '\Webmention\Comment_Walker', 'init' ) ); - - // Handler Control Class. - require_once __DIR__ . '/includes/class-handler.php'; - require_once __DIR__ . '/includes/Handler/class-base.php'; - - // Webmention Item Class - require_once __DIR__ . '/includes/Entity/class-item.php'; - - // list of various public helper functions. - require_once __DIR__ . '/includes/functions.php'; - - // load local avatar support. - require_once __DIR__ . '/includes/class-avatar.php'; - add_action( 'init', array( '\Webmention\Avatar', 'init' ) ); - - // load HTTP 410 support. - require_once __DIR__ . '/includes/class-http-gone.php'; - add_action( 'init', array( '\Webmention\HTTP_Gone', 'init' ) ); - - // initialize Webmention Sender. - require_once __DIR__ . '/includes/class-sender.php'; - add_action( 'init', array( '\Webmention\Sender', 'init' ) ); - - // initialize Webmention Receiver. - require_once __DIR__ . '/includes/class-receiver.php'; - add_action( 'init', array( '\Webmention\Receiver', 'init' ) ); - - // initialize Webmention Discovery. - require_once __DIR__ . '/includes/class-discovery.php'; - add_action( 'init', array( '\Webmention\Discovery', 'init' ) ); - - if ( site_supports_blocks() ) { - // initialize Webmention Bloks. - require_once __DIR__ . '/includes/class-block.php'; - add_action( 'init', array( '\Webmention\Block', 'init' ) ); - } - - // load local avatar store. - if ( 1 === (int) get_option( 'webmention_avatar_store_enable', 0 ) ) { - require_once __DIR__ . '/includes/class-avatar-store.php'; - add_action( 'init', array( '\Webmention\Avatar_Store', 'init' ) ); - } - - // initialize Webmention Vouch - if ( WEBMENTION_VOUCH ) { - require_once __DIR__ . '/includes/class-vouch.php'; - add_action( 'init', array( '\Webmention\Vouch', 'init' ) ); - } - - // Default Comment Status. - add_filter( 'get_default_comment_status', 'webmention_get_default_comment_status', 11, 3 ); - - // Load language files. - load_plugin_textdomain( 'webmention', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' ); - - add_action( 'comment_form_after', 'webmention_comment_form', 11 ); - add_action( 'comment_form_comments_closed', 'webmention_comment_form' ); - - // remove old Webmention code. - remove_action( 'init', array( '\WebMentionFormPlugin', 'init' ) ); - remove_action( 'init', array( '\WebMentionForCommentsPlugin', 'init' ) ); - - // remove old Semantic Linkbacks code - remove_action( 'plugins_loaded', array( 'Semantic_Linkbacks_Plugin', 'init' ), 11 ); - remove_action( 'admin_init', array( 'Semantic_Linkbacks_Plugin', 'admin_init' ) ); - - add_action( 'wp_enqueue_scripts', '\Webmention\enqueue_scripts' ); - - // remove the "webmentions_disabled" meta value if the post is updated - \add_action( - 'updated_postmeta', - function ( $meta_id, $object_id, $meta_key, $meta_value ) { - if ( 'webmentions_disabled' === $meta_key && empty( $meta_value ) ) { - \delete_post_meta( $object_id, 'webmentions_disabled' ); - } - }, - 10, - 4 - ); -} -add_action( 'plugins_loaded', '\Webmention\init' ); - /** * Activation Hook * * Migrate DB if needed */ function activation() { - require_once __DIR__ . '/includes/class-db.php'; - \Webmention\DB::update_database(); - - \Webmention\remove_semantic_linkbacks(); + \Webmention\Upgrade::maybe_upgrade(); } register_activation_hook( __FILE__, '\Webmention\activation' ); -/** - * Add CSS and JavaScript - */ -function enqueue_scripts() { - if ( \is_singular() ) { - wp_enqueue_style( 'webmention', plugin_dir_url( __FILE__ ) . 'assets/css/webmention.css', array(), version() ); - } -} - /** * `get_plugin_data` wrapper * @@ -218,27 +79,6 @@ function get_plugin_meta( $default_headers = array() ) { } // Check for CLI env, to add the CLI commands -if ( defined( 'WP_CLI' ) && WP_CLI ) { - require_once __DIR__ . '/includes/class-cli.php'; - WP_CLI::add_command( 'webmention', '\Webmention\Cli' ); -} - -/** - * Remove the Semantic Linkbacks plugin - * - * @since 5.0.0 - * - * @return void - */ -function remove_semantic_linkbacks() { - require_once ABSPATH . 'wp-admin/includes/plugin.php'; - require_once ABSPATH . 'wp-admin/includes/file.php'; - - $plugin_slug = 'semantic-linkbacks/semantic-linkbacks.php'; - $installed_plugins = get_plugins(); - - if ( array_key_exists( $plugin_slug, $installed_plugins ) ) { - \deactivate_plugins( array( $plugin_slug ), true ); - \delete_plugins( array( $plugin_slug ) ); - } +if ( \defined( 'WP_CLI' ) && \WP_CLI ) { + \WP_CLI::add_command( 'webmention', Cli::class ); }