当前用户的限制核对表

时间:2013-01-14 作者:Christine Cooper

当我解释这件事时,请跟我坦白。我正在为我的多作者平台添加一个助理编辑器功能。在post edit页面中出现的框中,编辑可以勾选他们完成的任务(例如校对文章),然后检查他们的用户名(这样他们就可以在头版帖子中获得信任)。盒子看起来像这样:

enter image description here

框的代码:

// author checkboxes
add_action( \'add_meta_boxes\', \'assisting_editor\' );
function assisting_editor() {
    add_meta_box(
        \'assisting_editor\', // id, used as the html id att
        __( \'Editorial Tasks\' ), // meta box title
        \'editor_tasks\', // callback function, spits out the content
        \'post\', // post type or page. This adds to posts only
        \'side\', // context, where on the screen
        \'low\' // priority, where should this go in the context
    );

}

function editor_tasks( $post ) {
    global $wpdb;
    $value = get_post_meta($post->ID, \'ratings\', true);

    echo \'<div class="misc-pub-section misc-pub-section-last"><span id="timestamp"><label>Editorial tasks: </label>\';

    $ratings = array(
        1 => \' Proofread \',
        2 => \' Graphics Added \',
        3 => \' SEO Fixed \',
        4 => \' Ready for Publish \'
    );

    foreach ($ratings as $id => $text) {
    $checked = (in_array($id,(array)$value)) ? \' checked="checked"\' : \'\';
    echo \'<input type="checkbox" name="ratings[]" value="\' . $id . \'"\'.  $checked . \'/><label for="ratings[]">\'.$text.\'</label>\';
    }

    $qry[\'relation\'] = \'OR\';
    $qry[] = array(
      \'key\' => $wpdb->prefix.\'capabilities\',
      \'value\' => \'editor\',
      \'compare\' => \'like\'
    );
    $qry[] = array(
      \'key\' => $wpdb->prefix.\'capabilities\',
      \'value\' => \'administrator\',
      \'compare\' => \'like\'
    );
    $qry = array(\'fields\' => \'all_with_meta\',\'meta_query\'=>$qry);

    $alleds = get_users($qry);

    $currenteds = get_post_meta($post->ID, \'currenteds\', true);

    foreach ($alleds as $ed) {
    $checked = (in_array($ed->ID,(array)$currenteds)) ? \' checked="checked"\' : \'\';
    echo \'<input type="checkbox" name="currenteds[]" value="\' . $ed->ID . \'"\' .$checked . \'"/><label for="ratings[]">\'.$ed->user_nicename.\'</label>\';
    }
    echo \'</span></div>\';
}

add_action( \'save_post\', \'save_metadata\');

function save_metadata($postid)
{   
    if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ) return false;
    if ( !current_user_can( \'edit_page\', $postid ) ) return false;
    if( empty($postid) ) return false;

    if ( is_null($_REQUEST["ratings"]) ) {
        delete_post_meta($postid, \'ratings\');
    } else {
        update_post_meta($postid, \'ratings\', $_REQUEST[\'ratings\']);
    }

    if ( is_null($_REQUEST["currenteds"]) ) {
    delete_post_meta($postid, \'currenteds\');
    } else {
    update_post_meta($postid, \'currenteds\', $_REQUEST[\'currenteds\']);
    }
}

function display_current_eds($ID = \'\') {
  if (empty($ID)) {
    global $post;
    if (!empty($post)) {
      $ID = $post->ID;
    }
  }
  if (empty($ID)) return false;
  $eds = get_post_meta($post->ID,\'currenteds\',true);
  if (!empty($eds)) {
    foreach ($eds as $e) {
      $edu = get_userdata($e);
      $edusers[] = sprintf(
        \'<a href="%1$s" title="%2$s" rel="author">%3$s</a>\',
        get_author_posts_url( $edu->ID, $edu->user_nicename ),
        esc_attr( sprintf( __( \'Posts by %s\' ), $edu->user_nicename ) ),
        $edu->user_nicename
      );
    }
    return $edusers;
  }
  return false;
}

function authors_content_filter($content) {
  $authors = display_current_eds();
  if (false !== $authors) {
    $content .= implode(\', \',$authors);
  }
  return $content;
}
add_filter(\'the_content\',\'authors_content_filter\');

    // add author checkboxes
现在,问题是用户可以勾选自己的名字and any of the other editors. 这意味着用户也可以取消选中任何名称。这不好。

我的问题是,我该如何做才能让用户只勾选自己的名字,而其他名字会变灰?

2 个回复
最合适的回答,由SO网友:diggy 整理而成

使用与$checked相同的技巧:

$disabled = ( $ed->ID != get_current_user_id() ) ? \' disabled="disabled"\' : \'\';
您的代码:

foreach ($alleds as $ed) {
    $checked = (in_array($ed->ID,(array)$currenteds)) ? \' checked="checked"\' : \'\';
    $disabled = ( $ed->ID != get_current_user_id() ) ? \' disabled="disabled"\' : \'\';
    echo \'<input type="checkbox" name="currenteds[]" value="\' . $ed->ID . \'"\' . $checked . $disabled . \'"/><label for="ratings[]">\'.$ed->user_nicename.\'</label>\';
}

SO网友:s_ha_dum

根据当前用户ID检查编辑器ID并添加disabled="disabled" 如果他们不匹配。

$current_user = get_currentuseinfo();
foreach ($alleds as $ed) {
    $checked = (in_array($ed->ID,(array)$currenteds)) ? \' checked="checked"\' : \'\';
    $disabled = (!current_user_can(\'administrator\') && $current_user->ID !== $ed->ID) ? \' disabled="disabled" : \'\';
    echo \'<input type="checkbox" name="currenteds[]" value="\' . $ed->ID . \'"\' .$checked . \'" \'.$disabled.\' /><label for="ratings[]">\'.$ed->user_nicename.\'</label>\';
}
请注意,这并不是万无一失的。我可以在10秒内绕过它。认为这只是视觉上的便利。在保存数据时,您也会希望执行相同的操作。这更为棘手。我想我会改变这一点:

if ( is_null($_REQUEST["currenteds"]) ) {
    delete_post_meta($postid, \'currenteds\');
} else {
    update_post_meta($postid, \'currenteds\', $_REQUEST[\'currenteds\']);
}
更像是:

// admins have full capabilities
if (current_user_can(\'administrator\')) {
    if ( is_null($_REQUEST["currenteds"]) ) {
        delete_post_meta($postid, \'currenteds\');
    } else {
        update_post_meta($postid, \'currenteds\', $_REQUEST[\'currenteds\']);
    }
} else {
   $meta_values = get_post_meta($postid, \'currenteds\', true);
   // I don\'t remember what the relevant arrays look like
   // even though I wrote much of that code :)
   // Here is the idea
   // Check the IDs in $meta_values against the IDs in $_REQUEST[\'currenteds\']
   // and only allow manipulation of IDs that match get_currentuserinfo()->ID
   // I\'ll have to install the code to do better but if you can\'t get it I will.
}
两种改进方法,轻度测试。

function editor_tasks( $post ) {
    global $wpdb;
    $value = get_post_meta($post->ID, \'ratings\', true);

    echo \'<div class="misc-pub-section misc-pub-section-last"><span id="timestamp"><label>Editorial tasks: </label>\';

    $ratings = array(
        1 => \' Proofread \',
        2 => \' Graphics Added \',
        3 => \' SEO Fixed \',
        4 => \' Ready for Publish \'
    );

    foreach ($ratings as $id => $text) {
    $checked = (in_array($id,(array)$value)) ? \' checked="checked"\' : \'\';
    echo \'<input type="checkbox" name="ratings[]" value="\' . $id . \'"\'.  $checked . \'/><label for="ratings[]">\'.$text.\'</label>\';
    }

    $qry[\'relation\'] = \'OR\';
    $qry[] = array(
      \'key\' => $wpdb->prefix.\'capabilities\',
      \'value\' => \'editor\',
      \'compare\' => \'like\'
    );
    $qry[] = array(
      \'key\' => $wpdb->prefix.\'capabilities\',
      \'value\' => \'administrator\',
      \'compare\' => \'like\'
    );
    $qry = array(\'fields\' => \'all_with_meta\',\'meta_query\'=>$qry);

    $alleds = get_users($qry);

    $currenteds = get_post_meta($post->ID, \'currenteds\', true);

    global $current_user;
    get_currentuserinfo();
    foreach ($alleds as $ed) {
    $checked = (in_array($ed->ID,(array)$currenteds)) ? \' checked="checked"\' : \'\';
    $disabled = (!current_user_can(\'administrator\') && $current_user->ID !== $ed->ID) ? \' disabled="disabled"\' : \'\';
    echo \'<input type="checkbox" name="currenteds[]" value="\' . $ed->ID . \'" \' .$checked . \' \'.$disabled.\' /><label for="ratings[]">\'.$ed->user_nicename.\'</label>\';
    }
    echo \'</span></div>\';
}
还有。。。

function save_metadata($postid)
{
    $rid = wp_is_post_revision($postid);
    if ($postid !== $rid) $postid = $rid;

    if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ) return false;
    if ( !current_user_can( \'edit_page\', $postid ) ) return false;
    if( empty($postid) ) return false;

    if (!empty($_REQUEST[\'ratings\'])) {
      if ( is_null($_REQUEST["ratings"]) ) {
      delete_post_meta($postid, \'ratings\');
      } else {
      update_post_meta($postid, \'ratings\', $_REQUEST[\'ratings\']);
      }
    }

    // admins have full capabilities
    if (current_user_can(\'administrator\')) {
    if ( is_null($_REQUEST["currenteds"]) ) {
        delete_post_meta($postid, \'currenteds\');
    } else {
        update_post_meta($postid, \'currenteds\', $_REQUEST[\'currenteds\']);
    }
    } else {
      global $current_user;
      get_currentuserinfo();
      $meta_values = get_post_meta($postid, \'currenteds\', true);
      if (!empty($_REQUEST["currenteds"]) && in_array($current_user->ID,$_REQUEST["currenteds"])) {
    $meta_values[] = "$current_user->ID";
      } else {
    $u = array_search($current_user->ID,$meta_values);
    var_dump($u);
    if (false !== $u) {
      unset($meta_values[$u]);
    }
      }
      $meta_values = array_unique($meta_values);
      update_post_meta($postid, \'currenteds\', $meta_values);
    }
}

结束

相关推荐

WooCommerce cart.php模板应该如何使用?

我有一个新安装的Wordpress 3.5和WooCommerce 1.6.6(从现在开始使用WC),并使用默认的十二主题。我在WC中添加了一个产品,当查看该产品并使用Debug Bar Template Trace plug-in 我可以看到模板woocommerce/templates/single-product.php 被使用了。但是,如果我查看由WooCommerce自动生成的购物车页面,它会使用twentytwelve/page.php 模板,而不是woocommerce/templates/