Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Limit the frontend submit "Add Media" button for Authors to "Insert from URL" only.
- */
- if ( ! function_exists( 'my_jnews_fs_author_url_only' ) ) {
- /**
- * Check whether the current user has the Author role.
- *
- * @return bool True when the current user is an Author, otherwise false.
- */
- function my_jnews_fs_author_url_only() {
- if ( ! is_user_logged_in() ) {
- return false;
- }
- $user = wp_get_current_user();
- return $user instanceof WP_User && in_array( 'author', (array) $user->roles, true );
- }
- }
- if ( ! function_exists( 'my_jnews_fs_is_editor_page' ) ) {
- /**
- * Check whether the current page is the JNews frontend submit editor.
- *
- * @return bool True when the current page is the frontend editor, otherwise false.
- */
- function my_jnews_fs_is_editor_page() {
- if ( ! class_exists( 'JNews_Frontend_Endpoint' ) ) {
- return false;
- }
- global $wp;
- $endpoint = JNews_Frontend_Endpoint::getInstance()->get_endpoint();
- return isset( $endpoint['editor']['slug'], $wp->query_vars[ $endpoint['editor']['slug'] ] );
- }
- }
- /**
- * Keep only the "Insert from URL" tab inside the WordPress media popup.
- */
- add_filter(
- 'media_upload_tabs',
- function ( $tabs ) {
- if ( ! my_jnews_fs_author_url_only() ) {
- return $tabs;
- }
- global $pagenow;
- if ( 'media-upload.php' !== $pagenow ) {
- return $tabs;
- }
- $referer = wp_get_referer();
- if ( ! $referer || false === strpos( $referer, '/editor/' ) ) {
- return $tabs;
- }
- return array(
- 'type_url' => __( 'Insert from URL', 'jnews' ),
- );
- }
- );
- /**
- * Force the media popup to load the "Insert from URL" tab for Authors.
- */
- add_action(
- 'admin_init',
- function () {
- if ( ! my_jnews_fs_author_url_only() ) {
- return;
- }
- global $pagenow;
- if ( 'media-upload.php' !== $pagenow ) {
- return;
- }
- $referer = wp_get_referer();
- if ( ! $referer || false === strpos( $referer, '/editor/' ) ) {
- return;
- }
- $_REQUEST['tab'] = 'type_url';
- $_GET['tab'] = 'type_url';
- },
- 1
- );
- /**
- * Load Thickbox assets because the frontend editor uses the classic WordPress media popup.
- */
- add_action(
- 'wp_enqueue_scripts',
- function () {
- if ( ! my_jnews_fs_author_url_only() || ! my_jnews_fs_is_editor_page() ) {
- return;
- }
- add_thickbox();
- },
- 20
- );
- /**
- * Override the "Add Media" button so it opens the URL-only media popup.
- */
- add_action(
- 'wp_footer',
- function () {
- if ( ! my_jnews_fs_author_url_only() || ! my_jnews_fs_is_editor_page() ) {
- return;
- }
- $media_upload_url = add_query_arg(
- array(
- 'post_id' => 0,
- 'TB_iframe' => 'true',
- 'width' => 640,
- 'height' => 517,
- 'tab' => 'type_url',
- ),
- admin_url( 'media-upload.php' )
- );
- ?>
- <script>
- (function($){
- "use strict";
- $(function () {
- var $button = $('#insert-media-button');
- var customUrl = <?php echo wp_json_encode( $media_upload_url ); ?>;
- // Stop if the editor button is not available.
- if (!$button.length) {
- return;
- }
- // Remove the default WordPress media behavior from this button.
- $button.removeAttr('data-editor data-wp-media');
- $button.off('click');
- // Open the URL-only popup instead of the normal media uploader.
- $button.on('click.jnewsUrlOnly', function (event) {
- event.preventDefault();
- if (typeof window.tb_show === 'function') {
- window.tb_show('<?php echo esc_js( __( 'Insert from URL', 'jnews' ) ); ?>', customUrl);
- }
- return false;
- });
- });
- })(jQuery);
- </script>
- <?php
- },
- 99
- );
Advertisement
Add Comment
Please, Sign In to add comment