move function to Shortcode class

This commit is contained in:
Matthias Pfefferle 2023-07-18 08:20:09 +02:00
parent 964ceee869
commit f4c8264e9a
2 changed files with 43 additions and 45 deletions

View file

@ -1,8 +1,6 @@
<?php <?php
namespace Activitypub; namespace Activitypub;
use function Activitypub\get_item;
class Shortcodes { class Shortcodes {
/** /**
* Class constructor, registering WordPress then Shortcodes * Class constructor, registering WordPress then Shortcodes
@ -30,7 +28,7 @@ class Shortcodes {
* @return string The post tags as hashtags. * @return string The post tags as hashtags.
*/ */
public static function hashtags( $atts, $content, $tag ) { public static function hashtags( $atts, $content, $tag ) {
$item = get_item(); $item = self::get_item();
if ( ! $item ) { if ( ! $item ) {
return ''; return '';
@ -65,7 +63,7 @@ class Shortcodes {
* @return string The post title. * @return string The post title.
*/ */
public static function title( $atts, $content, $tag ) { public static function title( $atts, $content, $tag ) {
$item = get_item(); $item = self::get_item();
if ( ! $item ) { if ( ! $item ) {
return ''; return '';
@ -85,7 +83,7 @@ class Shortcodes {
* @return string The post excerpt. * @return string The post excerpt.
*/ */
public static function excerpt( $atts, $content, $tag ) { public static function excerpt( $atts, $content, $tag ) {
$item = get_item(); $item = self::get_item();
if ( ! $item ) { if ( ! $item ) {
return ''; return '';
@ -188,7 +186,7 @@ class Shortcodes {
* @return string The post content. * @return string The post content.
*/ */
public static function content( $atts, $content, $tag ) { public static function content( $atts, $content, $tag ) {
$item = get_item(); $item = self::get_item();
if ( ! $item ) { if ( ! $item ) {
return ''; return '';
@ -233,7 +231,7 @@ class Shortcodes {
* @return string The post permalink. * @return string The post permalink.
*/ */
public static function permalink( $atts, $content, $tag ) { public static function permalink( $atts, $content, $tag ) {
$item = get_item(); $item = self::get_item();
if ( ! $item ) { if ( ! $item ) {
return ''; return '';
@ -267,7 +265,7 @@ class Shortcodes {
* @return string The post shortlink. * @return string The post shortlink.
*/ */
public static function shortlink( $atts, $content, $tag ) { public static function shortlink( $atts, $content, $tag ) {
$item = get_item(); $item = self::get_item();
if ( ! $item ) { if ( ! $item ) {
return ''; return '';
@ -301,7 +299,7 @@ class Shortcodes {
* @return string * @return string
*/ */
public static function image( $atts, $content, $tag ) { public static function image( $atts, $content, $tag ) {
$item = get_item(); $item = self::get_item();
if ( ! $item ) { if ( ! $item ) {
return ''; return '';
@ -344,7 +342,7 @@ class Shortcodes {
* @return string The post categories as hashtags. * @return string The post categories as hashtags.
*/ */
public static function hashcats( $atts, $content, $tag ) { public static function hashcats( $atts, $content, $tag ) {
$item = get_item(); $item = self::get_item();
if ( ! $item ) { if ( ! $item ) {
return ''; return '';
@ -379,7 +377,7 @@ class Shortcodes {
* @return string The author name. * @return string The author name.
*/ */
public static function author( $atts, $content, $tag ) { public static function author( $atts, $content, $tag ) {
$item = get_item(); $item = self::get_item();
if ( ! $item ) { if ( ! $item ) {
return ''; return '';
@ -404,7 +402,7 @@ class Shortcodes {
* @return string The author URL. * @return string The author URL.
*/ */
public static function authorurl( $atts, $content, $tag ) { public static function authorurl( $atts, $content, $tag ) {
$item = get_item(); $item = self::get_item();
if ( ! $item ) { if ( ! $item ) {
return ''; return '';
@ -468,7 +466,7 @@ class Shortcodes {
* @return string The post date. * @return string The post date.
*/ */
public static function date( $atts, $content, $tag ) { public static function date( $atts, $content, $tag ) {
$item = get_item(); $item = self::get_item();
if ( ! $item ) { if ( ! $item ) {
return ''; return '';
@ -497,7 +495,7 @@ class Shortcodes {
* @return string The post time. * @return string The post time.
*/ */
public static function time( $atts, $content, $tag ) { public static function time( $atts, $content, $tag ) {
$item = get_item(); $item = self::get_item();
if ( ! $item ) { if ( ! $item ) {
return ''; return '';
@ -526,7 +524,7 @@ class Shortcodes {
* @return string The post date/time. * @return string The post date/time.
*/ */
public static function datetime( $atts, $content, $tag ) { public static function datetime( $atts, $content, $tag ) {
$item = get_item(); $item = self::get_item();
if ( ! $item ) { if ( ! $item ) {
return ''; return '';
@ -544,4 +542,34 @@ class Shortcodes {
return $date; return $date;
} }
/**
* Get a WordPress item to federate.
*
* Checks if item (WP_Post) is "public", a supported post type
* and not password protected.
*
* @return null|WP_Post The WordPress item.
*/
protected static function get_item() {
$post = \get_post();
if ( ! $post ) {
return null;
}
if ( 'publish' !== \get_post_status( $post ) ) {
return null;
}
if ( \post_password_required( $post ) ) {
return null;
}
if ( ! \in_array( \get_post_type( $post ), \get_post_types_by_support( 'activitypub' ), true ) ) {
return null;
}
return $post;
}
} }

View file

@ -351,33 +351,3 @@ if ( ! function_exists( 'get_self_link' ) ) {
return esc_url( apply_filters( 'self_link', set_url_scheme( 'http://' . $host['host'] . wp_unslash( $_SERVER['REQUEST_URI'] ) ) ) ); return esc_url( apply_filters( 'self_link', set_url_scheme( 'http://' . $host['host'] . wp_unslash( $_SERVER['REQUEST_URI'] ) ) ) );
} }
} }
/**
* Get a WordPress item to federate.
*
* Checks if item (WP_Post) is "public", a supported post type
* and not password protected.
*
* @return null|WP_Post The WordPress item.
*/
function get_item() {
$post = \get_post();
if ( ! $post ) {
return null;
}
if ( 'publish' !== \get_post_status( $post ) ) {
return null;
}
if ( \post_password_required( $post ) ) {
return null;
}
if ( ! \in_array( \get_post_type( $post ), \get_post_types_by_support( 'activitypub' ), true ) ) {
return null;
}
return $post;
}