Skip to content

Commit

Permalink
Merge pull request #90 from Automattic/fix/73-postnotesurl
Browse files Browse the repository at this point in the history
Fixes for #73, also introducing a REST API namespace for the plugin a…
  • Loading branch information
tommusrhodus authored Dec 20, 2024
2 parents ffce29b + db53d45 commit eadfc57
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 53 deletions.
8 changes: 4 additions & 4 deletions includes/block-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -698,15 +698,15 @@ function ttgarden_block_post_notes( $atts, $content = '' ): string {
add_shortcode( 'block_postnotes', 'ttgarden_block_post_notes' );

/**
* Rendered if the post has at least one comment.
* Always rendered, but only outputs content if the post has comments or comments open.
*
* @param array $atts The attributes of the shortcode.
* @param string $content The content of the shortcode.
*
* @return string
*/
function ttgarden_block_notecount( $atts, $content = '' ): string {
return ( get_comments_number() > 0 ) ? ttgarden_do_shortcode( $content ) : '';
return ttgarden_do_shortcode( $content );
}
add_shortcode( 'block_notecount', 'ttgarden_block_notecount' );

Expand Down Expand Up @@ -1686,7 +1686,7 @@ function ttgarden_block_daypage( $atts, $content = '' ): string {
* @return string
*/
function ttgarden_block_previouspage( $atts, $content = '' ): string {
return ( get_next_posts_link() ) ? ttgarden_do_shortcode( $content ) : '';
return ( get_previous_posts_link() ) ? ttgarden_do_shortcode( $content ) : '';
}
add_shortcode( 'block_previouspage', 'ttgarden_block_previouspage' );

Expand All @@ -1699,7 +1699,7 @@ function ttgarden_block_previouspage( $atts, $content = '' ): string {
* @return string
*/
function ttgarden_block_nextpage( $atts, $content = '' ): string {
return ( get_previous_posts_link() ) ? ttgarden_do_shortcode( $content ) : '';
return ( get_next_posts_link() ) ? ttgarden_do_shortcode( $content ) : '';
}
add_shortcode( 'block_nextpage', 'ttgarden_block_nextpage' );

Expand Down
12 changes: 8 additions & 4 deletions includes/tag-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ function ttgarden_tag_nextpost(): string {
* @see https://www.tumblr.com/docs/en/custom_themes#basic_variables
*/
function ttgarden_tag_previouspage(): string|null {
return untrailingslashit( esc_url( get_next_posts_page_link() ) );
return untrailingslashit( esc_url( get_previous_posts_page_link() ) );
}
add_shortcode( 'tag_previouspage', 'ttgarden_tag_previouspage' );

Expand All @@ -701,7 +701,7 @@ function ttgarden_tag_previouspage(): string|null {
* @see https://www.tumblr.com/docs/en/custom_themes#basic_variables
*/
function ttgarden_tag_nextpage(): string|null {
return untrailingslashit( esc_url( get_previous_posts_page_link() ) );
return untrailingslashit( esc_url( get_next_posts_page_link() ) );
}
add_shortcode( 'tag_nextpage', 'ttgarden_tag_nextpage' );

Expand Down Expand Up @@ -874,6 +874,7 @@ function ttgarden_tag_notecountwithlabel(): string {
return get_comments_number_text();
}
add_shortcode( 'tag_notecountwithlabel', 'ttgarden_tag_notecountwithlabel' );
add_shortcode( 'tag_formattednotecount', 'ttgarden_tag_notecountwithlabel' );

/**
* The post comments.
Expand Down Expand Up @@ -1928,11 +1929,14 @@ public function __construct( $id ) {
add_shortcode( 'tag_likebutton', 'ttgarden_tag_likebutton' );

/**
* Returns a URL to the post comments.
* Returns a URL to the post comments HTML partial.
*
* @return string
*/
function ttgarden_tag_postnotesurl(): string {
return get_comments_link();
return sprintf(
'/?p=%d&ttgarden_html_comments=true',
get_the_ID()
);
}
add_shortcode( 'tag_postnotesurl', 'ttgarden_tag_postnotesurl' );
1 change: 1 addition & 0 deletions includes/tumblr-theme-language/tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@
'PostNotes-64',
'NoteCount',
'NoteCountWithLabel',
'FormattedNoteCount',
'Tag',
'URLSafeTag',
'TagURL',
Expand Down
17 changes: 8 additions & 9 deletions src/Customizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -468,16 +468,15 @@ public function theme_specific_options( $wp_customize ): void {
$wp_customize,
$name,
array(
'label' => $label,
'section' => 'colors',
'settings' => $name,
'label' => $label,
'section' => 'colors',
)
)
);

// If it doesn't exist, load the default value into the theme mod.
if ( ! get_theme_mod( $name ) ) {
set_theme_mod( $name, $color );
set_theme_mod( $name, sanitize_hex_color( $color ) );
}

continue;
Expand Down Expand Up @@ -514,7 +513,7 @@ public function theme_specific_options( $wp_customize ): void {

// If it doesn't exist, load the default value into the theme mod.
if ( ! get_theme_mod( $name ) ) {
set_theme_mod( $name, $font );
set_theme_mod( $name, sanitize_text_field( $font ) );
}

continue;
Expand Down Expand Up @@ -551,7 +550,7 @@ public function theme_specific_options( $wp_customize ): void {

// If it doesn't exist, load the default value into the theme mod.
if ( null === get_theme_mod( $name, null ) ) {
set_theme_mod( $name, '1' === $condition ? '1' : '' );
set_theme_mod( $name, sanitize_text_field( '1' === $condition ? '1' : '' ) );
}

continue;
Expand Down Expand Up @@ -588,7 +587,7 @@ public function theme_specific_options( $wp_customize ): void {

// If it doesn't exist, load the default value into the theme mod.
if ( ! get_theme_mod( $name ) ) {
set_theme_mod( $name, $text );
set_theme_mod( $name, sanitize_text_field( $text ) );
}

continue;
Expand Down Expand Up @@ -628,7 +627,7 @@ public function theme_specific_options( $wp_customize ): void {

// If it doesn't exist, load the default value into the theme mod.
if ( ! get_theme_mod( $name ) ) {
set_theme_mod( $name, $image );
set_theme_mod( $name, esc_url_raw( $image ) );
}

continue;
Expand Down Expand Up @@ -676,7 +675,7 @@ public function theme_specific_options( $wp_customize ): void {

// If it doesn't exist, load the default value into the theme mod.
if ( ! get_theme_mod( $name ) ) {
set_theme_mod( $name, $default );
set_theme_mod( $name, sanitize_text_field( $default ) );
}
}
}
Expand Down
14 changes: 9 additions & 5 deletions src/ThemeGarden.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ public function get_theme( string $theme_id ) {
*/
public function register_rest_routes(): void {
register_rest_route(
'tumblr-theme-garden/v1',
TTGARDEN_REST_NAMESPACE,
'/themes',
array(
'methods' => 'GET',
Expand All @@ -210,7 +210,7 @@ public function register_rest_routes(): void {
);

register_rest_route(
'tumblr-theme-garden/v1',
TTGARDEN_REST_NAMESPACE,
'/theme',
array(
'methods' => 'GET',
Expand Down Expand Up @@ -331,14 +331,18 @@ function ( $theme ) {
* @return void
*/
public function option_defaults_helper( $default_params ): void {
$ttgarden_mods = get_option( 'theme_mods_ttgarden', array() );
$ttgarden_mods = get_option( 'theme_mods_tumblr-theme-garden', array() );

if ( ! is_array( $ttgarden_mods ) ) {
$ttgarden_mods = array();
}

foreach ( $default_params as $key => $value ) {
$normal = ttgarden_normalize_option_name( $key );
$ttgarden_mods[ $normal ] = $value;
$ttgarden_mods[ $normal ] = ( str_starts_with( $key, 'color:' ) ) ? sanitize_hex_color( $value ) : sanitize_text_field( $value );
}

update_option( 'theme_mods_ttgarden', $ttgarden_mods );
update_option( 'theme_mods_tumblr-theme-garden', $ttgarden_mods );
}

/**
Expand Down
53 changes: 27 additions & 26 deletions theme/tumblr-theme-garden/comments.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,37 +18,38 @@
}
?>

<?php if ( have_comments() ) : ?>

<ol class="notes">
<?php
wp_list_comments(
array(
'style' => 'ol',
'short_ping' => true,
'avatar_size' => 24,
'callback' => 'ttgarden_comment_markup',
'max_depth' => 0,
)
);
?>
</ol><!-- .comment-list -->

<?php if ( get_comment_pages_count() > 1 && get_option( 'page_comments' ) ) : ?>
<nav class="navigation comment-navigation" role="navigation">
<div class="nav-previous"><?php previous_comments_link( __( '&larr; Older Comments', 'tumblr-theme-garden' ) ); ?></div>
<div class="nav-next"><?php next_comments_link( __( 'Newer Comments &rarr;', 'tumblr-theme-garden' ) ); ?></div>
</nav><!-- .comment-navigation -->
<?php endif; // Check for comment navigation ?>

<?php if ( ! comments_open() && get_comments_number() ) : ?>
<p class="no-comments"><?php esc_html_e( 'Comments are closed.', 'tumblr-theme-garden' ); ?></p>
<?php endif; ?>
<ol class="notes">
<?php
wp_list_comments(
array(
'style' => 'ol',
'short_ping' => true,
'avatar_size' => 24,
'callback' => 'ttgarden_comment_markup',
'max_depth' => 0,
)
);
?>
</ol><!-- .comment-list -->

<?php if ( get_comment_pages_count() > 1 && get_option( 'page_comments' ) ) : ?>
<nav class="navigation comment-navigation" role="navigation">
<div class="nav-previous"><?php previous_comments_link( __( '&larr; Older Comments', 'tumblr-theme-garden' ) ); ?></div>
<div class="nav-next"><?php next_comments_link( __( 'Newer Comments &rarr;', 'tumblr-theme-garden' ) ); ?></div>
</nav><!-- .comment-navigation -->
<?php endif; // Check for comment navigation ?>

<?php if ( ! comments_open() && get_comments_number() ) : ?>
<p class="no-comments"><?php esc_html_e( 'Comments are closed.', 'tumblr-theme-garden' ); ?></p>
<?php endif; ?>

<?php if ( comments_open() ) : ?>

<ol class="notes">
<li>
<?php comment_form(); ?>
</li>
</ol><!-- .comment-list -->

<?php
endif;
50 changes: 45 additions & 5 deletions theme/tumblr-theme-garden/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ function ttgarden_enqueue_scripts(): void {
*
* @return void
*/
function ttgarden_random_endpoint_rewrite(): void {
function ttgarden_rewrite_rules(): void {
// Handle the Tumblr random endpoint.
add_rewrite_rule(
'^random/?$',
Expand All @@ -111,7 +111,7 @@ function ttgarden_random_endpoint_rewrite(): void {
'top'
);
}
add_action( 'init', 'ttgarden_random_endpoint_rewrite' );
add_action( 'init', 'ttgarden_rewrite_rules' );

/**
* Add a new query variable for Tumblr search.
Expand All @@ -123,16 +123,56 @@ function ttgarden_random_endpoint_rewrite(): void {
function ttgarden_add_tumblr_search_var( $vars ): array {
$vars[] = 'q';
$vars[] = 'random';
$vars[] = 'ttgarden_html_comments';
return $vars;
}
add_filter( 'query_vars', 'ttgarden_add_tumblr_search_var' );

/**
* Redirect Tumblr search to core search.
* Handles template redirects for Tumblr theme.
*
* - If 'random' is set, redirect to a random post.
* - If 'q' is set, redirect to the core search page.
* - If 'ttgarden_html_comments' is set, redirect to a HTML only comments page.
*
* @return void
*/
function ttgarden_redirect_tumblr_search(): void {
function ttgarden_template_redirects(): void {
// If 'ttgarden_html_comments' is set, redirect to a HTML only comments page. /?p=85&ttgarden_html_comments=true
if ( get_query_var( 'ttgarden_html_comments' ) ) {
$post_id = get_query_var( 'p' );

// Ensure this is a valid post, it's published, not private.
if ( ! 'post' === get_post_type( $post_id ) || 'publish' !== get_post_status( $post_id ) ) {
exit;
}

// Get the comments.
$comments = get_comments(
array(
'post_id' => $post_id,
'status' => 'approve',
)
);

// Build the HTML output.
$html_output = sprintf(
'<ol class="notes">%s</ol>',
wp_list_comments(
array(
'style' => 'ol',
'callback' => 'ttgarden_comment_markup',
'echo' => false,
'per_page' => 100,
),
$comments
)
);

echo wp_kses_post( $html_output );
exit;
}

// If random is set, redirect to a random post.
if ( get_query_var( 'random' ) ) {
// @see https://docs.wpvip.com/databases/optimize-queries/using-post__not_in/
Expand All @@ -158,7 +198,7 @@ function ttgarden_redirect_tumblr_search(): void {
exit;
}
}
add_action( 'template_redirect', 'ttgarden_redirect_tumblr_search' );
add_action( 'template_redirect', 'ttgarden_template_redirects', 1 );

/**
* Custom comment markup.
Expand Down
3 changes: 3 additions & 0 deletions tumblr-theme-garden.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ function_exists( 'get_plugin_data' ) || require_once ABSPATH . 'wp-admin/include
define( 'TTGARDEN_PATH', plugin_dir_path( __FILE__ ) );
define( 'TTGARDEN_URL', plugin_dir_url( __FILE__ ) );

// Define REST API namespace.
define( 'TTGARDEN_REST_NAMESPACE', 'tumblr-theme-garden/v1' );

// Define tag and block names from Tumblr Theme language.
define( 'TTGARDEN_TAGS', require_once TTGARDEN_PATH . 'includes/tumblr-theme-language/tags.php' );
define( 'TTGARDEN_BLOCKS', require_once TTGARDEN_PATH . 'includes/tumblr-theme-language/blocks.php' );
Expand Down

0 comments on commit eadfc57

Please sign in to comment.