generated from bisco/codex-bootstrap
148 lines
6.4 KiB
PHP
148 lines
6.4 KiB
PHP
<?php
|
|
/**
|
|
* Plugin Name: Azione!Lab structured content
|
|
* Description: Shows and gallery content types used by the Azione!Lab theme.
|
|
* Version: 1.0.0
|
|
*/
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
exit;
|
|
}
|
|
|
|
function azionelab_register_content_types(): void {
|
|
register_post_type(
|
|
'azl_show',
|
|
array(
|
|
'labels' => array(
|
|
'name' => 'Spettacoli',
|
|
'singular_name' => 'Spettacolo',
|
|
'add_new_item' => 'Aggiungi spettacolo',
|
|
'edit_item' => 'Modifica spettacolo',
|
|
),
|
|
'public' => true,
|
|
'show_in_rest' => true,
|
|
'menu_icon' => 'dashicons-tickets-alt',
|
|
'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', 'page-attributes' ),
|
|
'has_archive' => false,
|
|
'rewrite' => false,
|
|
)
|
|
);
|
|
|
|
register_post_type(
|
|
'azl_gallery',
|
|
array(
|
|
'labels' => array(
|
|
'name' => 'Galleria',
|
|
'singular_name' => 'Foto',
|
|
'add_new_item' => 'Aggiungi foto',
|
|
'edit_item' => 'Modifica foto',
|
|
),
|
|
'public' => false,
|
|
'show_ui' => true,
|
|
'show_in_rest' => true,
|
|
'menu_icon' => 'dashicons-format-gallery',
|
|
'supports' => array( 'title', 'thumbnail', 'page-attributes' ),
|
|
)
|
|
);
|
|
}
|
|
add_action( 'init', 'azionelab_register_content_types' );
|
|
|
|
function azionelab_add_meta_boxes(): void {
|
|
add_meta_box( 'azl_show_details', 'Dettagli spettacolo', 'azionelab_show_meta_box', 'azl_show', 'normal' );
|
|
add_meta_box( 'azl_gallery_details', 'Dettagli foto', 'azionelab_gallery_meta_box', 'azl_gallery', 'normal' );
|
|
}
|
|
add_action( 'add_meta_boxes', 'azionelab_add_meta_boxes' );
|
|
|
|
function azionelab_show_meta_box( WP_Post $post ): void {
|
|
wp_nonce_field( 'azionelab_save_meta', 'azionelab_meta_nonce' );
|
|
$year = get_post_meta( $post->ID, 'azl_show_year', true );
|
|
$year = '0' === (string) $year ? '' : $year;
|
|
$location = get_post_meta( $post->ID, 'azl_show_location', true );
|
|
?>
|
|
<p>
|
|
<label for="azl_show_excerpt"><strong>Descrizione breve</strong></label><br>
|
|
<textarea id="azl_show_excerpt" name="azl_show_excerpt" class="widefat" rows="4"><?php echo esc_textarea( $post->post_excerpt ); ?></textarea>
|
|
<span class="description">Questo testo appare nella card dello spettacolo in homepage.</span>
|
|
</p>
|
|
<p><label for="azl_show_year"><strong>Anno</strong></label><br><input id="azl_show_year" name="azl_show_year" type="number" min="1900" max="2100" value="<?php echo esc_attr( $year ); ?>"></p>
|
|
<p><label for="azl_show_location"><strong>Luogo</strong></label><br><input id="azl_show_location" name="azl_show_location" type="text" class="widefat" value="<?php echo esc_attr( $location ); ?>"></p>
|
|
<?php
|
|
}
|
|
|
|
function azionelab_gallery_meta_box( WP_Post $post ): void {
|
|
wp_nonce_field( 'azionelab_save_meta', 'azionelab_meta_nonce' );
|
|
$category = get_post_meta( $post->ID, 'azl_gallery_category', true );
|
|
?>
|
|
<p><label for="azl_gallery_category"><strong>Categoria</strong></label><br>
|
|
<select id="azl_gallery_category" name="azl_gallery_category">
|
|
<?php foreach ( array( 'Lezioni', 'Backstage', 'Spettacoli', 'Gruppo' ) as $option ) : ?>
|
|
<option value="<?php echo esc_attr( $option ); ?>" <?php selected( $category, $option ); ?>><?php echo esc_html( $option ); ?></option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</p>
|
|
<?php
|
|
}
|
|
|
|
function azionelab_save_meta( int $post_id ): void {
|
|
if ( ! isset( $_POST['azionelab_meta_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['azionelab_meta_nonce'] ) ), 'azionelab_save_meta' ) ) {
|
|
return;
|
|
}
|
|
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
|
|
return;
|
|
}
|
|
if ( ! current_user_can( 'edit_post', $post_id ) ) {
|
|
return;
|
|
}
|
|
|
|
$post_type = get_post_type( $post_id );
|
|
if ( 'azl_show' === $post_type ) {
|
|
$excerpt = isset( $_POST['azl_show_excerpt'] ) ? sanitize_textarea_field( wp_unslash( $_POST['azl_show_excerpt'] ) ) : '';
|
|
$year = '';
|
|
if ( isset( $_POST['azl_show_year'] ) && '' !== trim( (string) wp_unslash( $_POST['azl_show_year'] ) ) ) {
|
|
$submitted_year = absint( $_POST['azl_show_year'] );
|
|
$year = $submitted_year >= 1900 && $submitted_year <= 2100 ? (string) $submitted_year : '';
|
|
}
|
|
$location = isset( $_POST['azl_show_location'] ) ? sanitize_text_field( wp_unslash( $_POST['azl_show_location'] ) ) : '';
|
|
update_post_meta( $post_id, 'azl_show_year', $year );
|
|
update_post_meta( $post_id, 'azl_show_location', $location );
|
|
remove_action( 'save_post', 'azionelab_save_meta' );
|
|
wp_update_post(
|
|
array(
|
|
'ID' => $post_id,
|
|
'post_excerpt' => $excerpt,
|
|
)
|
|
);
|
|
add_action( 'save_post', 'azionelab_save_meta' );
|
|
} elseif ( 'azl_gallery' === $post_type ) {
|
|
$allowed = array( 'Lezioni', 'Backstage', 'Spettacoli', 'Gruppo' );
|
|
$category = isset( $_POST['azl_gallery_category'] ) ? sanitize_text_field( wp_unslash( $_POST['azl_gallery_category'] ) ) : '';
|
|
update_post_meta( $post_id, 'azl_gallery_category', in_array( $category, $allowed, true ) ? $category : 'Lezioni' );
|
|
}
|
|
}
|
|
add_action( 'save_post', 'azionelab_save_meta' );
|
|
|
|
add_filter( 'xmlrpc_enabled', '__return_false' );
|
|
add_filter( 'comments_open', '__return_false', 20 );
|
|
add_filter( 'pings_open', '__return_false', 20 );
|
|
remove_action( 'wp_head', 'wp_generator' );
|
|
add_filter( 'the_generator', '__return_empty_string' );
|
|
|
|
function azionelab_restrict_public_user_api( $result, WP_REST_Server $server, WP_REST_Request $request ) {
|
|
unset( $server );
|
|
if ( ! is_user_logged_in() && str_starts_with( $request->get_route(), '/wp/v2/users' ) ) {
|
|
return new WP_Error( 'rest_no_route', 'No route was found matching the URL and request method.', array( 'status' => 404 ) );
|
|
}
|
|
return $result;
|
|
}
|
|
add_filter( 'rest_pre_dispatch', 'azionelab_restrict_public_user_api', 10, 3 );
|
|
|
|
function azionelab_disable_public_author_archives(): void {
|
|
if ( is_author() && ! is_user_logged_in() ) {
|
|
global $wp_query;
|
|
$wp_query->set_404();
|
|
status_header( 404 );
|
|
nocache_headers();
|
|
}
|
|
}
|
|
add_action( 'template_redirect', 'azionelab_disable_public_author_archives' );
|