编辑函数以根据用户角色采取不同的操作

时间:2020-05-05 作者:Patrick

我想修改下面的publishorpend函数,以便在决定是否将帖子状态设置为“挂起”或“发布”时考虑当前登录用户的用户组(此外还要考虑submission\\u requires\\u approval是true还是false)。例如:

如果submission\\u requires\\u approval为false,则无论用户角色是什么,post\\u状态都应设置为publish(这是该功能当前的工作方式)

  • 如果当前用户是管理员或编辑的成员,则帖子状态应设置为“发布”。请注意,当前用户可能分配了多个角色
  • 如果submission\\u requires\\u approval为true,并且当前用户不是管理员/编辑组(或根本没有登录*),则post\\u状态应设置为pending*这很理想,但并非绝对必要
            public function publishorpend( $listing_id ) {
            $listing = \\MyListing\\Src\\Listing::force_get( $listing_id );
    
            $post_status = mylisting_get_setting( \'submission_requires_approval\' ) ? \'pending\' : \'publish\';
    
            wp_update_post( [
                \'ID\' => $listing->get_id(),
                \'post_status\' => $post_status,
            ] );
    
        }
    
    我发现这个功能可能很有用:

    function wcmo_get_current_user_roles() {
      if( is_user_logged_in() ) {
        $user = wp_get_current_user();
        $roles = ( array ) $user->roles;
        return $roles; // This returns an array
        // Use this to return a single value
        // return $roles[0];
      } else {
        return array();
      }
    }
    
    谢谢!

  • 1 个回复
    SO网友:Antti Koskinen

    wp_update_post() 最终使用wp_insert_post(), 其中wp_insert_post_data 在里面。过滤器“在将post数据插入数据库之前对其进行了筛选。”我认为这是添加您描述的自定义逻辑的合适时机publishorpend 方法中似乎没有其他可用的筛选器。

    你也许可以这样做,

    add_filter(\'wp_insert_post_data\', \'filter_wp_insert_post_data\', 10, 2);
    
    function filter_wp_insert_post_data( $data, $postarr ) {
      // Only affect custom post type. Update the post type slug!
      if ( \'my_post_type\' !== get_post_type($data[\'ID\']) ) {
        return $data;
      }
    
      // You may want to have some extra checks so that the status change doesn\'t affect every post of this type
      // if ( ! some_theme_or_plugin_function_to_check_if_orphaned($data[\'ID\']) ) {
      //   return $data;
      // }
    
      // assuming the function returns boolean or truthy/falsy value
      $requires_approval = mylisting_get_setting( \'submission_requires_approval\' ); 
    
      // If submission_requires_approval is false, post_status should be set to publish no matter what the user role is (this is how the function works currently).
      if ( ! $requires_approval ) {
        return $data;
      }
    
      // if current user is a member of administrators or editors, the post status should be set to publish. note, the current user may have more than one role assigned.
      // As a personal preference, check capabilities instead of role. Admins and editors can edit_others_posts
      if ( current_user_can( \'edit_others_posts\' ) ) {
        $data[\'post_status\'] = \'publish\';
      // If submission_requires_approval is true and if the current user is a group other than admin/editor (or is not logged in at all*) post_status should be set to pending.
      } else if ( $requires_approval ) {
        $data[\'post_status\'] = \'pending\';
      }
    
      return $data;
    }
    
    这只是一个示例,可能需要一些调整来匹配您的确切设置和需要。