Example #1
0
 public function parse_request($qty, $request = array())
 {
     if (is_null($qty)) {
         $qty = Variable::super(INPUT_POST, array(Plugin::$slug, 'qty'), FILTER_UNSAFE_RAW);
         $min = absint($qty['min']);
         $max = max(absint(isset($qty['max']) ? $qty['max'] : 0), $min);
         $qty = $this->faker->numberBetween($min, $max);
     }
     if (0 === $qty) {
         return esc_attr__('Zero is not a good number of posts to fake...', 'fakerpress');
     }
     $comment_status = array_map('trim', explode(',', Variable::super($request, array('comment_status'), FILTER_SANITIZE_STRING)));
     $post_author = array_intersect(get_users(array('fields' => 'ID')), array_map('trim', explode(',', Variable::super($request, array('author')))));
     $min_date = Variable::super($request, array('interval_date', 'min'));
     $max_date = Variable::super($request, array('interval_date', 'max'));
     $post_types = array_intersect(get_post_types(array('public' => true)), array_map('trim', explode(',', Variable::super($request, array('post_types'), FILTER_SANITIZE_STRING))));
     $taxonomies = array_intersect(get_taxonomies(array('public' => true)), array_map('trim', explode(',', Variable::super($request, array('taxonomies'), FILTER_SANITIZE_STRING))));
     $post_content_use_html = Variable::super($request, array('use_html'), FILTER_SANITIZE_NUMBER_INT, 0) === 1;
     $post_content_html_tags = array_map('trim', explode(',', Variable::super($request, array('html_tags'), FILTER_SANITIZE_STRING)));
     $post_parents = array_map('trim', explode(',', Variable::super($request, array('post_parent'), FILTER_SANITIZE_STRING)));
     $featured_image_rate = absint(Variable::super($request, array('featured_image_rate'), FILTER_SANITIZE_NUMBER_INT));
     $images_origin = array_map('trim', explode(',', Variable::super($request, array('images_origin'), FILTER_SANITIZE_STRING)));
     $metas = Variable::super($request, array('meta'), FILTER_UNSAFE_RAW);
     $attach_module = Attachment::instance();
     $meta_module = Meta::instance();
     $results = array();
     for ($i = 0; $i < $qty; $i++) {
         $this->param('tax_input', $taxonomies);
         $this->param('post_status', 'publish');
         $this->param('post_date', array($min_date, $max_date));
         $this->param('post_parent', $post_parents);
         $this->param('post_content', $post_content_use_html, array('elements' => $post_content_html_tags));
         $this->param('post_author', $post_author);
         $this->param('post_type', $post_types);
         $this->param('comment_status', $comment_status);
         $post_id = $this->generate()->save();
         if ($post_id && is_numeric($post_id)) {
             foreach ($metas as $meta_index => $meta) {
                 $meta_module->object($post_id)->build($meta['type'], $meta['name'], $meta)->save();
             }
             if ($this->faker->numberBetween(0, 100) <= $featured_image_rate) {
                 $attach_module->param('attachment_url', $this->faker->randomElement($images_origin));
                 $attach_module->param('post_parent', $post_id, 1);
                 $attach_module->generate();
                 $attachment_id = $attach_module->save();
                 $meta_module->object($post_id)->build('raw', '_thumbnail_id', array(100, $attachment_id, 0))->save();
             }
         }
         $results[] = $post_id;
     }
     $results = array_filter((array) $results, 'absint');
     return $results;
 }
