Example #1
0
function flamingo_array_flatten($input)
{
    if (!is_array($input)) {
        return array($input);
    }
    $output = array();
    foreach ($input as $value) {
        $output = array_merge($output, flamingo_array_flatten($value));
    }
    return $output;
}
 public function save()
 {
     $post_title = $this->email;
     $post_name = strtr($this->email, '@', '-');
     $fields = flamingo_array_flatten($this->props);
     $fields = array_merge($fields, array($this->email, $this->name));
     $fields = array_filter(array_map('trim', $fields));
     $fields = array_unique($fields);
     $post_content = implode("\n", $fields);
     $postarr = array('ID' => absint($this->id), 'post_type' => self::post_type, 'post_status' => 'publish', 'post_title' => $post_title, 'post_name' => $post_name, 'post_content' => $post_content);
     $post_id = wp_insert_post($postarr);
     if ($post_id) {
         $this->id = $post_id;
         update_post_meta($post_id, '_email', $this->email);
         update_post_meta($post_id, '_name', $this->name);
         update_post_meta($post_id, '_props', $this->props);
         update_post_meta($post_id, '_last_contacted', $this->last_contacted);
         wp_set_object_terms($this->id, $this->tags, self::contact_tag_taxonomy);
     }
     return $post_id;
 }
