wordpress-activitypub/activitypub.php
Jeremy Herve fe07d5eb32
Blocks: short-circuit early on sites that do not support blocks (#431)
* Blocks: short-circuit early on sites that do not support blocks

Fixes #430

This is typically only the case for sites using a custom version of WordPress, like ClassicPress.

* let grunt build the markdown

* Check for block support earlier and add filter

One can now deactivate the blocks registered by ActivityPub like so:

```
add_filter( 'activitypub_site_supports_blocks', '__return_false' );
```

* Fix readme (gotta remember to use grunt)

* alias function

---------

Co-authored-by: Matthias Pfefferle <pfefferle@users.noreply.github.com>
2023-09-15 10:38:47 +02:00

200 lines
5.6 KiB
PHP

<?php
/**
* Plugin Name: ActivityPub
* Plugin URI: https://github.com/pfefferle/wordpress-activitypub/
* Description: The ActivityPub protocol is a decentralized social networking protocol based upon the ActivityStreams 2.0 data format.
* Version: 1.0.0
* Author: Matthias Pfefferle & Automattic
* Author URI: https://automattic.com/
* License: MIT
* License URI: http://opensource.org/licenses/MIT
* Requires PHP: 5.6
* Text Domain: activitypub
* Domain Path: /languages
*/
namespace Activitypub;
use function Activitypub\site_supports_blocks;
\defined( 'ACTIVITYPUB_REST_NAMESPACE' ) || \define( 'ACTIVITYPUB_REST_NAMESPACE', 'activitypub/1.0' );
/**
* Initialize plugin
*/
function init() {
\defined( 'ACTIVITYPUB_EXCERPT_LENGTH' ) || \define( 'ACTIVITYPUB_EXCERPT_LENGTH', 400 );
\defined( 'ACTIVITYPUB_SHOW_PLUGIN_RECOMMENDATIONS' ) || \define( 'ACTIVITYPUB_SHOW_PLUGIN_RECOMMENDATIONS', true );
\defined( 'ACTIVITYPUB_MAX_IMAGE_ATTACHMENTS' ) || \define( 'ACTIVITYPUB_MAX_IMAGE_ATTACHMENTS', 3 );
\defined( 'ACTIVITYPUB_HASHTAGS_REGEXP' ) || \define( 'ACTIVITYPUB_HASHTAGS_REGEXP', '(?:(?<=\s)|(?<=<p>)|(?<=<br>)|^)#([A-Za-z0-9_]+)(?:(?=\s|[[:punct:]]|$))' );
\defined( 'ACTIVITYPUB_USERNAME_REGEXP' ) || \define( 'ACTIVITYPUB_USERNAME_REGEXP', '(?:([A-Za-z0-9_-]+)@((?:[A-Za-z0-9_-]+\.)+[A-Za-z]+))' );
\defined( 'ACTIVITYPUB_CUSTOM_POST_CONTENT' ) || \define( 'ACTIVITYPUB_CUSTOM_POST_CONTENT', "<strong>[ap_title]</strong>\n\n[ap_content]\n\n[ap_hashtags]\n\n[ap_shortlink]" );
\defined( 'ACTIVITYPUB_AUTHORIZED_FETCH' ) || \define( 'ACTIVITYPUB_AUTHORIZED_FETCH', false );
\define( 'ACTIVITYPUB_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
\define( 'ACTIVITYPUB_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );
\define( 'ACTIVITYPUB_PLUGIN_FILE', plugin_dir_path( __FILE__ ) . '/' . basename( __FILE__ ) );
\define( 'ACTIVITYPUB_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
Migration::init();
Activitypub::init();
Activity_Dispatcher::init();
Collection\Followers::init();
// Configure the REST API route
Rest\Users::init();
Rest\Outbox::init();
Rest\Inbox::init();
Rest\Followers::init();
Rest\Following::init();
Rest\Webfinger::init();
Rest\Server::init();
Rest\Collection::init();
Admin::init();
Hashtag::init();
Shortcodes::init();
Mention::init();
Health_Check::init();
Scheduler::init();
if ( site_supports_blocks() ) {
Blocks::init();
}
}
\add_action( 'init', __NAMESPACE__ . '\init' );
/**
* Class Autoloader
*/
spl_autoload_register(
function ( $full_class ) {
$base_dir = __DIR__ . '/includes/';
$base = 'Activitypub\\';
if ( strncmp( $full_class, $base, strlen( $base ) ) === 0 ) {
$maybe_uppercase = str_replace( $base, '', $full_class );
$class = strtolower( $maybe_uppercase );
// All classes should be capitalized. If this is instead looking for a lowercase method, we ignore that.
if ( $maybe_uppercase === $class ) {
return;
}
if ( false !== strpos( $class, '\\' ) ) {
$parts = explode( '\\', $class );
$class = array_pop( $parts );
$sub_dir = implode( '/', $parts );
$base_dir = $base_dir . $sub_dir . '/';
}
$filename = 'class-' . strtr( $class, '_', '-' );
$file = $base_dir . $filename . '.php';
if ( file_exists( $file ) && is_readable( $file ) ) {
require_once $file;
} else {
// translators: %s is the class name
\wp_die( sprintf( esc_html__( 'Required class not found or not readable: %s', 'activitypub' ), esc_html( $full_class ) ) );
}
}
}
);
require_once __DIR__ . '/includes/functions.php';
// load NodeInfo endpoints only if blog is public
if ( \get_option( 'blog_public', 1 ) ) {
Rest\NodeInfo::init();
}
$debug_file = __DIR__ . '/includes/debug.php';
if ( \WP_DEBUG && file_exists( $debug_file ) && is_readable( $debug_file ) ) {
require_once $debug_file;
Debug::init();
}
/**
* Add plugin settings link
*/
function plugin_settings_link( $actions ) {
$settings_link = array();
$settings_link[] = \sprintf(
'<a href="%1s">%2s</a>',
\menu_page_url( 'activitypub', false ),
\__( 'Settings', 'activitypub' )
);
return \array_merge( $settings_link, $actions );
}
\add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), __NAMESPACE__ . '\plugin_settings_link' );
\register_activation_hook(
__FILE__,
array(
__NAMESPACE__ . '\Activitypub',
'activate',
)
);
\register_deactivation_hook(
__FILE__,
array(
__NAMESPACE__ . '\Activitypub',
'deactivate',
)
);
\register_uninstall_hook(
__FILE__,
array(
__NAMESPACE__ . '\Activitypub',
'uninstall',
)
);
/**
* Only load code that needs BuddyPress to run once BP is loaded and initialized.
*/
add_action(
'bp_include',
function() {
require_once __DIR__ . '/integration/class-buddypress.php';
Integration\Buddypress::init();
},
0
);
/**
* `get_plugin_data` wrapper
*
* @return array The plugin metadata array
*/
function get_plugin_meta( $default_headers = array() ) {
if ( ! $default_headers ) {
$default_headers = array(
'Name' => 'Plugin Name',
'PluginURI' => 'Plugin URI',
'Version' => 'Version',
'Description' => 'Description',
'Author' => 'Author',
'AuthorURI' => 'Author URI',
'TextDomain' => 'Text Domain',
'DomainPath' => 'Domain Path',
'Network' => 'Network',
'RequiresWP' => 'Requires at least',
'RequiresPHP' => 'Requires PHP',
'UpdateURI' => 'Update URI',
);
}
return \get_file_data( __FILE__, $default_headers, 'plugin' );
}
/**
* Plugin Version Number used for caching.
*/
function get_plugin_version() {
$meta = get_plugin_meta( array( 'Version' => 'Version' ) );
return $meta['Version'];
}