function prepare_items()
 {
     $current_screen = get_current_screen();
     $per_page = $this->get_items_per_page($current_screen->id . '_per_page');
     $this->_column_headers = $this->get_column_info();
     $args = array('posts_per_page' => $per_page, 'offset' => ($this->get_pagenum() - 1) * $per_page, 'orderby' => 'meta_value', 'order' => 'DESC', 'meta_key' => '_last_contacted');
     if (!empty($_REQUEST['s'])) {
         $args['s'] = $_REQUEST['s'];
     }
     if (!empty($_REQUEST['orderby'])) {
         if ('email' == $_REQUEST['orderby']) {
             $args['meta_key'] = '_email';
         } elseif ('name' == $_REQUEST['orderby']) {
             $args['meta_key'] = '_name';
         }
     }
     if (!empty($_REQUEST['order']) && 'asc' == strtolower($_REQUEST['order'])) {
         $args['order'] = 'ASC';
     }
     if (!empty($_REQUEST['contact_tag_id'])) {
         $args['contact_tag_id'] = explode(',', $_REQUEST['contact_tag_id']);
     }
     $this->items = Flamingo_Contact::find($args);
     $total_items = Flamingo_Contact::$found_items;
     $total_pages = ceil($total_items / $per_page);
     $this->set_pagination_args(array('total_items' => $total_items, 'total_pages' => $total_pages, 'per_page' => $per_page));
 }
Exemplo n.º 2
0
function flamingo_load_contact_admin()
{
    $action = flamingo_current_action();
    $redirect_to = admin_url('admin.php?page=flamingo');
    if ('save' == $action && !empty($_REQUEST['post'])) {
        $post = new Flamingo_Contact($_REQUEST['post']);
        if (!empty($post)) {
            if (!current_user_can('flamingo_edit_contact', $post->id)) {
                wp_die(__('You are not allowed to edit this item.', 'flamingo'));
            }
            check_admin_referer('flamingo-update-contact_' . $post->id);
            $post->props = (array) $_POST['contact'];
            $post->name = trim($_POST['contact']['name']);
            $post->tags = !empty($_POST['tax_input'][Flamingo_Contact::contact_tag_taxonomy]) ? explode(',', $_POST['tax_input'][Flamingo_Contact::contact_tag_taxonomy]) : array();
            $post->save();
            $redirect_to = add_query_arg(array('action' => 'edit', 'post' => $post->id, 'message' => 'contactupdated'), $redirect_to);
        }
        wp_safe_redirect($redirect_to);
        exit;
    }
    if ('delete' == $action && !empty($_REQUEST['post'])) {
        if (!is_array($_REQUEST['post'])) {
            check_admin_referer('flamingo-delete-contact_' . $_REQUEST['post']);
        } else {
            check_admin_referer('bulk-posts');
        }
        $deleted = 0;
        foreach ((array) $_REQUEST['post'] as $post) {
            $post = new Flamingo_Contact($post);
            if (empty($post)) {
                continue;
            }
            if (!current_user_can('flamingo_delete_contact', $post->id)) {
                wp_die(__('You are not allowed to delete this item.', 'flamingo'));
            }
            if (!$post->delete()) {
                wp_die(__('Error in deleting.', 'flamingo'));
            }
            $deleted += 1;
        }
        if (!empty($deleted)) {
            $redirect_to = add_query_arg(array('message' => 'contactdeleted'), $redirect_to);
        }
        wp_safe_redirect($redirect_to);
        exit;
    }
    if (!empty($_GET['export'])) {
        check_admin_referer('bulk-posts');
        $sitename = sanitize_key(get_bloginfo('name'));
        $filename = (empty($sitename) ? '' : $sitename . '-') . sprintf('flamingo-contact-%s.csv', date('Y-m-d'));
        header('Content-Description: File Transfer');
        header("Content-Disposition: attachment; filename={$filename}");
        header('Content-Type: text/csv; charset=' . get_option('blog_charset'));
        $labels = array(__('Email', 'flamingo'), __('Full name', 'flamingo'), __('First name', 'flamingo'), __('Last name', 'flamingo'));
        echo flamingo_csv_row($labels);
        $args = array('posts_per_page' => -1, 'orderby' => 'meta_value', 'order' => 'ASC', 'meta_key' => '_email');
        if (!empty($_GET['s'])) {
            $args['s'] = $_GET['s'];
        }
        if (!empty($_GET['orderby'])) {
            if ('email' == $_GET['orderby']) {
                $args['meta_key'] = '_email';
            } elseif ('name' == $_GET['orderby']) {
                $args['meta_key'] = '_name';
            }
        }
        if (!empty($_GET['order']) && 'asc' == strtolower($_GET['order'])) {
            $args['order'] = 'ASC';
        }
        if (!empty($_GET['contact_tag_id'])) {
            $args['contact_tag_id'] = explode(',', $_GET['contact_tag_id']);
        }
        $items = Flamingo_Contact::find($args);
        foreach ($items as $item) {
            $row = array($item->email, $item->get_prop('name'), $item->get_prop('first_name'), $item->get_prop('last_name'));
            echo "\r\n" . flamingo_csv_row($row);
        }
        exit;
    }
    if (!empty($_GET['sendmail']) && !empty($_REQUEST['contact_tag_id'])) {
        $redirect_to = admin_url('admin.php?page=flamingo_outbound');
        $redirect_to = add_query_arg(array('action' => 'new', 'contact_tag_id' => absint($_REQUEST['contact_tag_id'])), $redirect_to);
        wp_safe_redirect($redirect_to);
        exit;
    }
    $post_id = !empty($_REQUEST['post']) ? $_REQUEST['post'] : '';
    if (Flamingo_Contact::post_type == get_post_type($post_id)) {
        add_meta_box('submitdiv', __('Save', 'flamingo'), 'flamingo_contact_submit_meta_box', null, 'side', 'core');
        add_meta_box('contacttagsdiv', __('Tags', 'flamingo'), 'flamingo_contact_tags_meta_box', null, 'side', 'core');
        add_meta_box('contactnamediv', __('Name', 'flamingo'), 'flamingo_contact_name_meta_box', null, 'normal', 'core');
    } else {
        if (!class_exists('Flamingo_Contacts_List_Table')) {
            require_once FLAMINGO_PLUGIN_DIR . '/admin/includes/class-contacts-list-table.php';
        }
        $current_screen = get_current_screen();
        add_filter('manage_' . $current_screen->id . '_columns', array('Flamingo_Contacts_List_Table', 'define_columns'));
        add_screen_option('per_page', array('label' => __('Contacts', 'flamingo'), 'default' => 20));
    }
}