代码之家  ›  专栏  ›  技术社区  ›  Duck

阻止编辑具有特定ID的页面

  •  0
  • Duck  · 技术社区  · 4 年前

    我创建这个插件是为了防止某些帖子被删除。

    此插件从页面列表中删除TRASH选项。

    register_activation_hook( __FILE__, 'prevent_page_delete_activate' );
    function prevent_page_delete_activate() {
        add_action('admin_menu', 'prevent_page_delete');
    }
    
    add_filter ('user_has_cap', 'prevent_default_theme_deletion', 10, 3);
    
    function prevent_default_theme_deletion($allcaps, $caps, $args) {
      $ids = [2000,2333,4444];
      // trash will ve removed for this page IDs
    
      $numElementos = count($ids);
    
      for ($i=0; $i < $numElementos; $i++) {
    
        $post_id = $ids[$i];
        if ( isset( $args[0] ) && isset( $args[2] ) && $args[2] == $post_id && $args[0] == 'delete_post' ) {
          $allcaps[ $caps[0] ] = false;
        }
    
      }
      return $allcaps;
    
    }
    

    从某些页面ID中删除EDIT链接等效于什么?

    0 回复  |  直到 4 年前
        1
  •  0
  •   amarinediary    4 年前

    根据您上次的评论,有几件事需要考虑:

    • 直观地隐藏任何要编辑/删除的提示。。。等
    • 删除任何默认链接样式。
    • 阻止直接访问。
    • 正在阻止数据库更新。

    您可以通过 pre_post_update 在数据库中更新现有帖子之前立即启动的操作挂钩。。

    我们可以使用 post_row_actions 其过滤帖子列表表上的行动作链接的阵列以防止编辑动作。

    最后我们使用 admin_head-{$hook_suffix} 钩子可以删除任何视觉样式,并防止通过URL直接访问。

    所有内容都封装在一个类中,以使其更容易。

    <?php
    
    if ( ! class_exists( 'wpso70412723' ) ) {
    
        class wpso70412723 {
    
            public $protected_posts_IDs = [ //Define the protected posts IDs
                2000,
                2333,
                4444,
            ];
    
            public function __construct() {
                add_action( 'pre_post_update', array( $this, 'wpso_70412723_prevent_database_update_on_specific_post_edit' ), 10, 2 );
                add_filter( 'post_row_actions', array( $this, 'wpso_70412723_remove_edit_related_actions_from_post_action_row' ), 10, 2 );
                add_action( 'admin_head-edit.php', array( $this, 'wpso_70412723_prevent_link_style_and_click_ability_from_post_title' ));
                add_action( 'admin_head-post.php', array( $this, 'wpso_70412723_prevent_direct_access_to_a_specific_post_through_URL' ));
            } //public function __construct() {
    
            /**
             * Prevent specific posts edits actions.
             * Any post modifications (edit, delete, etc.) will be prevented.
             *
             * @param Integer $post_ID
             * @param Array $data
             */
            public function wpso_70412723_prevent_database_update_on_specific_post_edit($post_ID, $data) {
                if (in_array($post_ID, $this->protected_posts_IDs))
                    wp_die('You are not allowed to edit this post.', 'Something went wrong...', [
                        'back_link' => true
                    ]);
            } //public function wpso_70412723_prevent_database_update_on_specific_post_edit() {
    
            /**
             * Filters-out edit related actions from the array of row action links on the Posts list table.
             * 
             * @param String $actions An array of row action links.
             * @param Object (WP_Post) The post object.
             */
            public function wpso_70412723_remove_edit_related_actions_from_post_action_row($actions, $post) {
                if (in_array($post->ID, $this->protected_posts_IDs)) {
                    unset( $actions['edit'] );
                    unset( $actions['inline hide-if-no-js'] );
                    unset( $actions['trash'] );
                };
                return $actions;
            } //public function wpso_70412723_prevent_database_update_on_specific_post_edit() {
    
            /**
             * Prevent link style and click ability from the post title.
             * Fires in head section for a specific admin page.
             * In our case, the admin posts listing edit page.
             * 
             * @see https://developer.wordpress.org/reference/hooks/admin_head-hook_suffix/
             */
            public function wpso_70412723_prevent_link_style_and_click_ability_from_post_title() {
                if ( 'edit' !== get_current_screen()->base )
                    return;
    
                global $wp_query;
                $posts = $wp_query->posts;
                foreach ($posts as $post) {
                    if (in_array($post->ID, $this->protected_posts_IDs)) {
                        echo '<style type="text/css">
                            #the-list .post-' . $post->ID . ' strong a {
                                pointer-events: none;
                                color: initial;
                                text-decoration: none;
                            }
                        </style>';
                    };
                };
            } //public function wpso_70412723_prevent_link_style_and_click_ability_from_post_title() {
    
            /**
             * Prevent direct access to a specific post through URL.
             * Fires in head section for a specific admin page.
             * In our case, the admin posts listing edit page.
             * 
             * @see https://developer.wordpress.org/reference/hooks/admin_head-hook_suffix/
             */
            public function wpso_70412723_prevent_direct_access_to_a_specific_post_through_URL() {
                if ( 'post' !== get_current_screen()->base )
                    return;
    
                if (in_array(get_the_ID(), $this->protected_posts_IDs)) {
                    wp_die('You are not allowed to edit this post.', 'Something went wrong...', [
                        'back_link' => true
                    ]);
                };
            } //public function wpso_70412723_prevent_direct_access_to_a_specific_post_through_URL() {
    
        }; //class wpso70412723 {
    
        new wpso70412723();
    
    }; //if ( ! class_exists( 'wpso70412723' ) ) {
    

    顺便说一句,删除帖子被视为编辑。

    你不需要你的 prevent_default_theme_deletion() 作用

    enter image description here

    您可能会想使用 edit_post action hook tho这不起作用,因为:

    编辑_发布 :更新现有帖子后激发。

    “一次”语句是我们需要使用的原因 前后期更新 .

    前后期更新 :在数据库中更新现有帖子之前立即激发。