Thử thách thực đơn quản trị viên WordPress tuyệt vời của tháng 1 năm 2011 (còn gọi là Cách giải quyết một số thách thức khi sửa đổi hệ thống menu quản trị WordPress?)


14

Câu hỏi này là một chút độc đáo.

Đây là một phần của "thử thách" tôi đang phát hành cho nhóm WordPress ( hoặc bất kỳ ai khác ) liên quan đến vé trac: # 16048 , # 16050# 16204 .

Mục đích

Mục tiêu là khắc phục ba (3) vấn đề được minh họa trong ảnh chụp màn hình bên dưới khi cố gắng sửa đổi phần menu Quản trị viên WordPress:

  1. Làm nổi bật trang menu con "microsite" khi chỉnh sửa Luật sư (vì điều này chúng ta cần bằng cách nào đó có thể áp dụng "hiện tại" cho mục menu con, _ và một số móc trong hàm _wp_menuDefput () sẽ cung cấp những gì cần thiết ở đây) ,

  2. Nhận liên kết Trang Menu của luật sư để liên kết đến /wp-admin/edit.php?post_type=attorneykhi chỉnh sửa một Luật sư (và những móc nối cần thiết tương tự trong hàm _wp_menuDefput () có thể xử lý việc này)

  3. Nhận liên kết "microsite" không kích hoạt lỗi "Bạn không có đủ quyền truy cập trang này" * (đây là lỗi khó giải quyết nhất và một cái móc vào giá trị trả vềuser_can_access_admin_page() có thể xử lý vấn đề này một cách độc đáo.)

Ảnh chụp màn hình cho thử thách thực đơn quản trị viên WordPress tuyệt vời của tháng 1 năm 2011
(nguồn: mikechinkel.com )

Không chỉ là trường hợp sử dụng của tôi

Ba (3) vấn đề này dành cho trường hợp sử dụng của tôi nhưng chúng là biểu tượng của các vấn đề liên quan đến việc định cấu hình menu quản trị viên trong WordPress.

Một số người trong nhóm WordPress nói rằng điều đó thật dễ dàng và do đó ngụ ý rằng tôi đang thiếu một cái gì đó (có thể đúng) nhưng tôi đã xem xét vấn đề này trong nhiều tuần và không tìm ra cách khắc phục nó nên tôi đã tạo ra plugin bạn thấy bên dưới ( cũng có thể tải xuống từ Gist ) là ví dụ trường hợp sử dụng đơn giản nhất về các vấn đề. Mã trong admin_menu2()khá hack nhưng đó là những gì cần thiết để sửa đổi Menu Admin trong WordPress.

Lưu ý rằng tôi đã không thử sử dụng các hàm mới remove_menu_page()cũng như các remove_submenu_page()hàm mới trong 3.1 vì sẽ mất nhiều thời gian hơn để tạo plugin - Tôi đã có mã admin_menu2()từ một dự án hiện tại - và tôi không tin rằng họ sẽ giải quyết vấn đề nào

Tôi cần những gì?

Tôi cần một trong hai (2) thứ:

  1. Một giải pháp cho các vấn đề mà tôi gặp phải với plugin này và giải thích trong câu hỏi này và trong ảnh chụp màn hình (BTW, tôi sẽ loại bỏ giải pháp của bạn nếu bạn sử dụng Bộ đệm đầu ra PHP để giải quyết bất kỳ phần nào của vấn đề này) , hoặc

  2. Để Nhóm WordPress nhận ra rằng thực sự cần có những cái móc này và để họ xem xét lại vị trí của họ trên vé.

Làm thế nào để bạn thực hiện thử thách?

  1. Tải xuống và cài đặt một bản sao mới nguyên sơ của WordPress 3.1 (mọi sửa đổi có thể sẽ làm) ,

  2. Tải xuống, cài đặt và kích hoạt plugin "Thử thách thực đơn quản trị viên WordPress tuyệt vời của tháng 1 năm 2011" bên dưới (hoặc tải xuống plugin từ Gist ) , sau đó

  3. Làm theo các hướng dẫn được tìm thấy trên trang plugin cho plugin này (xem ảnh chụp màn hình sau) nhưng về cơ bản tải ảnh chụp màn hình bạn thấy ở trên và sau đó thử tìm ra ba (3) vấn đề được mô tả:

