Skip to content

Commit

Permalink
Set custom page type in post sidebar was removed by mistake
Browse files Browse the repository at this point in the history
  • Loading branch information
TorbenLundsgaard committed Oct 6, 2023
1 parent 0b7546d commit 4892b79
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 0 deletions.
2 changes: 2 additions & 0 deletions inc/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use TLA_Media\GTM_Kit\Admin\Analytics;
use TLA_Media\GTM_Kit\Admin\HelpOptionsPage;
use TLA_Media\GTM_Kit\Admin\IntegrationsOptionsPage;
use TLA_Media\GTM_Kit\Admin\MetaBox;
use TLA_Media\GTM_Kit\Admin\SetupWizard;
use TLA_Media\GTM_Kit\Common\RestAPIServer;
use TLA_Media\GTM_Kit\Common\Util;
Expand Down Expand Up @@ -135,6 +136,7 @@ function gtmkit_admin_init(): void {
$util = new Util( $rest_api_server );

Analytics::register( $options, $util );
MetaBox::register( $options );
SetupWizard::register();
GeneralOptionsPage::register();
IntegrationsOptionsPage::register();
Expand Down
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ Yes! Pagespeed is one of our main focus points, and we strive to make the plugin
Enhancements:

Bugfixes:
* Set custom page type in post sidebar was removed by mistake in 1.14

Other:

Expand Down
121 changes: 121 additions & 0 deletions src/Admin/MetaBox.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php
/**
* GTM Kit plugin file.
*
* @package GTM Kit
*/

namespace TLA_Media\GTM_Kit\Admin;

use TLA_Media\GTM_Kit\Options;

/**
* MetaBox
*/
final class MetaBox {

/**
* Plugin options.
*
* @var Options
*/
protected $options;

/**
* Constructor.
*
* @param Options $options The Options instance.
*/
public function __construct( Options $options ) {
$this->options = $options;
}

/**
* Register meta box
*
* @param Options $options The Options instance.
*/
public static function register( Options $options ): void {
$page = new MetaBox( $options );

add_action( 'add_meta_boxes', [ $page, 'add_meta_boxes' ] );
add_action( 'save_post', [ $page, 'save_meta_box_options' ] );
}

/**
* Add "GTM Kit" meta box
*/
public function add_meta_boxes() {
if ( current_user_can( 'manage_options' ) ) {
$post_types = get_post_types(
[
'public' => true,
],
'objects'
);
if ( isset( $post_types['attachment'] ) ) {
unset( $post_types['attachment'] );
}

foreach ( $post_types as $post_type => $post_type_object ) {
$label = $post_type_object->labels->singular_name;
add_meta_box(
'gtmkit_options',
sprintf( __( 'GTM Kit', 'gtm-kit' ), $label ),
[
$this,
'display_meta_boxes',
],
$post_type,
'side',
'core'
);
}
}
}

/**
* Displays some checkbox to de/activate some cache options
*/
public function display_meta_boxes() {
if ( current_user_can( 'manage_options' ) ) {
wp_nonce_field( 'gtmkit_box_option', '_gtmkitnonce', false );
$page_type = get_post_meta( get_the_ID(), 'gtmkit_page_type', true );
?>
<div class="gtmkit_options">

<label for="gtmkit_option_page_type"
style="font-weight: bold;"><?php esc_html_e( 'Set page type in datalayer:', 'gtm-kit' ); ?></label>
<input name="gtmkit_option[page_type]" id="gtmkit_option_page_type" type="text"
title="<?php esc_html_e( 'Page type', 'gtm-kit' ); ?>" value="<?php echo esc_attr( $page_type ); ?>">

<p class="gtmkit-note" style="margin-top: 16px;">
<?php
// translators: %1$s = opening strong tag, %2$s = closing strong tag.
printf( esc_html__( '%1$sNote:%2$s This will only be applied if page type has been activated in the global settings of GTM Kit.', 'gtm-kit' ), '<strong>', '</strong>' );
?>
</p>
</div>
<?php
}
}

/**
* Manage the cache options from the meta box.
*/
public function save_meta_box_options() {
if ( current_user_can( 'manage_options' ) && isset( $_POST['post_ID'], $_POST['_gtmkitnonce'] ) ) {

check_admin_referer( 'gtmkit_box_option', '_gtmkitnonce' );

if ( isset( $_POST['gtmkit_option']['page_type'] ) ) {
if ( empty( $_POST['gtmkit_option']['page_type'] ) ) {
delete_post_meta( (int) $_POST['post_ID'], 'gtmkit_page_type' );
} else {
update_post_meta( (int) $_POST['post_ID'], 'gtmkit_page_type', $_POST['gtmkit_option']['page_type'] );
}
}
}
}
}

0 comments on commit 4892b79

Please sign in to comment.