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; }
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); }
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)))); } }
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; }