|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Webmention Class |
| 4 | + * |
| 5 | + * @package Webmention |
| 6 | + */ |
| 7 | + |
| 8 | +namespace Webmention; |
| 9 | + |
| 10 | +/** |
| 11 | + * Webmention Class |
| 12 | + * |
| 13 | + * @package Webmention |
| 14 | + */ |
| 15 | +class Webmention { |
| 16 | + /** |
| 17 | + * Instance of the class. |
| 18 | + * |
| 19 | + * @var Webmention |
| 20 | + */ |
| 21 | + private static $instance; |
| 22 | + |
| 23 | + /** |
| 24 | + * Text domain. |
| 25 | + * |
| 26 | + * @var string |
| 27 | + */ |
| 28 | + const TEXT_DOMAIN = 'webmention'; |
| 29 | + |
| 30 | + /** |
| 31 | + * Get the instance of the class. |
| 32 | + * |
| 33 | + * @return Webmention |
| 34 | + */ |
| 35 | + public static function get_instance() { |
| 36 | + if ( null === self::$instance ) { |
| 37 | + self::$instance = new self(); |
| 38 | + } |
| 39 | + |
| 40 | + return self::$instance; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Do not allow multiple instances of the class. |
| 45 | + */ |
| 46 | + private function __construct() { |
| 47 | + // Do nothing. |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Initialize the plugin. |
| 52 | + */ |
| 53 | + public function init() { |
| 54 | + $this->register_constants(); |
| 55 | + |
| 56 | + $this->register_hooks(); |
| 57 | + $this->register_admin_hooks(); |
| 58 | + $this->add_post_type_support(); |
| 59 | + $this->unregister_legacy_hooks(); |
| 60 | + |
| 61 | + // Load language files. |
| 62 | + load_plugin_textdomain( self::TEXT_DOMAIN, false, dirname( plugin_basename( __FILE__ ) ) . '/languages' ); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Get the plugin version. |
| 67 | + * |
| 68 | + * @return string |
| 69 | + */ |
| 70 | + public function get_version() { |
| 71 | + return WEBMENTION_VERSION; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Register hooks. |
| 76 | + */ |
| 77 | + public function register_hooks() { |
| 78 | + \add_action( 'init', array( Tools::class, 'init' ) ); |
| 79 | + \add_action( 'init', array( Comment::class, 'init' ) ); |
| 80 | + \add_action( 'init', array( Comment_Walker::class, 'init' ) ); |
| 81 | + |
| 82 | + // load local avatar support. |
| 83 | + \add_action( 'init', array( Avatar::class, 'init' ) ); |
| 84 | + |
| 85 | + // load HTTP 410 support. |
| 86 | + \add_action( 'init', array( HTTP_Gone::class, 'init' ) ); |
| 87 | + |
| 88 | + // initialize Webmention Sender. |
| 89 | + \add_action( 'init', array( Sender::class, 'init' ) ); |
| 90 | + |
| 91 | + // initialize Webmention Receiver. |
| 92 | + \add_action( 'init', array( Receiver::class, 'init' ) ); |
| 93 | + |
| 94 | + // initialize Webmention Discovery. |
| 95 | + \add_action( 'init', array( Discovery::class, 'init' ) ); |
| 96 | + |
| 97 | + if ( site_supports_blocks() ) { |
| 98 | + // initialize Webmention Bloks. |
| 99 | + \add_action( 'init', array( Block::class, 'init' ) ); |
| 100 | + } |
| 101 | + |
| 102 | + // load local avatar store. |
| 103 | + if ( 1 === (int) get_option( 'webmention_avatar_store_enable', 0 ) ) { |
| 104 | + \add_action( 'init', array( Avatar_Store::class, 'init' ) ); |
| 105 | + } |
| 106 | + |
| 107 | + // initialize Webmention Vouch |
| 108 | + if ( WEBMENTION_VOUCH ) { |
| 109 | + \add_action( 'init', array( Vouch::class, 'init' ) ); |
| 110 | + } |
| 111 | + |
| 112 | + \add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); |
| 113 | + |
| 114 | + // remove the "webmentions_disabled" meta value if the post is updated |
| 115 | + \add_action( 'updated_postmeta', array( $this, 'updated_postmeta' ), 10, 4 ); |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Register admin hooks. |
| 120 | + */ |
| 121 | + public function register_admin_hooks() { |
| 122 | + add_action( 'admin_init', array( Admin::class, 'admin_init' ) ); |
| 123 | + add_action( 'admin_menu', array( Admin::class, 'admin_menu' ) ); |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Add support for Webmentions to custom post types. |
| 128 | + */ |
| 129 | + public function add_post_type_support() { |
| 130 | + // Add support for Webmentions to custom post types. |
| 131 | + $post_types = get_option( 'webmention_support_post_types', array( 'post', 'page' ) ) ? get_option( 'webmention_support_post_types', array( 'post', 'page' ) ) : array(); |
| 132 | + |
| 133 | + foreach ( $post_types as $post_type ) { |
| 134 | + add_post_type_support( $post_type, 'webmentions' ); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Register constants. |
| 140 | + */ |
| 141 | + private function register_constants() { |
| 142 | + \defined( 'WEBMENTION_ALWAYS_SHOW_HEADERS' ) || \define( 'WEBMENTION_ALWAYS_SHOW_HEADERS', 0 ); |
| 143 | + \defined( 'WEBMENTION_COMMENT_APPROVE' ) || \define( 'WEBMENTION_COMMENT_APPROVE', 0 ); |
| 144 | + \defined( 'WEBMENTION_COMMENT_TYPE' ) || \define( 'WEBMENTION_COMMENT_TYPE', 'webmention' ); |
| 145 | + \defined( 'WEBMENTION_GRAVATAR_CACHE_TIME' ) || \define( 'WEBMENTION_GRAVATAR_CACHE_TIME', WEEK_IN_SECONDS ); |
| 146 | + |
| 147 | + \defined( 'WEBMENTION_AVATAR_QUALITY' ) || \define( 'WEBMENTION_AVATAR_QUALITY', null ); |
| 148 | + \defined( 'WEBMENTION_AVATAR_SIZE' ) || \define( 'WEBMENTION_AVATAR_SIZE', 256 ); |
| 149 | + |
| 150 | + \define( 'WEBMENTION_PROCESS_TYPE_ASYNC', 'async' ); |
| 151 | + \define( 'WEBMENTION_PROCESS_TYPE_SYNC', 'sync' ); |
| 152 | + |
| 153 | + \defined( 'WEBMENTION_PROCESS_TYPE' ) || \define( 'WEBMENTION_PROCESS_TYPE', WEBMENTION_PROCESS_TYPE_SYNC ); |
| 154 | + |
| 155 | + \defined( 'WEBMENTION_VOUCH' ) || \define( 'WEBMENTION_VOUCH', false ); |
| 156 | + |
| 157 | + // Mentions with content less than this length will be rendered in full. |
| 158 | + \defined( 'MAX_INLINE_MENTION_LENGTH' ) || \define( 'MAX_INLINE_MENTION_LENGTH', 300 ); |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Enqueue scripts. |
| 163 | + */ |
| 164 | + public function enqueue_scripts() { |
| 165 | + if ( is_singular() ) { |
| 166 | + wp_enqueue_style( self::TEXT_DOMAIN, WEBMENTION_PLUGIN_URL . 'assets/css/webmention.css', array(), $this->get_version() ); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * Delete the webmentions_disabled meta value if the post is updated. |
| 172 | + * |
| 173 | + * @param int $meta_id The meta ID. |
| 174 | + * @param int $object_id The object ID. |
| 175 | + * @param string $meta_key The meta key. |
| 176 | + * @param mixed $meta_value The meta value. |
| 177 | + */ |
| 178 | + public function updated_postmeta( $meta_id, $object_id, $meta_key, $meta_value ) { |
| 179 | + if ( 'webmentions_disabled' === $meta_key && empty( $meta_value ) ) { |
| 180 | + \delete_post_meta( $object_id, 'webmentions_disabled' ); |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + /** |
| 185 | + * Remove old Webmention/Semantic-Linkbacks code. |
| 186 | + */ |
| 187 | + public function unregister_legacy_hooks() { |
| 188 | + // remove old Webmention code. |
| 189 | + remove_action( 'init', array( '\WebMentionFormPlugin', 'init' ) ); |
| 190 | + remove_action( 'init', array( '\WebMentionForCommentsPlugin', 'init' ) ); |
| 191 | + |
| 192 | + // remove old Semantic Linkbacks code |
| 193 | + remove_action( 'plugins_loaded', array( 'Semantic_Linkbacks_Plugin', 'init' ), 11 ); |
| 194 | + remove_action( 'admin_init', array( 'Semantic_Linkbacks_Plugin', 'admin_init' ) ); |
| 195 | + } |
| 196 | +} |
0 commit comments