admin
2 weeks ago
assets
2 weeks ago
core
2 weeks ago
demo-setup
2 weeks ago
helpers
2 weeks ago
icons
2 weeks ago
posts-shortcode
2 weeks ago
recommended-plugins-installer
2 weeks ago
templates
2 weeks ago
admin-settings.php
2 weeks ago
ajax-handler.php
2 weeks ago
blog-helpers.php
2 weeks ago
book-list-renderer.php
2 weeks ago
class-reviews-handler.php
2 weeks ago
context-manager.php
2 weeks ago
download-image-from-url.php
2 weeks ago
header-helpers.php
2 weeks ago
layout-renderers.php
2 weeks ago
meta-boxes.php
2 weeks ago
meta-boxes.php
165 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Meta Boxes for Author Website Templates |
| 4 | * |
| 5 | * Provides per-page/post override controls for header and footer display. |
| 6 | * Allows users to force enable or disable header/footer regardless of global settings. |
| 7 | * |
| 8 | * @package Author_Website_Templates |
| 9 | * @since 1.0.9 |
| 10 | */ |
| 11 | |
| 12 | // Prevent direct access |
| 13 | if ( ! defined( 'ABSPATH' ) ) { |
| 14 | exit; |
| 15 | } |
| 16 | |
| 17 | /** |
| 18 | * Register Meta Box |
| 19 | */ |
| 20 | function rswpthemes_awt_register_meta_boxes() { |
| 21 | add_meta_box( |
| 22 | 'awt_template_options', |
| 23 | __( 'Author Template Options', 'author-website-templates' ), |
| 24 | 'rswpthemes_awt_render_meta_box', |
| 25 | array( 'page', 'post', 'book' ), |
| 26 | 'side', |
| 27 | 'default' |
| 28 | ); |
| 29 | } |
| 30 | add_action( 'add_meta_boxes', 'rswpthemes_awt_register_meta_boxes' ); |
| 31 | |
| 32 | /** |
| 33 | * Render Meta Box Content |
| 34 | */ |
| 35 | function rswpthemes_awt_render_meta_box( $post ) { |
| 36 | // Add nonce for security |
| 37 | wp_nonce_field( 'awt_meta_box_nonce_action', 'awt_meta_box_nonce' ); |
| 38 | |
| 39 | // Get current values |
| 40 | $header_display = get_post_meta( $post->ID, '_awt_header_display', true ); |
| 41 | $footer_display = get_post_meta( $post->ID, '_awt_footer_display', true ); |
| 42 | |
| 43 | // Default to 'default' if empty |
| 44 | if ( empty( $header_display ) ) { |
| 45 | $header_display = 'default'; |
| 46 | } |
| 47 | if ( empty( $footer_display ) ) { |
| 48 | $footer_display = 'default'; |
| 49 | } |
| 50 | |
| 51 | // Display options |
| 52 | $options = array( |
| 53 | 'default' => __( 'Default (Follow Global Settings)', 'author-website-templates' ), |
| 54 | 'enable' => __( 'Enable (Force Show)', 'author-website-templates' ), |
| 55 | 'disable' => __( 'Disable (Force Hide)', 'author-website-templates' ), |
| 56 | ); |
| 57 | ?> |
| 58 | |
| 59 | <style> |
| 60 | .awt-meta-field { |
| 61 | margin-bottom: 15px; |
| 62 | } |
| 63 | .awt-meta-field label { |
| 64 | display: block; |
| 65 | font-weight: 600; |
| 66 | margin-bottom: 5px; |
| 67 | } |
| 68 | .awt-meta-field select { |
| 69 | width: 100%; |
| 70 | } |
| 71 | .awt-meta-field .description { |
| 72 | font-size: 12px; |
| 73 | color: #666; |
| 74 | margin-top: 5px; |
| 75 | } |
| 76 | </style> |
| 77 | |
| 78 | <div class="awt-meta-fields"> |
| 79 | <!-- Header Display Control --> |
| 80 | <div class="awt-meta-field"> |
| 81 | <label for="awt_header_display"> |
| 82 | <?php esc_html_e( 'Header Display', 'author-website-templates' ); ?> |
| 83 | </label> |
| 84 | <select name="awt_header_display" id="awt_header_display"> |
| 85 | <?php foreach ( $options as $value => $label ) : ?> |
| 86 | <option value="<?php echo esc_attr( $value ); ?>" <?php selected( $header_display, $value ); ?>> |
| 87 | <?php echo esc_html( $label ); ?> |
| 88 | </option> |
| 89 | <?php endforeach; ?> |
| 90 | </select> |
| 91 | <p class="description"> |
| 92 | <?php esc_html_e( 'Override global header settings for this page.', 'author-website-templates' ); ?> |
| 93 | </p> |
| 94 | </div> |
| 95 | |
| 96 | <!-- Footer Display Control --> |
| 97 | <div class="awt-meta-field"> |
| 98 | <label for="awt_footer_display"> |
| 99 | <?php esc_html_e( 'Footer Display', 'author-website-templates' ); ?> |
| 100 | </label> |
| 101 | <select name="awt_footer_display" id="awt_footer_display"> |
| 102 | <?php foreach ( $options as $value => $label ) : ?> |
| 103 | <option value="<?php echo esc_attr( $value ); ?>" <?php selected( $footer_display, $value ); ?>> |
| 104 | <?php echo esc_html( $label ); ?> |
| 105 | </option> |
| 106 | <?php endforeach; ?> |
| 107 | </select> |
| 108 | <p class="description"> |
| 109 | <?php esc_html_e( 'Override global footer settings for this page.', 'author-website-templates' ); ?> |
| 110 | </p> |
| 111 | </div> |
| 112 | </div> |
| 113 | |
| 114 | <?php |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Save Meta Box Data |
| 119 | */ |
| 120 | function rswpthemes_awt_save_meta_box( $post_id ) { |
| 121 | // Check nonce |
| 122 | if ( ! isset( $_POST['awt_meta_box_nonce'] ) || |
| 123 | ! wp_verify_nonce( $_POST['awt_meta_box_nonce'], 'awt_meta_box_nonce_action' ) ) { |
| 124 | return; |
| 125 | } |
| 126 | |
| 127 | // Check autosave |
| 128 | if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | // Check permissions |
| 133 | $post_type = get_post_type( $post_id ); |
| 134 | if ( $post_type === 'page' ) { |
| 135 | if ( ! current_user_can( 'edit_page', $post_id ) ) { |
| 136 | return; |
| 137 | } |
| 138 | } else { |
| 139 | if ( ! current_user_can( 'edit_post', $post_id ) ) { |
| 140 | return; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | // Sanitize and save header display |
| 145 | if ( isset( $_POST['awt_header_display'] ) ) { |
| 146 | $header_display = sanitize_text_field( $_POST['awt_header_display'] ); |
| 147 | |
| 148 | // Validate value |
| 149 | if ( in_array( $header_display, array( 'default', 'enable', 'disable' ), true ) ) { |
| 150 | update_post_meta( $post_id, '_awt_header_display', $header_display ); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | // Sanitize and save footer display |
| 155 | if ( isset( $_POST['awt_footer_display'] ) ) { |
| 156 | $footer_display = sanitize_text_field( $_POST['awt_footer_display'] ); |
| 157 | |
| 158 | // Validate value |
| 159 | if ( in_array( $footer_display, array( 'default', 'enable', 'disable' ), true ) ) { |
| 160 | update_post_meta( $post_id, '_awt_footer_display', $footer_display ); |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | add_action( 'save_post', 'rswpthemes_awt_save_meta_box' ); |
| 165 |