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
*/
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_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>" );

View file

@ -98,6 +98,15 @@ class Admin {
'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(
'activitypub',
'activitypub_object_type',

View file

@ -76,7 +76,7 @@ class Post {
}
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();
@ -94,20 +94,22 @@ class Post {
$max_images--;
}
// then list any image attachments
$query = new \WP_Query(
array(
'post_parent' => $id,
'post_status' => 'inherit',
'post_type' => 'attachment',
'post_mime_type' => 'image',
'order' => 'ASC',
'orderby' => 'menu_order ID',
'posts_per_page' => $max_images,
)
);
foreach ( $query->get_posts() as $attachment ) {
if ( ! \in_array( $attachment->ID, $image_ids, true ) ) {
$image_ids[] = $attachment->ID;
if ( $max_images > 0 ) {
$query = new \WP_Query(
array(
'post_parent' => $id,
'post_status' => 'inherit',
'post_type' => 'attachment',
'post_mime_type' => 'image',
'order' => 'ASC',
'orderby' => 'menu_order ID',
'posts_per_page' => $max_images,
)
);
foreach ( $query->get_posts() as $attachment ) {
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>
</td>
</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>
<th scope="row">
<?php \esc_html_e( 'Activity-Object-Type', 'activitypub' ); ?>