Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up on the Modules Code #71

Merged
merged 1 commit into from
Sep 24, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions modules/attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,23 @@ class Attachment extends Base {
public $page = false;

public function init() {
add_filter( "fakerpress.module.{$this->slug}.save", array( $this, 'do_save' ), 10, 4 );
add_filter( "fakerpress.module.{$this->slug}.save", array( $this, 'do_save' ), 10, 3 );
}

public function do_save( $return_val, $params, $metas, $module ){
if ( empty( $params['attachment_url'] ) ){
public function do_save( $return_val, $data, $module ) {
if ( empty( $data['attachment_url'] ) ){
return false;
}

$bits = file_get_contents( $params['attachment_url'] );
$bits = file_get_contents( $data['attachment_url'] );
$filename = $this->faker->uuid() . '.jpg';
$upload = wp_upload_bits( $filename, null, $bits );

$params['guid'] = $upload['url'];
$params['post_mime_type'] = 'image/jpeg';
$data['guid'] = $upload['url'];
$data['post_mime_type'] = 'image/jpeg';

// Insert the attachment.
$attach_id = wp_insert_attachment( $params, $upload['file'], 0 );
$attach_id = wp_insert_attachment( $data, $upload['file'], 0 );

if ( ! is_numeric( $attach_id ) ){
return false;
Expand All @@ -46,11 +46,10 @@ public function do_save( $return_val, $params, $metas, $module ){
}

// Generate the metadata for the attachment, and update the database record.
$metas['_wp_attachment_metadata'] = wp_generate_attachment_metadata( $attach_id, $upload['file'] );
update_post_meta( $attach_id, '_wp_attachment_metadata', wp_generate_attachment_metadata( $attach_id, $upload['file'] ) );

foreach ( $metas as $key => $value ) {
update_post_meta( $attach_id, $key, $value );
}
// Flag the Object as FakerPress
update_post_meta( $attach_id, self::$flag, 1 );

return $attach_id;
}
Expand Down
191 changes: 52 additions & 139 deletions modules/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ abstract class Base {

public $faker = null;

public $params = array();

public $meta = array();

public $faked = array();
public $data = array();

public $dependencies = array();

Expand All @@ -28,7 +24,7 @@ abstract class Base {

public $slug = null;

final public static function instance() {
public static function instance() {
$class_name = get_called_class();
$reflection = new \ReflectionClass( $class_name );
$slug = strtolower( $reflection->getShortName() );
Expand All @@ -38,11 +34,7 @@ final public static function instance() {
}

self::$_instances[ $slug ]->slug = $slug;
self::$_instances[ $slug ]->params = array();

if ( is_array( self::$_instances[ $slug ]->meta ) ){
self::$_instances[ $slug ]->meta( self::$flag, null, 1 );
}
self::$_instances[ $slug ]->data = array();

return self::$_instances[ $slug ];
}
Expand Down Expand Up @@ -78,32 +70,27 @@ final public function __construct() {
$this->faker->addProvider( $provider );
}

if ( ! empty( $this->provider ) ){
// Create a Reflection of the Provider class to discover all the methods that will fake an Argument
$provider_reflection = new \ReflectionClass( $this->provider );
$provider_methods = $provider_reflection->getMethods();

// Loop and verify which methods are will be faked on `generate`
foreach ( $provider_methods as $method ) {
if ( $provider_reflection->getName() !== $method->class ){
continue;
}
$this->faked[] = $method->name;
}
}

// Execute a method that can be overwritten by the called class
$this->init();

// Create a meta with the FakerPress flag, always
$this->meta( self::$flag, null, 1 );

if ( $this->page ){
add_action( 'admin_menu', array( $this, '_action_setup_admin_page' ) );
add_action( 'fakerpress.view.request.' . $this->page->view, array( &$this, '_action_parse_request' ) );
}
}

public function _action_setup_admin_page() {
if ( ! $this->page ){
return;
}

\FakerPress\Admin::add_menu( $this->page->view, $this->page->title, $this->page->menu, 'manage_options', 10 );
}

public function _action_parse_request( $view ) {
return;
}

/**
* Method that will be called from the construct which is a final function
* @return null
Expand All @@ -120,31 +107,46 @@ public function get_amount_allowed() {
return apply_filters( "fakerpress.module.{$this->slug}.amount_allowed", 15, $this );
}

final public function set( $key ) {
if ( ! is_string( $key ) ){
return null;
}

// Allow a bunch of params
$arguments = func_get_args();

// Remove $key
array_shift( $arguments );

$this->data[ $key ] = (object) array(
'key' => $key,
'generator' => $key,
'arguments' => (array) $arguments,
);

return $this;
}

/**
* Use this method to save the fake data to the database
*
* @return int|bool|WP_Error Should return an error, or the $wpdb->insert_id or bool for the state
*/
final public function save( $reset = true ) {
do_action( "fakerpress.module.{$this->slug}.pre_save", $this );

$params = array();
foreach ( $this->params as $key => $param ) {
if ( is_object( $param ) ){
$params[ $param->key ] = $param->value;
$data = array();
foreach ( $this->data as $key => $item ) {
if ( is_object( $item ) ){
$data[ $item->key ] = $item->value;
} else {
$params[ $key ] = $param;
$data[ $key ] = $item;
}
}

$metas = false;
if ( is_array( $this->meta ) ){
$metas = array();
foreach ( $this->meta as $meta ) {
$metas[ $meta->key ] = $meta->value;
}
}
$response = apply_filters( "fakerpress.module.{$this->slug}.save", false, $data, $this );

$response = apply_filters( "fakerpress.module.{$this->slug}.save", false, $params, $metas, $this );
// @todo Set the flag

if ( $reset ){
$this->reset();
Expand All @@ -154,50 +156,19 @@ final public function save( $reset = true ) {
}

public function reset() {
$this->params = array();
$this->metas = array();

// This needs to move away from here
$this->object_id = 0;
$this->object_name = 'post';
}

public function _action_setup_admin_page() {
if ( ! $this->page ){
return;
}

\FakerPress\Admin::add_menu( $this->page->view, $this->page->title, $this->page->menu, 'manage_options', 10 );
$this->data = array();
}

public function _action_parse_request( $view ) {
return;
}

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

$taxonomies = array_intersect( get_taxonomies( array( 'public' => true ) ), array_map( 'trim', explode( ',', Variable::super( $request, array( 'taxonomies' ), FILTER_SANITIZE_STRING ) ) ) );

if ( 0 === $qty ){
return sprintf( __( 'Zero is not a good number of %s to fake...', 'fakerpress' ), 'posts' );
}

for ( $i = 0; $i < $qty; $i++ ) {
$this->param( 'taxonomy', $taxonomies );
$this->generate();

$results[] = $this->save();
/**
* Use this method to generate all the needed data
* @return array An array of the data generated
*/
public function generate() {
foreach ( $this->data as $name => $item ) {
$this->data[ $name ]->value = $this->apply( $item );
}

$results = array_filter( (array) $results, 'absint' );

return $results;
return $this;
}

protected function apply( $item ) {
Expand All @@ -206,62 +177,4 @@ protected function apply( $item ) {
}
return call_user_func_array( array( $this->faker, $item->generator ), ( isset( $item->arguments ) ? (array) $item->arguments : array() ) );
}

final public function meta( $key, $generator = null ) {
// If there is no meta just leave
if ( ! is_array( $this->meta ) ){
return false;
}
$arguments = func_get_args();
// Remove $key and $generator
array_shift( $arguments );
array_shift( $arguments );

$this->meta[ $key ] = (object) array(
'key' => $key,
'generator' => $generator,
'arguments' => (array) $arguments,
);

return $this->meta[ $key ];
}

final public function param( $key, $arguments = array() ) {
$arguments = func_get_args();
// Remove $key
array_shift( $arguments );

$this->params[ $key ] = (object) array(
'key' => $key,
'generator' => $key,
'arguments' => (array) $arguments,
);

return $this->params[ $key ];
}

/**
* Use this method to generate all the needed data
* @return array An array of the data generated
*/
final public function generate() {
foreach ( $this->faked as $name ) {
if ( ! isset( $this->params[ $name ] ) ){
$this->params[ $name ] = (object) array(
'key' => $name,
'generator' => $name,
'arguments' => array(),
);
}
$this->params[ $name ]->value = $this->apply( $this->params[ $name ] );
}

if ( is_array( $this->meta ) ){
foreach ( $this->meta as $meta ) {
$this->meta[ $meta->key ]->value = $this->apply( $meta );
}
}

return $this;
}
}
34 changes: 19 additions & 15 deletions modules/comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,22 @@ public function init() {
'view' => 'comments',
);

add_filter( "fakerpress.module.{$this->slug}.save", array( $this, 'do_save' ), 10, 4 );
add_filter( "fakerpress.module.{$this->slug}.save", array( $this, 'do_save' ), 10, 3 );
}

public function format_link( $id ) {
return '<a href="' . esc_url( get_edit_comment_link( $id ) ) . '">' . absint( $id ) . '</a>';
}

public function do_save( $return_val, $params, $metas, $module ) {
$comment_id = wp_insert_comment( $params );
public function do_save( $return_val, $data, $module ) {
$comment_id = wp_insert_comment( $data );

if ( ! is_numeric( $comment_id ) ){
return false;
}

foreach ( $metas as $key => $value ) {
update_comment_meta( $comment_id, $key, $value );
}
// Flag the Object as FakerPress
update_post_meta( $comment_id, self::$flag, 1 );

return $comment_id;
}
Expand All @@ -55,8 +54,6 @@ public function parse_request( $qty, $request = array() ) {
return esc_attr__( 'Zero is not a good number of comments to fake...', 'fakerpress' );
}

$meta_module = Meta::instance();

$comment_content_use_html = Variable::super( $request, array( 'use_html' ), FILTER_SANITIZE_STRING, 'off' ) === 'on';
$comment_content_html_tags = array_map( 'trim', explode( ',', Variable::super( $request, array( 'html_tags' ), FILTER_SANITIZE_STRING ) ) );

Expand All @@ -66,18 +63,25 @@ public function parse_request( $qty, $request = array() ) {

$results = array();

for ( $i = 0; $i < $quantity; $i++ ) {
$this->param( 'comment_date', $min_date, $max_date );
$this->param( 'comment_content', $comment_content_use_html, array( 'elements' => $comment_content_html_tags ) );
$this->param( 'user_id', 0 );
for ( $i = 0; $i < $qty; $i++ ) {
$this->set( 'comment_date', $min_date, $max_date );
$this->set( 'comment_content', $comment_content_use_html, array( 'elements' => $comment_content_html_tags ) );
$this->set( 'user_id', 0 );

$this->generate();
$this->set( 'comment_author' );
$this->set( 'comment_parent' );
$this->set( 'comment_author_IP' );
$this->set( 'comment_agent' );
$this->set( 'comment_approved' );
$this->set( 'comment_post_ID' );
$this->set( 'comment_author_email' );
$this->set( 'comment_author_url' );

$comment_id = $this->save();
$comment_id = $this->generate()->save();

if ( $comment_id && is_numeric( $comment_id ) ){
foreach ( $metas as $meta_index => $meta ) {
$meta_module->object( $comment_id, 'comment' )->build( $meta['type'], $meta['name'], $meta )->save();
Meta::instance()->object( $comment_id, 'comment' )->generate( $meta['type'], $meta['name'], $meta )->save();
}
}
$results[] = $comment_id;
Expand Down
Loading