Merge pull request #248 from mexon/configure-number-of-images

configuration item for number of images to attach
This commit is contained in:
Matthias Pfefferle 2023-01-13 08:58:45 +01:00 committed by GitHub
commit c7044f7ede
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 51 additions and 17 deletions

View file

@ -19,6 +19,7 @@ namespace Activitypub;
* Initialize plugin * Initialize plugin
*/ */
function init() { function init() {
\defined( 'ACTIVITYPUB_MAX_IMAGE_ATTACHMENTS' ) || \define( 'ACTIVITYPUB_MAX_IMAGE_ATTACHMENTS', 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_max_image_attachments',
array(
'type' => 'integer',
'description' => \__( 'Number of images to attach to posts.', 'activitypub' ),
'default' => ACTIVITYPUB_MAX_IMAGE_ATTACHMENTS,
)
);
\register_setting( \register_setting(
'activitypub', 'activitypub',
'activitypub_object_type', 'activitypub_object_type',

View file

@ -80,38 +80,42 @@ 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_image_attachments', \get_option( 'activitypub_max_image_attachments', ACTIVITYPUB_MAX_IMAGE_ATTACHMENTS ) ) );
$images = array(); $images = array();
// max images can't be negative or zero // max images can't be negative or zero
if ( $max_images <= 0 ) { if ( $max_images <= 0 ) {
$max_images = 1; return $images;
} }
$id = $this->post->ID; $id = $this->post->ID;
$image_ids = array(); $image_ids = array();
// list post thumbnail first if this post has one // list post thumbnail first if this post has one
if ( \function_exists( 'has_post_thumbnail' ) && \has_post_thumbnail( $id ) ) { if ( \function_exists( 'has_post_thumbnail' ) && \has_post_thumbnail( $id ) ) {
$image_ids[] = \get_post_thumbnail_id( $id ); $image_ids[] = \get_post_thumbnail_id( $id );
$max_images--; $max_images--;
} }
// then list any image attachments
$query = new \WP_Query( if ( $max_images > 0 ) {
array( // then list any image attachments
'post_parent' => $id, $query = new \WP_Query(
'post_status' => 'inherit', array(
'post_type' => 'attachment', 'post_parent' => $id,
'post_mime_type' => 'image', 'post_status' => 'inherit',
'order' => 'ASC', 'post_type' => 'attachment',
'orderby' => 'menu_order ID', 'post_mime_type' => 'image',
'posts_per_page' => $max_images, '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; 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> <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>
<input value="<?php echo esc_attr( \get_option( 'activitypub_max_image_attachments', ACTIVITYPUB_MAX_IMAGE_ATTACHMENTS ) ); ?>" name="activitypub_max_image_attachments" id="activitypub_max_image_attachments" type="number" min="0" />
<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_MAX_IMAGE_ATTACHMENTS )
),
'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' ); ?>