Checks if item (WP_Post) is "public", a supported post type and not password protected.

This commit is contained in:
Matthias Pfefferle 2023-07-17 15:25:30 +02:00
parent 0f54ea465e
commit d7e9d54063
2 changed files with 59 additions and 23 deletions

View file

@ -1,11 +1,18 @@
<?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
*/ */
public static function init() { public static function init() {
// do not load on admin pages
if ( is_admin() ) {
return;
}
foreach ( get_class_methods( self::class ) as $shortcode ) { foreach ( get_class_methods( self::class ) as $shortcode ) {
if ( 'init' !== $shortcode ) { if ( 'init' !== $shortcode ) {
add_shortcode( 'ap_' . $shortcode, array( self::class, $shortcode ) ); add_shortcode( 'ap_' . $shortcode, array( self::class, $shortcode ) );
@ -23,13 +30,13 @@ 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 ) {
$post_id = get_the_ID(); $post = get_item();
if ( ! $post_id ) { if ( ! $post ) {
return ''; return '';
} }
$tags = \get_the_tags( $post_id ); $tags = \get_the_tags( $post->ID );
if ( ! $tags ) { if ( ! $tags ) {
return ''; return '';
@ -58,13 +65,13 @@ 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 ) {
$post_id = get_the_ID(); $post = get_item();
if ( ! $post_id ) { if ( ! $post ) {
return ''; return '';
} }
return \wp_strip_all_tags( \get_the_title( $post_id ), true ); return \wp_strip_all_tags( \get_the_title( $post->ID ), true );
} }
@ -78,9 +85,9 @@ 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 ) {
$post = get_post(); $post = get_item();
if ( ! $post || \post_password_required( $post ) ) { if ( ! $post ) {
return ''; return '';
} }
@ -184,9 +191,9 @@ class Shortcodes {
// prevent inception // prevent inception
remove_shortcode( 'ap_content' ); remove_shortcode( 'ap_content' );
$post = get_post(); $post = get_item();
if ( ! $post || \post_password_required( $post ) ) { if ( ! $post ) {
return ''; return '';
} }
@ -226,7 +233,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 ) {
$post = get_post(); $post = get_item();
if ( ! $post ) { if ( ! $post ) {
return ''; return '';
@ -260,7 +267,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 ) {
$post = get_post(); $post = get_item();
if ( ! $post ) { if ( ! $post ) {
return ''; return '';
@ -294,9 +301,9 @@ class Shortcodes {
* @return string * @return string
*/ */
public static function image( $atts, $content, $tag ) { public static function image( $atts, $content, $tag ) {
$post_id = get_the_ID(); $post = get_item();
if ( ! $post_id ) { if ( ! $post ) {
return ''; return '';
} }
@ -318,7 +325,7 @@ class Shortcodes {
$size = $atts['type']; $size = $atts['type'];
} }
$image = \get_the_post_thumbnail_url( $post_id, $size ); $image = \get_the_post_thumbnail_url( $post->ID, $size );
if ( ! $image ) { if ( ! $image ) {
return ''; return '';
@ -337,13 +344,13 @@ 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 ) {
$post_id = get_the_ID(); $post = get_item();
if ( ! $post_id ) { if ( ! $post ) {
return ''; return '';
} }
$categories = \get_the_category( $post_id ); $categories = \get_the_category( $post->ID );
if ( ! $categories ) { if ( ! $categories ) {
return ''; return '';
@ -372,7 +379,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 ) {
$post = get_post(); $post = get_item();
if ( ! $post ) { if ( ! $post ) {
return ''; return '';
@ -397,7 +404,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 ) {
$post = get_post(); $post = get_item();
if ( ! $post ) { if ( ! $post ) {
return ''; return '';
@ -461,7 +468,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 ) {
$post = get_post(); $post = get_item();
if ( ! $post ) { if ( ! $post ) {
return ''; return '';
@ -490,7 +497,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 ) {
$post = get_post(); $post = get_item();
if ( ! $post ) { if ( ! $post ) {
return ''; return '';
@ -519,7 +526,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 ) {
$post = get_post(); $post = get_item();
if ( ! $post ) { if ( ! $post ) {
return ''; return '';

View file

@ -352,3 +352,32 @@ if ( ! function_exists( 'get_self_link' ) ) {
} }
} }
/**
* 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 ( ! \in_array( $post->post_type, \get_post_types_by_support( 'activitypub' ), true ) ) {
return null;
}
if ( 'publish' !== $post->post_status ) {
return null;
}
if ( \post_password_required( $post ) ) {
return null;
}
return $post;
}