102 lines
No EOL
2.6 KiB
PHP
102 lines
No EOL
2.6 KiB
PHP
<?php
|
|
/**
|
|
* Inspired by the PHP ActivityPub Library by @Landrok
|
|
*
|
|
* @link https://github.com/landrok/activitypub
|
|
*/
|
|
|
|
namespace Activitypub;
|
|
use WP_Post;
|
|
|
|
/**
|
|
* Base class to implement WordPress to ActivityPub transformers.
|
|
*/
|
|
abstract class Transformer_Base {
|
|
/**
|
|
* The WP_Post object.
|
|
*
|
|
* @var WP_Post
|
|
*/
|
|
protected $wp_post;
|
|
|
|
/**
|
|
* Assign WP_Post Object to a specific transformer instance.
|
|
*
|
|
* This helps to chain the output of the Transformer.
|
|
*
|
|
* @param WP_Post $wp_post The WP_Post object
|
|
*
|
|
* @return void
|
|
*/
|
|
public function set_wp_post( WP_Post $wp_post ) {
|
|
if ( $this->supports_post_type( get_post_type( $wp_post ) ) ) {
|
|
$this->wp_post = $wp_post;
|
|
} else {
|
|
//TODO Error, this should not happen.
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get the supported WP post_types that the transformer can use as an input.
|
|
*
|
|
* By default all post types are supported.
|
|
* You may very likely wish to override this function.
|
|
*
|
|
* @since version_number_transformer_management_placeholder
|
|
* @return string[] An array containing all the supported post types.
|
|
*/
|
|
public function get_supported_post_types() {
|
|
return \get_post_types( array( 'public' => true ), 'names' );
|
|
}
|
|
|
|
/**
|
|
* Get the name of the plugin that registered the transformer.
|
|
*
|
|
* @see Forked from the WordPress elementor plugin.
|
|
*
|
|
* @since version_number_transformer_management_placeholder
|
|
* @return string Plugin name
|
|
*/
|
|
private function get_plugin_name_from_transformer_instance( $transformer ) {
|
|
$class_reflection = new \ReflectionClass( $transformer );
|
|
|
|
$plugin_basename = plugin_basename( $class_reflection->getFileName() );
|
|
|
|
$plugin_directory = strtok( $plugin_basename, '/' );
|
|
|
|
$plugins_data = get_plugins( '/' . $plugin_directory );
|
|
$plugin_data = array_shift( $plugins_data );
|
|
|
|
return $plugin_data['Name'] ?? esc_html__( 'Unknown', 'activitypub' );
|
|
}
|
|
|
|
/**
|
|
* Return whether the transformer supports a post type.
|
|
*
|
|
* @since version_number_transformer_management_placeholder
|
|
*
|
|
* @return string post_type Post type name.
|
|
*/
|
|
final public function supports_post_type( $post_type ) {
|
|
return in_array( $post_type, $this->get_supported_post_types() );
|
|
}
|
|
|
|
/**
|
|
* Get the name used for registering the transformer with the ActivityPub plugin.
|
|
*
|
|
* @since version_number_transformer_management_placeholder
|
|
*
|
|
* @return string name
|
|
*/
|
|
abstract public function get_name();
|
|
|
|
/**
|
|
* Get the display name for the ActivityPub transformer.
|
|
*
|
|
* @since version_number_transformer_management_placeholder
|
|
*
|
|
* @return string display name
|
|
*/
|
|
abstract public function get_label();
|
|
|
|
} |