Example #3
0
function flamingo_load_inbound_admin()
{
    $action = flamingo_current_action();
    $redirect_to = admin_url('admin.php?page=flamingo_inbound');
    if ('trash' == $action && !empty($_REQUEST['post'])) {
        if (!is_array($_REQUEST['post'])) {
            check_admin_referer('flamingo-trash-inbound-message_' . $_REQUEST['post']);
        } else {
            check_admin_referer('bulk-posts');
        }
        $trashed = 0;
        foreach ((array) $_REQUEST['post'] as $post) {
            $post = new Flamingo_Inbound_Message($post);
            if (empty($post)) {
                continue;
            }
            if (!current_user_can('flamingo_delete_inbound_message', $post->id)) {
                wp_die(__('You are not allowed to move this item to the Trash.', 'flamingo'));
            }
            if (!$post->trash()) {
                wp_die(__('Error in moving to Trash.', 'flamingo'));
            }
            $trashed += 1;
        }
        if (!empty($trashed)) {
            $redirect_to = add_query_arg(array('message' => 'inboundtrashed'), $redirect_to);
        }
        wp_safe_redirect($redirect_to);
        exit;
    }
    if ('untrash' == $action && !empty($_REQUEST['post'])) {
        if (!is_array($_REQUEST['post'])) {
            check_admin_referer('flamingo-untrash-inbound-message_' . $_REQUEST['post']);
        } else {
            check_admin_referer('bulk-posts');
        }
        $untrashed = 0;
        foreach ((array) $_REQUEST['post'] as $post) {
            $post = new Flamingo_Inbound_Message($post);
            if (empty($post)) {
                continue;
            }
            if (!current_user_can('flamingo_delete_inbound_message', $post->id)) {
                wp_die(__('You are not allowed to restore this item from the Trash.', 'flamingo'));
            }
            if (!$post->untrash()) {
                wp_die(__('Error in restoring from Trash.', 'flamingo'));
            }
            $untrashed += 1;
        }
        if (!empty($untrashed)) {
            $redirect_to = add_query_arg(array('message' => 'inbounduntrashed'), $redirect_to);
        }
        wp_safe_redirect($redirect_to);
        exit;
    }
    if ('delete_all' == $action) {
        check_admin_referer('bulk-posts');
        $_REQUEST['post'] = flamingo_get_all_ids_in_trash(Flamingo_Inbound_Message::post_type);
        $action = 'delete';
    }
    if ('delete' == $action && !empty($_REQUEST['post'])) {
        if (!is_array($_REQUEST['post'])) {
            check_admin_referer('flamingo-delete-inbound-message_' . $_REQUEST['post']);
        } else {
            check_admin_referer('bulk-posts');
        }
        $deleted = 0;
        foreach ((array) $_REQUEST['post'] as $post) {
            $post = new Flamingo_Inbound_Message($post);
            if (empty($post)) {
                continue;
            }
            if (!current_user_can('flamingo_delete_inbound_message', $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' => 'inbounddeleted'), $redirect_to);
        }
        wp_safe_redirect($redirect_to);
        exit;
    }
    if ('spam' == $action && !empty($_REQUEST['post'])) {
        if (!is_array($_REQUEST['post'])) {
            check_admin_referer('flamingo-spam-inbound-message_' . $_REQUEST['post']);
        } else {
            check_admin_referer('bulk-posts');
        }
        $submitted = 0;
        foreach ((array) $_REQUEST['post'] as $post) {
            $post = new Flamingo_Inbound_Message($post);
            if (empty($post)) {
                continue;
            }
            if (!current_user_can('flamingo_spam_inbound_message', $post->id)) {
                wp_die(__('You are not allowed to spam this item.', 'flamingo'));
            }
            if ($post->spam()) {
                $submitted += 1;
            }
        }
        if (!empty($submitted)) {
            $redirect_to = add_query_arg(array('message' => 'inboundspammed'), $redirect_to);
        }
        wp_safe_redirect($redirect_to);
        exit;
    }
    if ('unspam' == $action && !empty($_REQUEST['post'])) {
        if (!is_array($_REQUEST['post'])) {
            check_admin_referer('flamingo-unspam-inbound-message_' . $_REQUEST['post']);
        } else {
            check_admin_referer('bulk-posts');
        }
        $submitted = 0;
        foreach ((array) $_REQUEST['post'] as $post) {
            $post = new Flamingo_Inbound_Message($post);
            if (empty($post)) {
                continue;
            }
            if (!current_user_can('flamingo_unspam_inbound_message', $post->id)) {
                wp_die(__('You are not allowed to unspam this item.', 'flamingo'));
            }
            if ($post->unspam()) {
                $submitted += 1;
            }
        }
        if (!empty($submitted)) {
            $redirect_to = add_query_arg(array('message' => 'inboundunspammed'), $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-inbound-%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'));
        $args = array('posts_per_page' => -1, 'orderby' => 'date', 'order' => 'DESC');
        if (!empty($_REQUEST['s'])) {
            $args['s'] = $_REQUEST['s'];
        }
        if (!empty($_REQUEST['orderby'])) {
            if ('subject' == $_REQUEST['orderby']) {
                $args['meta_key'] = '_subject';
                $args['orderby'] = 'meta_value';
            } elseif ('from' == $_REQUEST['orderby']) {
                $args['meta_key'] = '_from';
                $args['orderby'] = 'meta_value';
            }
        }
        if (!empty($_REQUEST['order']) && 'asc' == strtolower($_REQUEST['order'])) {
            $args['order'] = 'ASC';
        }
        if (!empty($_REQUEST['m'])) {
            $args['m'] = $_REQUEST['m'];
        }
        if (!empty($_REQUEST['channel_id'])) {
            $args['channel_id'] = $_REQUEST['channel_id'];
        }
        if (!empty($_REQUEST['channel'])) {
            $args['channel'] = $_REQUEST['channel'];
        }
        $items = Flamingo_Inbound_Message::find($args);
        if (empty($items)) {
            exit;
        }
        $labels = array_keys($items[0]->fields);
        $labels[] = __('Date', 'flamingo');
        echo flamingo_csv_row($labels);
        foreach ($items as $item) {
            $row = array();
            foreach ($labels as $label) {
                $col = isset($item->fields[$label]) ? $item->fields[$label] : '';
                if (is_array($col)) {
                    $col = flamingo_array_flatten($col);
                    $col = array_filter(array_map('trim', $col));
                    $col = implode(', ', $col);
                }
                $row[] = $col;
            }
            $row[] = get_post_time('c', true, $item->id);
            echo "\r\n" . flamingo_csv_row($row);
        }
        exit;
    }
    $post_id = !empty($_REQUEST['post']) ? $_REQUEST['post'] : '';
    if (Flamingo_Inbound_Message::post_type == get_post_type($post_id)) {
        add_meta_box('submitdiv', __('Save', 'flamingo'), 'flamingo_inbound_submit_meta_box', null, 'side', 'core');
        add_meta_box('inboundfieldsdiv', __('Fields', 'flamingo'), 'flamingo_inbound_fields_meta_box', null, 'normal', 'core');
    } else {
        if (!class_exists('Flamingo_Inbound_Messages_List_Table')) {
            require_once FLAMINGO_PLUGIN_DIR . '/admin/includes/class-inbound-messages-list-table.php';
        }
        $current_screen = get_current_screen();
        add_filter('manage_' . $current_screen->id . '_columns', array('Flamingo_Inbound_Messages_List_Table', 'define_columns'));
        add_screen_option('per_page', array('label' => __('Messages', 'flamingo'), 'default' => 20));
    }
}
 public function save()
 {
     if (!empty($this->subject)) {
         $post_title = $this->subject;
     } else {
         $post_title = __('(No Title)', 'flamingo');
     }
     $fields = flamingo_array_flatten($this->fields);
     $fields = array_filter(array_map('trim', $fields));
     $post_content = implode("\n", $fields);
     $post_status = $this->spam ? self::spam_status : 'publish';
     $postarr = array('ID' => absint($this->id), 'post_type' => self::post_type, 'post_status' => $post_status, 'post_title' => $post_title, 'post_content' => $post_content);
     $post_id = wp_insert_post($postarr);
     if ($post_id) {
         $this->id = $post_id;
         update_post_meta($post_id, '_subject', $this->subject);
         update_post_meta($post_id, '_from', $this->from);
         update_post_meta($post_id, '_from_name', $this->from_name);
         update_post_meta($post_id, '_from_email', $this->from_email);
         foreach ($this->fields as $key => $value) {
             $meta_key = sanitize_key('_field_' . $key);
             update_post_meta($post_id, $meta_key, $value);
             $this->fields[$key] = null;
         }
         update_post_meta($post_id, '_fields', $this->fields);
         update_post_meta($post_id, '_meta', $this->meta);
         update_post_meta($post_id, '_akismet', $this->akismet);
         if (term_exists($this->channel, self::channel_taxonomy)) {
             wp_set_object_terms($this->id, $this->channel, self::channel_taxonomy);
         }
     }
     return $post_id;
 }