Example #2
0
$post_types = get_post_types(array('public' => true), 'object');
// Exclude Attachments as we don't support images yet
if (isset($post_types['attachment'])) {
    unset($post_types['attachment']);
}
$_json_post_types_output = array();
foreach ($post_types as $key => $post_type) {
    $_json_post_types_output[] = array('hierarchical' => $post_type->hierarchical, 'id' => $post_type->name, 'text' => $post_type->labels->name);
}
$fields[] = new Field('dropdown', array('id' => 'post_types', 'multiple' => true, 'data-options' => $_json_post_types_output, 'value' => 'post'), array('label' => __('Post Type', 'fakerpress'), 'description' => __('Sampling group of Post Types', 'fakerpress')));
$fields[] = new Field('dropdown', array('id' => 'post_parent', 'multiple' => true, 'data-source' => 'WP_Query'), array('label' => __('Parents', 'fakerpress'), 'description' => __('What posts can be choosen as Parent to the ones created', 'fakerpress')));
$fields[] = new Field('dropdown', array('id' => 'comment_status', 'multiple' => true, 'value' => 'open', 'data-options' => array(array('id' => 'open', 'text' => esc_attr__('Allow Comments', 'fakerpress')), array('id' => 'closed', 'text' => esc_attr__('Comments closed', 'fakerpress')))), array('label' => __('Comments Status', 'fakerpress'), 'description' => __('Sampling group of options for the comment status of the posts', 'fakerpress')));
$fields[] = new Field('checkbox', array('id' => 'use_html', 'options' => array(array('text' => __('Use HTML on your randomized post content?', 'fakerpress'), 'value' => 1)), 'value' => 1), array('label' => __('Use HTML', 'fakerpress')));
$_elements = array_merge(\Faker\Provider\HTML::$sets['header'], \Faker\Provider\HTML::$sets['list'], \Faker\Provider\HTML::$sets['block'], \Faker\Provider\HTML::$sets['self_close']);
$fields[] = new Field('dropdown', array('id' => 'html_tags', 'multiple' => true, 'data-tags' => true, 'data-options' => $_elements, 'value' => implode(',', $_elements)), array('label' => __('HTML tags', 'fakerpress'), 'description' => __('Select the group of tags that can be selected to print on the Post Content', 'fakerpress')));
$fields[] = new Field('dropdown', array('id' => 'images_origin', 'multiple' => true, 'value' => implode(',', wp_list_pluck(Module\Attachment::get_providers(), 'id')), 'data-options' => Module\Attachment::get_providers()), array('label' => __('Image Providers', 'fakerpress'), 'description' => __('Which image services will the generator use?', 'fakerpress')));
// Mount the options for Users
$users = get_users(array('blog_id' => $GLOBALS['blog_id'], 'count_total' => false, 'fields' => array('ID', 'display_name')));
$_json_users_output = array();
foreach ($users as $user) {
    $_json_users_output[] = array('id' => $user->ID, 'text' => esc_attr($user->display_name));
}
$fields[] = new Field('dropdown', array('id' => 'author', 'multiple' => true, 'data-options' => $_json_users_output), array('label' => __('Author', 'fakerpress'), 'description' => __('Choose some users to be authors of posts generated.', 'fakerpress')));
$fields[] = new Field('taxonomy', array('id' => 'taxonomy'), array('label' => __('Taxonomy Field Rules', 'fakerpress'), 'description' => __('Use the fields below to configure the rules for the Taxonomy and Terms selected', 'fakerpress')));
$fields[] = new Field('meta', array('id' => 'meta', 'config' => array(array('type' => 'attachment', 'name' => '_thumbnail_id', 'weight' => 75, 'store' => 'id'))), array('label' => __('Meta Field Rules', 'fakerpress'), 'description' => __('Use the fields below to configure a set of rules for your generated Posts', 'fakerpress')));
?>
<div class='wrap'>
	<h2><?php 
echo esc_attr(Admin::$view->title);
?>
</h2>
Example #3
0
 public function get_img_src($sources = array('lorempixel', 'placeholdit', 'unsplashit'))
 {
     $images = \FakerPress\Module\Post::fetch(array('post_type' => 'attachment'));
     $image = false;
     $count_images = count($images);
     $optional = $count_images * 2;
     $optional = $optional > 100 ? 100 : $optional;
     if ($count_images > 0) {
         $image = $this->generator->optional($optional, $image)->randomElement($images);
     }
     if (false === $image) {
         $image = \FakerPress\Module\Attachment::instance()->set('attachment_url', $this->generator->randomElement($sources))->generate()->save();
     }
     return wp_get_attachment_url($image);
 }
