wordpress-activitypub/includes/class-transformer-base.php

102 lines
2.6 KiB
PHP
Raw Normal View History

2023-11-18 13:40:27 +01:00
<?php
/**
* Inspired by the PHP ActivityPub Library by @Landrok
*
* @link https://github.com/landrok/activitypub
*/
2023-11-18 15:00:28 +01:00
namespace Activitypub;
2023-11-20 14:12:47 +01:00
use WP_Post;
2023-11-18 13:40:27 +01:00
/**
* Base class to implement WordPress to ActivityPub transformers.
*/
abstract class Transformer_Base {
2023-11-20 14:12:47 +01:00
/**
* 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.
}
}
2023-11-18 13:40:27 +01:00
/**
* Get the supported WP post_types that the transformer can use as an input.
*
* By default all post types are supported.
2023-11-20 14:14:25 +01:00
* You may very likely wish to override this function.
2023-11-18 13:40:27 +01:00
*
* @since version_number_transformer_management_placeholder
2023-11-18 13:42:56 +01:00
* @return string[] An array containing all the supported post types.
2023-11-18 13:40:27 +01:00
*/
public function get_supported_post_types() {
2023-11-19 17:32:41 +01:00
return \get_post_types( array( 'public' => true ), 'names' );
2023-11-18 13:40:27 +01:00
}
2023-11-19 17:32:41 +01:00
/**
* 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() );
}
2023-11-19 17:32:41 +01:00
2023-11-18 13:40:27 +01:00
/**
* Get the name used for registering the transformer with the ActivityPub plugin.
*
* @since version_number_transformer_management_placeholder
2023-11-18 13:42:56 +01:00
*
* @return string name
2023-11-18 13:40:27 +01:00
*/
abstract public function get_name();
/**
* Get the display name for the ActivityPub transformer.
*
* @since version_number_transformer_management_placeholder
2023-11-18 13:42:56 +01:00
*
* @return string display name
2023-11-18 13:40:27 +01:00
*/
abstract public function get_label();
2023-11-20 14:14:25 +01:00
2023-11-18 13:40:27 +01:00
}