2024-08-27 17:58:23 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace WordPressDotOrg\Theme\FiveForTheFuture_2024;
|
|
|
|
|
2024-08-29 00:34:24 +03:00
|
|
|
use function WordPressDotOrg\FiveForTheFuture\PledgeMeta\get_pledge_meta;
|
|
|
|
use const WordPressDotOrg\FiveForTheFuture\Pledge\CPT_ID as PLEDGE_POST_TYPE;
|
|
|
|
|
2024-08-27 17:58:23 +03:00
|
|
|
require_once __DIR__ . '/inc/block-config.php';
|
2024-08-29 00:34:24 +03:00
|
|
|
require_once __DIR__ . '/inc/block-bindings.php';
|
|
|
|
|
|
|
|
// Block files.
|
2024-08-31 00:35:28 +03:00
|
|
|
require_once __DIR__ . '/src/my-pledge-list/index.php';
|
2024-08-29 02:45:27 +03:00
|
|
|
require_once __DIR__ . '/src/pledge-contributors/index.php';
|
2024-08-29 00:34:24 +03:00
|
|
|
require_once __DIR__ . '/src/pledge-edit-button/index.php';
|
2024-08-29 02:45:27 +03:00
|
|
|
require_once __DIR__ . '/src/pledge-teams/index.php';
|
2024-08-27 17:58:23 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Actions and filters.
|
|
|
|
*/
|
|
|
|
add_action( 'wp_enqueue_scripts', __NAMESPACE__ . '\enqueue_assets' );
|
2024-08-29 21:51:58 +03:00
|
|
|
add_filter( 'the_content', __NAMESPACE__ . '\inject_pledge_content', 1 );
|
|
|
|
add_filter( 'get_the_excerpt', __NAMESPACE__ . '\inject_pledge_content', 1 );
|
2024-08-29 21:49:21 +03:00
|
|
|
add_filter( 'search_template_hierarchy', __NAMESPACE__ . '\use_archive_template' );
|
2024-08-30 01:22:37 +03:00
|
|
|
add_filter( 'body_class', __NAMESPACE__ . '\add_body_class' );
|
2024-08-27 17:58:23 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Enqueue scripts and styles.
|
|
|
|
*/
|
|
|
|
function enqueue_assets() {
|
|
|
|
$asset_file = get_theme_file_path( 'build/style/index.asset.php' );
|
|
|
|
if ( ! file_exists( $asset_file ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The parent style is registered as `wporg-parent-2021-style`, and will be loaded unless
|
|
|
|
// explicitly unregistered. We can load any child-theme overrides by declaring the parent
|
|
|
|
// stylesheet as a dependency.
|
|
|
|
|
|
|
|
$metadata = require $asset_file;
|
|
|
|
wp_enqueue_style(
|
|
|
|
'wporg-5ftf-2024',
|
|
|
|
get_theme_file_uri( 'build/style/style-index.css' ),
|
2024-09-02 21:18:51 +03:00
|
|
|
array( 'wporg-parent-2021-style', 'wporg-global-fonts', 'dashicons' ),
|
2024-08-27 17:58:23 +03:00
|
|
|
$metadata['version']
|
|
|
|
);
|
|
|
|
}
|
2024-08-29 00:34:24 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Replace the post content with the pledge description.
|
|
|
|
*
|
|
|
|
* @param string $content Content of the current post.
|
|
|
|
*
|
|
|
|
* @return string Updated content.
|
|
|
|
*/
|
|
|
|
function inject_pledge_content( $content ) {
|
|
|
|
if ( PLEDGE_POST_TYPE !== get_post_type() ) {
|
|
|
|
return $content;
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = get_pledge_meta( get_the_ID() );
|
2024-08-29 21:51:58 +03:00
|
|
|
return $data['org-description'];
|
|
|
|
}
|
2024-08-29 21:49:21 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Switch to the archive.html template on search results.
|
|
|
|
*
|
|
|
|
* @param string[] $templates A list of template candidates, in descending order of priority.
|
|
|
|
*/
|
|
|
|
function use_archive_template( $templates ) {
|
|
|
|
global $wp_query;
|
|
|
|
|
|
|
|
if ( is_search() ) {
|
|
|
|
array_unshift( $templates, 'archive-5ftf_pledge.html' );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $templates;
|
2024-08-29 00:34:24 +03:00
|
|
|
}
|
2024-08-30 01:22:37 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a class to body when the current page is in the menu.
|
|
|
|
*
|
|
|
|
* @param string[] $classes An array of body class names.
|
|
|
|
*
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
function add_body_class( $classes ) {
|
|
|
|
global $wp;
|
|
|
|
// Get the main menu using the hooked function.
|
2024-08-31 00:32:28 +03:00
|
|
|
$menus = Block_Config\add_site_navigation_menus( [] );
|
|
|
|
$slug = $wp->request;
|
2024-08-30 01:22:37 +03:00
|
|
|
$has_page = array_filter(
|
|
|
|
$menus['main'],
|
|
|
|
function ( $item ) use ( $slug ) {
|
|
|
|
return trim( $item['url'], '/' ) === $slug;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
if ( ! empty( $has_page ) ) {
|
|
|
|
$classes[] = 'is-page-in-menu';
|
|
|
|
}
|
|
|
|
return $classes;
|
|
|
|
}
|
2024-08-31 00:35:28 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Filter the featured image to show an avatar on the `my-pledges` page.
|
|
|
|
*
|
|
|
|
* @param string $html The post thumbnail HTML.
|
|
|
|
* @param int $post_id The post ID.
|
|
|
|
* @param int $post_thumbnail_id The post thumbnail ID, or 0 if there isn't one.
|
|
|
|
* @param string|int[] $size Requested image size. Can be any registered image size name, or
|
|
|
|
* an array of width and height values in pixels (in that order).
|
|
|
|
* @param string|array $attr Query string or array of attributes.
|
|
|
|
*
|
|
|
|
* @return string Updated HTML.
|
|
|
|
*/
|
|
|
|
function swap_avatar_for_featured_image( $html, $post_id, $post_thumbnail_id, $size, $attr ) {
|
|
|
|
if ( is_page( 'my-pledges' ) && empty( $html ) ) {
|
|
|
|
$attr_str = '';
|
|
|
|
foreach ( $attr as $name => $value ) {
|
|
|
|
$attr_str .= " $name=" . '"' . $value . '"';
|
|
|
|
}
|
|
|
|
$html = get_avatar( wp_get_current_user(), 110, 'mystery', '', array( 'extra_attr' => $attr_str ) );
|
|
|
|
}
|
|
|
|
return $html;
|
|
|
|
}
|
|
|
|
add_filter( 'post_thumbnail_html', __NAMESPACE__ . '\swap_avatar_for_featured_image', 10, 5 );
|