Ảnh chụp màn hình của hướng dẫn plugin "The Great WordPress Admin Menu of Jan 2011"
(nguồn: mikechinkel.com )

Một tia hy vọng

May mắn thay, Andrew Nacin của nhóm WordPress đã đề nghị xem xét điều này một lần tôi đã mã hóa nó vì vậy tôi chủ yếu đăng bài ở đây để anh ấy xem xét và nhận xét cũng như để người khác bình luận. Tôi biết anh ấy đang bận nhưng tôi hy vọng anh ấy (hoặc thậm chí là bạn) có thể dành thời gian để cài đặt plugin này trên bản cài đặt nguyên bản của v3.1 và xem liệu anh ấy có thể giải quyết vấn đề không.

Nếu bạn đồng ý Thách thức là không thể?

Nếu sau khi thử thách này, bạn sẽ đưa ra kết luận giống như tôi và nếu bạn muốn xem các menu Quản trị viên WordPress có thể định cấu hình nhiều hơn, hãy nhận xét về các vé trac này ( # 16048 - # 16050 - # 16204 ) và bỏ phiếu cho câu hỏi này để hiển thị hỗ trợ cho nó.

Tôi sẽ vui vẻ thừa nhận tôi đã bỏ lỡ điều gì đó, nếu tôi đã làm

Tất nhiên có khả năng tôi có thể hoàn toàn chết não về điều này và ai đó có thể chỉ ra chính xác làm thế nào để làm điều đó. Trên thực tế, tôi thực sự hy vọng rằng kết thúc là trường hợp; Tôi thà sai và làm việc này còn hơn ngược lại.

Và đây là Plugin

Bạn cũng có thể tải xuống nếu từ Gist :

<?php
/*
Plugin Name: The Great WordPress Admin Menu Challenge of Jan 2011
Description: <em>"The Great WordPress Admin Menu Challenge of Jan 2011"</em> was inspired by the WordPress team's apparent lack of understanding of the problems addressed by trac tickets <a href="http://core.trac.wordpress.org/ticket/16048">#16048</a> and <a href="http://core.trac.wordpress.org/ticket/16050">#16050</a> <em>(See also: <a href="http://core.trac.wordpress.org/ticket/16204">#16204</a>)</em> and suggestion that the <a href="http://wordpress.org/extend/plugins/admin-menu-editor/>Admin Menu Editor</a> plugin handles the use-cases that the tickets address. Debate spilled over onto Twitter with participation from <a href="http://twitter.com/nacin">@nacin</a>, <a href="http://twitter.com/aaronjorbin">@aaronjorbin</a>, <a href="http://twitter.com/petemall">@petemall</a>, <a href="http://twitter.com/westi">@westi</a>, <a href="http://twitter.com/janeforshort">@janeforshort</a>, <a href="http://twitter.com/PatchesWelcome">@PatchesWelcome</a>; supportive comments from <a href="http://twitter.com/ramsey">@ramsey</a>, <a href="http://twitter.com/brianlayman">@brianlayman</a>, <a href="http://twitter.com/TheLeggett">@TheLeggett</a>, a retweeting of @nacin's simple yet <em>(AFAICT)</em> insufficient solution by <a href="http://twitter.com/vbakaitis">@vbakaitis</a>, <a href="http://twitter.com/Viper007Bond">@Viper007Bond</a>, <a href="http://twitter.com/nickopris">@nickopris</a>, <a href="http://twitter.com/Trademark">@Trademark</a>, <a href="http://twitter.com/favstar_pop">@favstar_pop</a>, <a href="http://twitter.com/designsimply">@designsimply</a>, <a href="http://twitter.com/darylkoop">@darylkoop</a>, <a href="http://twitter.com/iamjohnford">@iamjohnford</a>, <a href="http://twitter.com/markjaquith">@markjaquith</a>, <a href="http://twitter.com/JohnJamesJacoby">@JohnJamesJacoby</a> and <a href="http://twitter.com/dd32">@dd32</a>. Also see <a href="http://andrewnacin.com/2010/12/20/better-admin-menu-controls-custom-post-types-wordpress-3-1/#comment-6360">comments</a> on @nacin's blog post entitled "<em>Better admin menu handling for post types in WordPress 3.1</em>." <strong>The desired goal of the <em>"challenge"</em></strong> is to simply either to find a solution that has eluded me or, to get those who are dismissing it as solvable without added hooks in WordPress to have a tangible example to explore in hopes they will recognize that there is indeed a need for at least some of the requested hooks. <strong>There are three (3) steps to the challenge:</strong> 1.) Get the "Microsite" submenu page to be highlighted when editing an Attorney, 2.) Get the Attorney Menu Page link to link <a href="/wordpress//wp-admin/edit.php?post_type=attorney">here</a>  when editing an Attorney, and 3.) Get the "Microsite" link not to trigger a "You do not have sufficient permissions to access this page" error.  Here is <a href="https://mikeschinkel.com/websnaps/skitched-20110114-235302.png" target="_blank"><strong>a screenshot</strong> that attempts to illustrate the callenge</a>. The code can be found on gist <a href="https://gist.github.com/780709"><strong>here</strong></a>. Activate it as a plugin in a WordPress 3.1 install and go <a href="/wordpress//wp-admin/post.php?post=10&action=edit"><strong>here</strong></a> to see what the screenshot illustrates. <strong>Be sure to load the <a href="https://mikeschinkel.com/websnaps/skitched-20110114-235302.png" target="_blank">screenshot</a> in another browser tab or window first</strong>.
Author:      Mike Schinkel
Author URI:  http://about.me/mikeschinkel
Plugin URI:  https://gist.github.com/780709
*/
if (!class_exists('TheGreatWordPressAdminMenuChallenge')) {
  class TheGreatWordPressAdminMenuChallenge {
    static function on_load() {
      add_action('init',array(__CLASS__,'init'));
      add_action('admin_menu',array(__CLASS__,'admin_menu1'));      // Simulates generic "Microsite" plugin
      add_action('admin_menu',array(__CLASS__,'admin_menu2'),100);  // Simulates website-specific plugin
      add_action('post_row_actions',array(__CLASS__,'post_row_actions'),10,2);
    }
    static function post_row_actions($actions,$post) {
      $url = admin_url(self::this_microsite_url($post->ID));
      $actions = array_merge(array('microsite'=>"<a href=\"{$url}\" title=\"Manage this Microsite\">Microsite</a>"),$actions);
      return $actions;
    }
    static function the_microsite_editor() {
      echo "We are in the Microsite Editor for " . self::post_title();
    }
    static function admin_menu1() {
      if (self::this_post_id() && in_array(self::this_post_type(),array('attorney','practice_area'))) {
        add_submenu_page(
          self::this_parent_slug(),
          self::microsite_page_title(),
          self::microsite_page_title(),
          $capability = 'edit_posts',
          'microsite',
          array($microsite,'the_microsite_editor')
        );
        global $wp_post_types;
        $parent_type_meta = $wp_post_types[self::this_post_type()];
        global $menu;
        $slug = false;
        foreach($menu as $index => $menu_page)
          if ($menu_page[0]===$parent_type_meta->label) {
            $slug = $menu_page[2];
            break;
          }
        if ($slug) {
          global $pagenow;
          global $submenu;
          // Setting this makes gives the link to the microsite in the menu the highlight for "current" menu option
          global $submenu_file;
          $submenu_file = self::this_microsite_url();
          $index = end(array_keys($submenu[$slug]));
          $submenu[$slug][$index][12] = $submenu_file;
        }
      }
    }
    static function this_parent_slug() {
      return "edit.php?post_type=" . self::this_post_type();
    }
    static function post_title() {
      $post_id = self::this_post_id();
      return ($post_id ? get_post($post_id)->post_title : false);
    }
    static function microsite_page_title() {
      return 'Microsite for ' . self::post_title();
    }
    static function this_post_type($get_post=true) {
      $post_type = (isset($_GET['post_type']) ? $_GET['post_type'] : false);
      if (!$post_type && $get_post) {
        $post_id = self::this_post_id();
        $post_type = get_post($post_id)->post_type;
      }
      return $post_type;
    }
    static function this_post_id() {
      $post_id = false;
      $post_type = self::this_post_type(false);
      if (isset($_GET[$post_type]))
        $post_id = intval($_GET[$post_type]);
      else if (isset($_GET['post']))
        $post_id = intval($_GET['post']);
      return $post_id;
    }
    static function this_microsite_url($post_id=false) {
      $post_type = self::this_post_type();
      $post_id = $post_id ? intval($post_id) : self::this_post_id();
      return "edit.php?post_type={$post_type}&page=microsite&attorney={$post_id}";
    }
    static function admin_menu2() {
      // The code required for this is super, nasty, ugly and shouldn't be, but at least it *is* doable
      global $menu;
      global $submenu;
      global $microsite;

      $parent_type = self::this_post_type();
      foreach(array('attorney','practice_area') as $post_type) {
        $slug = "edit.php?post_type={$post_type}";
        if ($post_type==$parent_type) {  // If a microsite remove everything except the microsite editor
          $microsite_url = self::this_microsite_url();
          foreach($submenu[$slug] as $submenu_index => $submenu_page) {
            if ($submenu_page[2]!=$microsite_url) {
              unset($submenu[$slug][$submenu_index]);
            }
          }
        } else {
          $submenu[$slug] = array();
        }
      }

       // Remove the Submenus for each menu
      unset($submenu['index.php']);
      unset($submenu['edit.php?post_type=article']);
      unset($submenu['edit.php?post_type=event']);
      unset($submenu['edit.php?post_type=case_study']);
      unset($submenu['edit.php?post_type=news_item']);
      unset($submenu['edit.php?post_type=transaction']);
      unset($submenu['edit.php?post_type=page']);
      unset($submenu['upload.php']);

      unset($submenu['users.php'][13]); // Removed the "Add New"

      $remove = array_flip(array(
        'edit.php',
        'link-manager.php',
        'edit-comments.php',
        'edit.php?post_type=microsite-page',
      ));
      if (!current_user_can('manage_tools'))
        $remove['tools.php'] = count($remove);

      foreach($menu as $index => $menu_page) {
        if (isset($remove[$menu_page[2]])) {
          unset($submenu[$menu_page[2]]);
          unset($menu[$index]);
        }
      }

      $move = array(
        'edit.php?post_type=page' => array( 'move-to' => 35,  0 => 'Other Pages' ),
        'separator2' => array( 'move-to' => 40 ),
        'upload.php' => array( 'move-to' => 50, 0 => 'Media Library' ),
      );
      $add = array();
      foreach($menu as $index => $menu_page) {
        if (isset($move[$menu_page[2]])) {
          foreach($move[$menu_page[2]] as $value_index => $value) {
            if ($value_index==='move-to') {
              $move_to = $value;
            } else {
              $menu_page[$value_index] = $value;
            }
          }
          $add[$move_to] = $menu_page;
          unset($menu[$index]);
        }
      }
      foreach($add as $index => $value)
        $menu[$index] = $value;

      add_menu_page(
        'Attorney Positions',
        'Attorney Positions',
        'edit_posts',
        'edit-tags.php?taxonomy=attorney-position&amp;post_type=attorney',
        false,
        false,
        55);

      ksort($menu); // Need to sort or it doesn't come out right.
    }
    static function init() {
      register_post_type('attorney',array(
        'label'           => 'Attorneys',
        'public'          => true,
      ));
      register_post_type('practice_area',array(
        'label'           => 'Practice Areas',
        'public'          => true,
      ));
      register_taxonomy('attorney-position','attorney',array(
        'label'=>'Attorney Positions',
      ));
      register_post_type('article',array(
        'label'           => 'Articles & Presentations',
        'public'          => true,
      ));
      register_post_type('case_study',array(
        'label'           => 'Case Studies',
        'public'          => true,
      ));
      register_post_type('news_item',array(
        'label'           => 'Firm News',
        'public'          => true,
      ));
      register_post_type('event',array(
        'label'           => 'Events',
        'public'          => true,
      ));
      register_post_type('transaction',array(
        'label'           => 'Transactions',
        'public'          => true,
      ));

      // Install the test data
      $post_id = 10;
      $attorney = get_post($post_id);
      if (!$attorney) {
        global $wpdb;
        $wpdb->insert($wpdb->posts,array(
          'ID' => $post_id,
          'post_title' => 'John Smith',
          'post_type' => 'attorney',
          'post_content' => 'This is a post about the Attorney John Smith.',
          'post_status' => 'publish',
          'post_author' => 1,
        ));
      }
    }
  }
  TheGreatWordPressAdminMenuChallenge::on_load();
}

Đối với tất cả những ai đọc nó, tôi thực sự hy vọng bạn có thể giúp đỡ.

Cảm ơn trước.


Tôi quan tâm (và thực sự cần nâng cao trải nghiệm của mình với phía quản trị viên), nhưng có lẽ sẽ đợi bản phát hành cuối cùng 3.1. Ngăn xếp thử nghiệm cục bộ của tôi không phù hợp với nhiều phiên bản lõi nên tôi vẫn sử dụng ổn định hiện tại.
Hiếm

Tôi biết chính xác vấn đề mà bạn nói về Mike, tôi không nghĩ rằng tôi có thể mô tả nó tốt hơn bạn có ở đó, nhưng tôi đã thấy những vấn đề tương tự khi viết menu thả xuống cho quản trị viên (cho vui), chỉ cần thêm +1 của tôi.
t31os

@ t 310s - Cảm ơn bạn đã thêm +1 của bạn. Có lẽ tôi đã mất 2 tuần nghiên cứu để có thể mô tả vấn đề, vì vậy thực tế bạn thậm chí có thể nhận ra rằng nó có liên quan (và đã không mất 2 tuần tôi có) có nghĩa là bạn đang đi trước hầu hết mọi người về vấn đề này, bao gồm cả tôi!
MikeSchinkel

Câu trả lời:


2

Mike, tôi đã xem mã và trường hợp sử dụng cuối lý tưởng của bạn ... và một số trong số họ, thật lòng mà nói, không thể với hệ thống hiện tại. Một lần nữa, yêu cầu của bạn:

  1. Làm nổi bật trang menu con "microsite" khi chỉnh sửa Luật sư
  2. Nhận liên kết Trang Menu Luật sư để liên kết đến /wp-admin/edit.php?post_type=attorneykhi chỉnh sửa Luật sư
  3. Nhận liên kết "microsite" không kích hoạt lỗi "Bạn không có đủ quyền truy cập trang này"

Và vấn đề chính ở đây là # 2.

Những gì tôi đã cố gắng

Tôi đã thử thêm một loại bài đăng tùy chỉnh cho Luật sư và ngay lập tức được nhắc nhở rằng /wp-admin/edit.php?post_type=attorneysẽ cung cấp cho bạn một danh sách luật sư, không phải màn hình chỉnh sửa thực tế. Việc chỉnh sửa thực tế diễn ra trên /wp-admin/post.php?post=10&action=edit. Vì vậy, nếu bạn thực sự bị ràng buộc với # 2 ... hai tiêu chí khác sẽ không hoạt động.

Đây là lý do tại sao # 3 thất bại trong việc triển khai ... và tôi thậm chí không thể thử # 1 vì tôi không thể đi xa đến thế.


Tin rằng phân tích của bạn là chính xác. Bố cục được hiển thị trong câu hỏi là một mục tôi đã có nhiều hơn một yêu cầu của khách hàng trong mục tiêu của họ để đơn giản hóa các cấu trúc menu cho các trường hợp sử dụng của họ. Tôi đã đề xuất sử dụng các menu con nhưng họ không thích điều đó; họ cảm thấy nó sẽ quá khó hiểu cho người dùng của họ. Một điều tôi có thể không đề cập là tôi đang phát triển một sản phẩm dựa trên WordPress để họ phân phối so với trang web WordPress cho họ, nơi tôi có thể chỉ cho họ biết cách làm cho mọi thứ hoạt động. Tùy chọn khác của họ là từ bỏ WordPress; không phải những gì tôi muốn họ làm.
MikeSchinkel

2

Này Mike, vấn đề # 3 của bạn là do bạn chỉ định ($microsite, 'the_microsite_editor'), nên ở đâu (__CLASS__, 'the_microsite_editor').

Cập nhật: Sau khi dành quá nhiều thời gian để cố gắng giải quyết một số rắc rối tương tự cho plugin của riêng tôi, đây là điều tôi thấy có thể giúp với Thử thách của bạn (lưu ý rằng các hàm là các phương thức bên dưới lớp của bạn):

function add_posttype_submenu_page($mytype, $label, $cap, $slug) {  
    /* we add two submenu pages to work around the 
       edit.php?post_type=...&page=...problem and have 
       our page called as admin.php?page=... instead */
    //first create a 'blind' pseudo-entry to register our page callback
    add_submenu_page($mytype, $label, $label, $cap, $slug, 
                     array( &$this, 'admin_'.$mytype ));
    //then create a real entry that 'calls' our pseudo-entry
    add_submenu_page('edit.php?post_type='.$mytype, $label, 
                     $label, $cap, 'admin.php?page='.$slug);
    /* then lets fix/hack the highlighting */
    global $plugin_page;
    global $submenu_file;
    if ($plugin_page == $slug) {
        // this next line highlights the submenu entry
        $submenu_file = 'admin.php?page='.$slug; 
        add_filter('parent_file', 
                   array(&$this, 'evil_parent_file_hack'));
    }
} 

function evil_parent_file_hack() {
    //we do this to get the parent menu properly highlighted, too
    //it only gets called on the submenu menu page in question
    global $self;
    global $parent_file;
    $self = $parent_file;
    remove_filter('parent_file', array(&$this, 'evil_parent_file_hack'));
}

Sau đó, bạn chỉ cần gọi add_posttype_submenu_page()với các tham số theo. Điều này sẽ thêm một mục menu con vào menu được tạo tự động trong khi register_post_type()gọi.


Rất tiếc ... các dấu gạch dưới kép xung quanh LỚP đã được chuyển thành định dạng in đậm ;-)
wyrfel

Tôi sửa nó rồi. :)
fuxia

Ồ, đó chỉ là tuyệt vời; cảm ơn! Làm thế nào tôi có thể bỏ lỡ điểm đầu tiên?!? Đừng!
MikeSchinkel

Cảm ơn, Mike. Quay lại chủ đề ban đầu của bạn ... WP nội bộ tạo ID cho một số mục menu và lưu nó trong giá trị 4 của mảng menu. Chẳng hạn như các plugin-page-hook. Tuy nhiên, đối với các loại bưu chính tùy chỉnh, nó lưu trữ một ID không phù hợp với định dạng hook-page-hook. Tôi nghĩ rằng có thể giúp được rất nhiều nếu WP thực hiện điều này một cách nhất quán (nghĩa là tạo các móc trang cho mọi thứ và sử dụng chúng để liên kết các mục menu với nhau, thay vì gắn các menu con bằng 'slug cha mẹ / tập tin'.
wyrfel
Khi sử dụng trang web của chúng tôi, bạn xác nhận rằng bạn đã đọc và hiểu Chính sách cookieChính sách bảo mật của chúng tôi.
Licensed under cc by-sa 3.0 with attribution required.