PluginProbe
ActivityPub / 4.0.1
ActivityPub v4.0.1
9.3.1 9.3.0 9.2.2 9.2.1 9.2.0 9.1.0 9.0.2 9.0.1 9.0.0 8.3.0 8.2.1 8.2.0 8.1.1 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.2.0 1.3.0 2.0.0 2.0.1 2.1.0 2.1.1 All 160 releases
activitypub / activitypub.php

activitypub.php in ActivityPub 4.0.1, at activitypub.php

248 lines 8.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugin Name: ActivityPub
4 * Plugin URI: https://github.com/pfefferle/wordpress-activitypub/
5 * Description: The ActivityPub protocol is a decentralized social networking protocol based upon the ActivityStreams 2.0 data format.
6 * Version: 4.0.1
7 * Author: Matthias Pfefferle & Automattic
8 * Author URI: https://automattic.com/
9 * License: MIT
10 * License URI: http://opensource.org/licenses/MIT
11 * Requires PHP: 7.0
12 * Text Domain: activitypub
13 * Domain Path: /languages
14 *
15 * @package Activitypub
16 */
17
18 namespace Activitypub;
19
20 use WP_CLI;
21
22 require_once __DIR__ . '/includes/compat.php';
23 require_once __DIR__ . '/includes/functions.php';
24
25 \define( 'ACTIVITYPUB_PLUGIN_VERSION', '4.0.1' );
26
27 /**
28 * Initialize the plugin constants.
29 */
30 \defined( 'ACTIVITYPUB_REST_NAMESPACE' ) || \define( 'ACTIVITYPUB_REST_NAMESPACE', 'activitypub/1.0' );
31 \defined( 'ACTIVITYPUB_EXCERPT_LENGTH' ) || \define( 'ACTIVITYPUB_EXCERPT_LENGTH', 400 );
32 \defined( 'ACTIVITYPUB_NOTE_LENGTH' ) || \define( 'ACTIVITYPUB_NOTE_LENGTH', 400 );
33 \defined( 'ACTIVITYPUB_SHOW_PLUGIN_RECOMMENDATIONS' ) || \define( 'ACTIVITYPUB_SHOW_PLUGIN_RECOMMENDATIONS', true );
34 \defined( 'ACTIVITYPUB_MAX_IMAGE_ATTACHMENTS' ) || \define( 'ACTIVITYPUB_MAX_IMAGE_ATTACHMENTS', 3 );
35 \defined( 'ACTIVITYPUB_HASHTAGS_REGEXP' ) || \define( 'ACTIVITYPUB_HASHTAGS_REGEXP', '(?:(?<=\s)|(?<=<p>)|(?<=<br>)|^)#([A-Za-z0-9_]+)(?:(?=\s|[[:punct:]]|$))' );
36 \defined( 'ACTIVITYPUB_USERNAME_REGEXP' ) || \define( 'ACTIVITYPUB_USERNAME_REGEXP', '(?:([A-Za-z0-9\._-]+)@((?:[A-Za-z0-9_-]+\.)+[A-Za-z]+))' );
37 \defined( 'ACTIVITYPUB_URL_REGEXP' ) || \define( 'ACTIVITYPUB_URL_REGEXP', '(https?:|www\.)\S+[\w\/]' );
38 \defined( 'ACTIVITYPUB_CUSTOM_POST_CONTENT' ) || \define( 'ACTIVITYPUB_CUSTOM_POST_CONTENT', "<h2>[ap_title]</h2>\n\n[ap_content]\n\n[ap_hashtags]\n\n[ap_shortlink]" );
39 \defined( 'ACTIVITYPUB_AUTHORIZED_FETCH' ) || \define( 'ACTIVITYPUB_AUTHORIZED_FETCH', false );
40 \defined( 'ACTIVITYPUB_DISABLE_REWRITES' ) || \define( 'ACTIVITYPUB_DISABLE_REWRITES', false );
41 \defined( 'ACTIVITYPUB_DISABLE_INCOMING_INTERACTIONS' ) || \define( 'ACTIVITYPUB_DISABLE_INCOMING_INTERACTIONS', false );
42 // Disable reactions like `Like` and `Announce` by default.
43 \defined( 'ACTIVITYPUB_DISABLE_REACTIONS' ) || \define( 'ACTIVITYPUB_DISABLE_REACTIONS', true );
44 \defined( 'ACTIVITYPUB_DISABLE_OUTGOING_INTERACTIONS' ) || \define( 'ACTIVITYPUB_DISABLE_OUTGOING_INTERACTIONS', false );
45 \defined( 'ACTIVITYPUB_SHARED_INBOX_FEATURE' ) || \define( 'ACTIVITYPUB_SHARED_INBOX_FEATURE', false );
46 \defined( 'ACTIVITYPUB_SEND_VARY_HEADER' ) || \define( 'ACTIVITYPUB_SEND_VARY_HEADER', false );
47 \defined( 'ACTIVITYPUB_DEFAULT_OBJECT_TYPE' ) || \define( 'ACTIVITYPUB_DEFAULT_OBJECT_TYPE', 'note' );
48
49 // Define Actor-Modes for the plugin.
50 \define( 'ACTIVITYPUB_ACTOR_MODE', 'actor' );
51 \define( 'ACTIVITYPUB_BLOG_MODE', 'blog' );
52 \define( 'ACTIVITYPUB_ACTOR_AND_BLOG_MODE', 'actor_blog' );
53
54 // Post visibility constants.
55 \define( 'ACTIVITYPUB_CONTENT_VISIBILITY_PUBLIC', '' );
56 \define( 'ACTIVITYPUB_CONTENT_VISIBILITY_QUIET_PUBLIC', 'quiet_public' );
57 \define( 'ACTIVITYPUB_CONTENT_VISIBILITY_LOCAL', 'local' );
58
59 // Plugin related constants.
60 \define( 'ACTIVITYPUB_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
61 \define( 'ACTIVITYPUB_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );
62 \define( 'ACTIVITYPUB_PLUGIN_FILE', plugin_dir_path( __FILE__ ) . '/' . basename( __FILE__ ) );
63 \define( 'ACTIVITYPUB_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
64
65 /**
66 * Initialize REST routes.
67 */
68 function rest_init() {
69 Rest\Actors::init();
70 Rest\Outbox::init();
71 Rest\Inbox::init();
72 Rest\Followers::init();
73 Rest\Following::init();
74 Rest\Webfinger::init();
75 Rest\Comment::init();
76 Rest\Server::init();
77 Rest\Collection::init();
78 Rest\Interaction::init();
79
80 // Load NodeInfo endpoints only if blog is public.
81 if ( is_blog_public() ) {
82 Rest\NodeInfo::init();
83 }
84 }
85 \add_action( 'rest_api_init', __NAMESPACE__ . '\rest_init' );
86
87 /**
88 * Initialize plugin.
89 */
90 function plugin_init() {
91 \add_action( 'init', array( __NAMESPACE__ . '\Migration', 'init' ) );
92 \add_action( 'init', array( __NAMESPACE__ . '\Activitypub', 'init' ) );
93 \add_action( 'init', array( __NAMESPACE__ . '\Activity_Dispatcher', 'init' ) );
94 \add_action( 'init', array( __NAMESPACE__ . '\Handler', 'init' ) );
95 \add_action( 'init', array( __NAMESPACE__ . '\Admin', 'init' ) );
96 \add_action( 'init', array( __NAMESPACE__ . '\Hashtag', 'init' ) );
97 \add_action( 'init', array( __NAMESPACE__ . '\Mention', 'init' ) );
98 \add_action( 'init', array( __NAMESPACE__ . '\Health_Check', 'init' ) );
99 \add_action( 'init', array( __NAMESPACE__ . '\Scheduler', 'init' ) );
100 \add_action( 'init', array( __NAMESPACE__ . '\Comment', 'init' ) );
101 \add_action( 'init', array( __NAMESPACE__ . '\Link', 'init' ) );
102
103 if ( site_supports_blocks() ) {
104 \add_action( 'init', array( __NAMESPACE__ . '\Blocks', 'init' ) );
105 }
106
107 $debug_file = __DIR__ . '/includes/debug.php';
108 if ( \WP_DEBUG && file_exists( $debug_file ) && is_readable( $debug_file ) ) {
109 require_once $debug_file;
110 Debug::init();
111 }
112 }
113 \add_action( 'plugins_loaded', __NAMESPACE__ . '\plugin_init' );
114
115
116 /**
117 * Class Autoloader.
118 */
119 \spl_autoload_register(
120 function ( $full_class ) {
121 $base_dir = __DIR__ . '/includes/';
122 $base = 'Activitypub\\';
123
124 if ( strncmp( $full_class, $base, strlen( $base ) ) === 0 ) {
125 $maybe_uppercase = str_replace( $base, '', $full_class );
126 $class = strtolower( $maybe_uppercase );
127 // All classes should be capitalized. If this is instead looking for a lowercase method, we ignore that.
128 if ( $maybe_uppercase === $class ) {
129 return;
130 }
131
132 if ( false !== strpos( $class, '\\' ) ) {
133 $parts = explode( '\\', $class );
134 $class = array_pop( $parts );
135 $sub_dir = strtr( implode( '/', $parts ), '_', '-' );
136 $base_dir = $base_dir . $sub_dir . '/';
137 }
138
139 $filename = 'class-' . strtr( $class, '_', '-' );
140 $file = $base_dir . $filename . '.php';
141
142 if ( file_exists( $file ) && is_readable( $file ) ) {
143 require_once $file;
144 } else {
145 // translators: %s is the class name.
146 $message = sprintf( esc_html__( 'Required class not found or not readable: %s', 'activitypub' ), esc_html( $full_class ) );
147 Debug::write_log( $message );
148 \wp_die( $message ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
149 }
150 }
151 }
152 );
153
154 /**
155 * Add plugin settings link.
156 *
157 * @param array $actions The current actions.
158 */
159 function plugin_settings_link( $actions ) {
160 $settings_link = array();
161 $settings_link[] = \sprintf(
162 '<a href="%1s">%2s</a>',
163 \menu_page_url( 'activitypub', false ),
164 \__( 'Settings', 'activitypub' )
165 );
166
167 return \array_merge( $settings_link, $actions );
168 }
169 \add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), __NAMESPACE__ . '\plugin_settings_link' );
170
171 \register_activation_hook(
172 __FILE__,
173 array(
174 __NAMESPACE__ . '\Activitypub',
175 'activate',
176 )
177 );
178
179 \register_deactivation_hook(
180 __FILE__,
181 array(
182 __NAMESPACE__ . '\Activitypub',
183 'deactivate',
184 )
185 );
186
187 \register_uninstall_hook(
188 __FILE__,
189 array(
190 __NAMESPACE__ . '\Activitypub',
191 'uninstall',
192 )
193 );
194
195 // Load integrations.
196 require_once __DIR__ . '/integration/load.php';
197
198 /**
199 * `get_plugin_data` wrapper.
200 *
201 * @param array $default_headers Optional. The default plugin headers. Default empty array.
202 * @return array The plugin metadata array.
203 */
204 function get_plugin_meta( $default_headers = array() ) {
205 if ( ! $default_headers ) {
206 $default_headers = array(
207 'Name' => 'Plugin Name',
208 'PluginURI' => 'Plugin URI',
209 'Version' => 'Version',
210 'Description' => 'Description',
211 'Author' => 'Author',
212 'AuthorURI' => 'Author URI',
213 'TextDomain' => 'Text Domain',
214 'DomainPath' => 'Domain Path',
215 'Network' => 'Network',
216 'RequiresWP' => 'Requires at least',
217 'RequiresPHP' => 'Requires PHP',
218 'UpdateURI' => 'Update URI',
219 );
220 }
221
222 return \get_file_data( __FILE__, $default_headers, 'plugin' );
223 }
224
225 /**
226 * Plugin Version Number used for caching.
227 */
228 function get_plugin_version() {
229 if ( \defined( 'ACTIVITYPUB_PLUGIN_VERSION' ) ) {
230 return ACTIVITYPUB_PLUGIN_VERSION;
231 }
232
233 $meta = get_plugin_meta( array( 'Version' => 'Version' ) );
234
235 return $meta['Version'];
236 }
237
238 // Check for CLI env, to add the CLI commands.
239 if ( defined( 'WP_CLI' ) && WP_CLI ) {
240 WP_CLI::add_command(
241 'activitypub',
242 '\Activitypub\Cli',
243 array(
244 'shortdesc' => __( 'ActivityPub related commands: Meta-Infos, Delete and soon Self-Destruct.', 'activitypub' ),
245 )
246 );
247 }
248