configuration item for number of images to attach

This commit is contained in:
Matthew Exon 2023-01-07 15:35:14 +01:00
parent 7d5b8e7a82
commit d1765b56dd
4 changed files with 47 additions and 15 deletions

View file

@ -19,6 +19,7 @@ namespace Activitypub;
* Initialize plugin * Initialize plugin
*/ */
function init() { function init() {
\defined( 'ACTIVITYPUB_NUMBER_IMAGES' ) || \define( 'ACTIVITYPUB_NUMBER_IMAGES', 3 );
\defined( 'ACTIVITYPUB_HASHTAGS_REGEXP' ) || \define( 'ACTIVITYPUB_HASHTAGS_REGEXP', '(?:(?<=\s)|(?<=<p>)|(?<=<br>)|^)#([A-Za-z0-9_]+)(?:(?=\s|[[:punct:]]|$))' ); \defined( 'ACTIVITYPUB_HASHTAGS_REGEXP' ) || \define( 'ACTIVITYPUB_HASHTAGS_REGEXP', '(?:(?<=\s)|(?<=<p>)|(?<=<br>)|^)#([A-Za-z0-9_]+)(?:(?=\s|[[:punct:]]|$))' );
\defined( 'ACTIVITYPUB_ALLOWED_HTML' ) || \define( 'ACTIVITYPUB_ALLOWED_HTML', '<strong><a><p><ul><ol><li><code><blockquote><pre><img>' ); \defined( 'ACTIVITYPUB_ALLOWED_HTML' ) || \define( 'ACTIVITYPUB_ALLOWED_HTML', '<strong><a><p><ul><ol><li><code><blockquote><pre><img>' );
\defined( 'ACTIVITYPUB_CUSTOM_POST_CONTENT' ) || \define( 'ACTIVITYPUB_CUSTOM_POST_CONTENT', "<p><strong>%title%</strong></p>\n\n%content%\n\n<p>%hashtags%</p>\n\n<p>%shortlink%</p>" ); \defined( 'ACTIVITYPUB_CUSTOM_POST_CONTENT' ) || \define( 'ACTIVITYPUB_CUSTOM_POST_CONTENT', "<p><strong>%title%</strong></p>\n\n%content%\n\n<p>%hashtags%</p>\n\n<p>%shortlink%</p>" );

View file

@ -98,6 +98,15 @@ class Admin {
'default' => ACTIVITYPUB_CUSTOM_POST_CONTENT, 'default' => ACTIVITYPUB_CUSTOM_POST_CONTENT,
) )
); );
\register_setting(
'activitypub',
'activitypub_number_images',
array(
'type' => 'integer',
'description' => \__( 'Number of images to attach to posts.', 'activitypub' ),
'default' => ACTIVITYPUB_NUMBER_IMAGES,
)
);
\register_setting( \register_setting(
'activitypub', 'activitypub',
'activitypub_object_type', 'activitypub_object_type',

View file

@ -76,7 +76,7 @@ class Post {
} }
public function generate_attachments() { public function generate_attachments() {
$max_images = \apply_filters( 'activitypub_max_images', 3 ); $max_images = intval( \apply_filters( 'activitypub_max_images', \get_option( 'activitypub_number_images', ACTIVITYPUB_NUMBER_IMAGES ) ) );
$images = array(); $images = array();
@ -94,20 +94,22 @@ class Post {
$max_images--; $max_images--;
} }
// then list any image attachments // then list any image attachments
$query = new \WP_Query( if ( $max_images > 0 ) {
array( $query = new \WP_Query(
'post_parent' => $id, array(
'post_status' => 'inherit', 'post_parent' => $id,
'post_type' => 'attachment', 'post_status' => 'inherit',
'post_mime_type' => 'image', 'post_type' => 'attachment',
'order' => 'ASC', 'post_mime_type' => 'image',
'orderby' => 'menu_order ID', 'order' => 'ASC',
'posts_per_page' => $max_images, 'orderby' => 'menu_order ID',
) 'posts_per_page' => $max_images,
); )
foreach ( $query->get_posts() as $attachment ) { );
if ( ! \in_array( $attachment->ID, $image_ids, true ) ) { foreach ( $query->get_posts() as $attachment ) {
$image_ids[] = $attachment->ID; if ( ! \in_array( $attachment->ID, $image_ids, true ) ) {
$image_ids[] = $attachment->ID;
}
} }
} }

View file

@ -74,6 +74,26 @@
<p><?php echo \wp_kses( \__( '<a href="https://github.com/pfefferle/wordpress-activitypub/issues/new" target="_blank">Let me know</a> if you miss a template pattern.', 'activitypub' ), 'default' ); ?></p> <p><?php echo \wp_kses( \__( '<a href="https://github.com/pfefferle/wordpress-activitypub/issues/new" target="_blank">Let me know</a> if you miss a template pattern.', 'activitypub' ), 'default' ); ?></p>
</td> </td>
</tr> </tr>
<tr>
<th scope="row">
<?php \esc_html_e( 'Number of images', 'activitypub' ); ?>
</th>
<td>
<textarea name="activitypub_number_images" id="activitypub_number_images" rows="1" cols="50" class="large-text"><?php echo esc_html( \get_option( 'activitypub_number_images', ACTIVITYPUB_NUMBER_IMAGES ) ); ?></textarea>
<p class="description">
<?php
echo \wp_kses(
\sprintf(
// translators:
\__( 'The number of images to attach to posts. Default: <code>%s</code>', 'activitypub' ),
\esc_html( ACTIVITYPUB_NUMBER_IMAGES )
),
'default'
);
?>
</p>
</td>
</tr>
<tr> <tr>
<th scope="row"> <th scope="row">
<?php \esc_html_e( 'Activity-Object-Type', 'activitypub' ); ?> <?php \esc_html_e( 'Activity-Object-Type', 'activitypub' ); ?>