* @return void */ public function __construct() { // Add a custom button next to the default "Add a media" button in the classic editor add_action( 'media_buttons', array( $this, 'media_buttons' ) ); add_action( 'admin_footer', array( $this, 'admin_footer' ) ); add_action( 'print_media_templates', array( $this, 'print_media_templates' ) ); add_action( 'mce_external_plugins', array( $this, 'mce_external_plugins' ) ); // Registers the sidebar metabox add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_publication_metabox_script' ), 20 ); } /** * Adds a custom button next to the default "Add a media" button in the classic editor * @author Affilizz * @return void */ public function admin_footer() { echo wp_kses( self::render_template_part_static( trailingslashit( AFFILIZZ_DIRECTORY ) . 'templates/administration/partial/footer' ), \Affilizz\Core::get_extended_allowed_tags() ); } /** * Adds a custom button next to the default "Add a media" button in the classic editor * @author Affilizz * @return void */ public function media_buttons() { echo wp_kses( self::render_template_part_static( trailingslashit( AFFILIZZ_DIRECTORY ) . 'templates/administration/editor/classic/button' ), \Affilizz\Core::get_extended_allowed_tags() ); } /** * Hooked on print_media_templates. Rendering the inline shortcode block inside the MCE edition frame. * @author Affilizz * @return void */ public function print_media_templates() { // Exclude Divi // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only request flag used to avoid rendering templates inside Divi builder. if ( ! empty( $_GET['et_fb'] ) && sanitize_text_field( wp_unslash( $_GET['et_fb'] ) ) ) { return; } // Deal with other plugins that may not have the same load cycle if ( ! function_exists( 'get_current_screen' ) ) { require_once ABSPATH . '/wp-admin/includes/screen.php'; } // Exclude other non-classic-editor situations if ( ! isset( \get_current_screen()->id ) || \get_current_screen()->base !== 'post' ) { return; } echo wp_kses( self::render_template_part_static( trailingslashit( AFFILIZZ_DIRECTORY ) . 'templates/administration/editor/classic/block' ), \Affilizz\Core::get_extended_allowed_tags() ); } /** * Registers the sidebar metabox for the classic editor to allow for successive adds to the same publication. * @author Affilizz * @return void */ public function add_meta_boxes() { add_meta_box( 'affilizz-information', __( 'Affilizz', 'affilizz' ), function () { $publication_id = get_post_meta( get_the_ID(), 'affilizz_publication_id', true ); echo wp_kses( self::render_template_part_static( trailingslashit( AFFILIZZ_DIRECTORY ) . 'templates/administration/editor/classic/metabox/publication', array( 'publication_id' => $publication_id ? $publication_id : 0 ) ), \Affilizz\Core::get_extended_allowed_tags() ); }, array( 'post', 'page' ), 'side', 'default' ); } /** * Adds the publication metabox behavior outside of the sanitized template markup. * @author Affilizz * @param string $hook_suffix The current admin page hook suffix. * @return void */ public function enqueue_publication_metabox_script( $hook_suffix = '' ) { if ( ! in_array( $hook_suffix, array( 'post.php', 'post-new.php' ), true ) ) { return; } wp_add_inline_script( 'affilizz-admin', "( function() { document.addEventListener( 'click', function( event ) { var target = event.target; var button; var input; var postId; var requestUrl; if ( ! target || ! target.closest ) { return; } button = target.closest( '#affilizz-empty-publication-id' ); if ( ! button ) { return; } input = document.getElementById( 'affilizz-wordpress-publication-id' ); postId = document.getElementById( 'post_ID' ); if ( ! input || ! postId ) { return; } if ( typeof window.ajaxurl === 'undefined' ) { return; } requestUrl = new URL( window.ajaxurl, window.location.origin ); requestUrl.searchParams.set( 'action', 'affilizz_reset_publication_id' ); requestUrl.searchParams.set( 'post_id', postId.value ); fetch( requestUrl.toString() ) .then( function( response ) { return response.json(); } ) .then( function( data ) { if ( ! data || ! data.deleted ) { return; } if ( ! window.affilizz_l10n || ! window.affilizz_l10n.language || ! window.affilizz_l10n.language.global ) { return; } input.value = window.affilizz_l10n.language.global.not_available; } ); } ); }() );" ); } /** * Registers a custom TinyMCE plugin to display a floating panel. * @author Affilizz * @param array $external_plugins The current list of external plugins. * @return array The updated list of external plugins. */ public function mce_external_plugins( $external_plugins = array() ) { if ( ! is_array( $external_plugins ) ) { $external_plugins = array(); } // Disabled for the 1.15 version // $external_plugins['affilizz-float'] = trailingslashit( esc_url( AFFILIZZ_URL ) ) . 'assets/dist/js/float.js?ver=' . AFFILIZZ_ASSETS_BUILD_VERSION; return $external_plugins; } }