Example #4
0
 public function _action_parse_request($view)
 {
     if ('post' !== Admin::$request_method || empty($_POST)) {
         return false;
     }
     $nonce_slug = Plugin::$slug . '.request.' . Admin::$view->slug . (isset(Admin::$view->action) ? '.' . Admin::$view->action : '');
     if (!check_admin_referer($nonce_slug)) {
         return false;
     }
     // After this point we are safe to say that we have a good POST request
     $qty_min = absint(Filter::super(INPUT_POST, array('fakerpress', 'qty', 'min'), FILTER_SANITIZE_NUMBER_INT));
     $qty_max = absint(Filter::super(INPUT_POST, array('fakerpress', 'qty', 'max'), FILTER_SANITIZE_NUMBER_INT));
     $comment_status = array_map('trim', explode(',', Filter::super(INPUT_POST, array('fakerpress', 'comment_status'), FILTER_SANITIZE_STRING)));
     $post_author = array_intersect(get_users(array('fields' => 'ID')), array_map('trim', explode(',', Filter::super(INPUT_POST, array('fakerpress', 'author')))));
     $min_date = Filter::super(INPUT_POST, array('fakerpress', 'interval_date', 'min'));
     $max_date = Filter::super(INPUT_POST, array('fakerpress', 'interval_date', 'max'));
     $post_types = array_intersect(get_post_types(array('public' => true)), array_map('trim', explode(',', Filter::super(INPUT_POST, array('fakerpress', 'post_types'), FILTER_SANITIZE_STRING))));
     $taxonomies = array_intersect(get_taxonomies(array('public' => true)), array_map('trim', explode(',', Filter::super(INPUT_POST, array('fakerpress', 'taxonomies'), FILTER_SANITIZE_STRING))));
     $post_content_use_html = Filter::super(INPUT_POST, array('fakerpress', 'use_html'), FILTER_SANITIZE_NUMBER_INT, 0) === 1;
     $post_content_html_tags = array_map('trim', explode(',', Filter::super(INPUT_POST, array('fakerpress', 'html_tags'), FILTER_SANITIZE_STRING)));
     $post_parents = array_map('trim', explode(',', Filter::super(INPUT_POST, array('fakerpress', 'post_parent'), FILTER_SANITIZE_STRING)));
     $featured_image_rate = absint(Filter::super(INPUT_POST, array('fakerpress', 'featured_image_rate'), FILTER_SANITIZE_NUMBER_INT));
     $images_origin = array_map('trim', explode(',', Filter::super(INPUT_POST, array('fakerpress', 'images_origin'), FILTER_SANITIZE_STRING)));
     $metas = Filter::super(INPUT_POST, array('fakerpress', 'meta'), FILTER_UNSAFE_RAW);
     $attach_module = Attachment::instance();
     $meta_module = Meta::instance();
     if (0 === $qty_min) {
         return Admin::add_message(sprintf(__('Zero is not a good number of %s to fake...', 'fakerpress'), 'posts'), 'error');
     }
     if (!empty($qty_min) && !empty($qty_max)) {
         $quantity = $this->faker->numberBetween($qty_min, $qty_max);
     }
     if (!empty($qty_min) && empty($qty_max)) {
         $quantity = $qty_min;
     }
     $results = (object) array();
     for ($i = 0; $i < $quantity; $i++) {
         $this->param('tax_input', $taxonomies);
         $this->param('post_status', 'publish');
         $this->param('post_date', array($min_date, $max_date));
         $this->param('post_parent', $post_parents);
         $this->param('post_content', $post_content_use_html, array('elements' => $post_content_html_tags));
         $this->param('post_author', $post_author);
         $this->param('post_type', $post_types);
         $this->param('comment_status', $comment_status);
         $this->generate();
         $post_id = $this->save();
         if ($post_id && is_numeric($post_id)) {
             foreach ($metas as $meta_index => $meta) {
                 $meta_module->object($post_id)->build($meta['type'], $meta['name'], $meta)->save();
             }
             if ($this->faker->numberBetween(0, 100) <= $featured_image_rate) {
                 $attach_module->param('attachment_url', $this->faker->randomElement($images_origin));
                 $attach_module->param('post_parent', $post_id, 1);
                 $attach_module->generate();
                 $attachment_id = $attach_module->save();
                 $meta_module->object($post_id)->build('raw', '_thumbnail_id', array(100, $attachment_id, 0))->save();
             }
         }
         $results->all[] = $post_id;
     }
     $results->success = array_filter($results->all, 'absint');
     if (!empty($results->success)) {
         return Admin::add_message(sprintf(__('Faked %d new %s: [ %s ]', 'fakerpress'), count($results->success), _n('post', 'posts', count($results->success), 'fakerpress'), implode(', ', array_map(function ($id) {
             return '<a href="' . esc_url(get_edit_post_link($id)) . '">' . absint($id) . '</a>';
         }, $results->success))));
     }
 }
Example #5
0
 public function meta_type_attachment($type, $providers, $weight = 50)
 {
     $providers = array_map('esc_attr', array_map('trim', explode(',', $providers)));
     // Generate the Attachment
     $attachment = FakerPress\Module\Attachment::instance()->set('attachment_url', $this->generator->randomElement($providers));
     // If it's meta for a post we need to mark the attachment as child of that post
     if ('post' === $this->meta_object->name) {
         $attachment->set('post_parent', $this->meta_object->id, 1);
     }
     // Actually save the Attachment and get it's ID
     $value = $attachment->generate()->save();
     // If there was an error, just bail now
     if (!$value) {
         return null;
     }
     // If asked URL, change to URL
     if ('url' === $type) {
         $value = wp_get_attachment_url($value);
     }
     // Apply Weight
     $value = $this->generator->optional((int) $weight, null)->randomElement((array) $value);
     return $value;
 }