Skip to content

Commit

Permalink
Merge pull request #95 from jgrossi/dev
Browse files Browse the repository at this point in the history
Merge dev branch to master
  • Loading branch information
jgrossi committed Mar 17, 2016
2 parents da1b6cf + e6687b0 commit dac2b0f
Show file tree
Hide file tree
Showing 12 changed files with 248 additions and 109 deletions.
39 changes: 31 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
Wordpress Corcel
WordPress Corcel
================

> This package allows you to use Wordpress as backend (admin panel) and retrieve its data using Eloquent, with any PHP project or even framework.
> This package allows you to use WordPress as backend (admin panel) and retrieve its data using Eloquent, with any PHP project or even framework.
Corcel is a class collection created to retrieve Wordpress database data using a better syntax. It uses the [Eloquent ORM](https://github.com/illuminate/database) developed for the Laravel Framework, but you can use Corcel in any type of PHP project, with any framework, including Laravel.
Corcel is a class collection created to retrieve WordPress database data using a better syntax. It uses the [Eloquent ORM](https://github.com/illuminate/database) developed for the Laravel Framework, but you can use Corcel in any type of PHP project, with any framework, including Laravel.

This way, you can use Wordpress as the backend (admin panel), to insert posts, custom types, etc, and you can use whatever you want in the frontend, like Silex, Slim Framework, Laravel, Zend, or even pure PHP (why not?). So, just use Corcel to retrieve data from Wordpress.
This way, you can use WordPress as the backend (admin panel), to insert posts, custom types, etc, and you can use whatever you want in the frontend, like Silex, Slim Framework, Laravel, Zend, or even pure PHP (why not?). So, just use Corcel to retrieve data from WordPress.

## Installation

Expand Down Expand Up @@ -47,7 +47,7 @@ If you are using Laravel you **do not need** to configure database again. It's a
'engine' => null,
],

'wordpress' => [ // this is your Corcel database connection, where Wordpress tables are
'wordpress' => [ // this is your Corcel database connection, where WordPress tables are
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'corcel',
Expand Down Expand Up @@ -93,7 +93,7 @@ Here you have to configure the database to fit the Corcel requirements. First, y
require __DIR__ . '/vendor/autoload.php';
```

Now you must set your Wordpress database params:
Now you must set your WordPress database params:

```php
$params = array(
Expand All @@ -112,7 +112,7 @@ You can specify all Eloquent params, but some are default (but you can override
'host' => 'localhost',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => 'wp_', // Specify the prefix for wordpress tables, default prefix is 'wp_'
'prefix' => 'wp_', // Specify the prefix for WordPress tables, default prefix is 'wp_'
```

### Posts
Expand Down Expand Up @@ -354,7 +354,30 @@ And then, define the user provider in `config/auth.php` :
],
```

** Note: Corcel isn't compatible with Laravel's password resetting as of Laravel 5.2, but it should be fixed in Laravel 5.3 **
To make Laravel's Password Reset work with Corcel, we have to override how passwords are stored in the database. To do this, you must change `Auth/PasswordController.php` from :

```php
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ResetsPasswords;

class PasswordController extends Controller
{
use ResetsPasswords;
```

to

```php
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ResetsPasswords;
use Corcel\Auth\ResetsPasswords as CorcelResetsPasswords;

class PasswordController extends Controller
{
use ResetsPasswords, CorcelResetsPasswords {
CorcelResetsPasswords::resetPassword insteadof ResetsPasswords;
}
```

#### Using something else

Expand Down
26 changes: 26 additions & 0 deletions src/Auth/ResetsPasswords.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Corcel\Auth;

use Corcel\Password\PasswordService;
use Auth;

trait ResetsPasswords {
/**
* Reset the given user's password.
*
* @param \Illuminate\Contracts\Auth\CanResetPassword $user
* @param string $password
* @return void
*/
protected function resetPassword($user, $password)
{
$passwordService = new PasswordService;

$user->user_pass = $passwordService->wp_hash_password($password);

$user->save();

Auth::guard($this->getGuard())->login($user);
}
}
26 changes: 26 additions & 0 deletions src/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@

class Comment extends Model
{
const CREATED_AT = 'comment_date';
const UPDATED_AT = null;

protected $table = 'comments';
protected $primaryKey = 'comment_ID';
protected $dates = ['comment_date'];

/**
* Post relationship
Expand Down Expand Up @@ -103,4 +107,26 @@ public function newQuery($excludeDeleted = true)

return $builder;
}

public function setCreatedAt($value)
{
$field = static::CREATED_AT;
$this->{$field} = $value;

$field .= '_gmt';
$this->{$field} = $value;

return parent::setCreatedAt($value);
}

public function setUpdatedAt($value)
{
$field = static::UPDATED_AT;
$this->{$field} = $value;

$field .= '_gmt';
$this->{$field} = $value;

return parent::setUpdatedAt($value);
}
}
28 changes: 25 additions & 3 deletions src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
namespace Corcel;

use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model as Eloquent;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
Expand All @@ -24,10 +25,9 @@ public function hasMany($related, $foreignKey = null, $localKey = null)

$localKey = $localKey ?: $this->getKeyName();

return new HasMany($instance->newQuery(), $this, $instance->getTable().'.'.$foreignKey, $localKey);
return new HasMany($instance->newQuery(), $this, $foreignKey, $localKey);
}


public function belongsTo($related, $foreignKey = null, $otherKey = null, $relation = null)
{
if (is_null($relation)) {
Expand All @@ -49,7 +49,6 @@ public function belongsTo($related, $foreignKey = null, $otherKey = null, $relat

return new BelongsTo($query, $this, $foreignKey, $otherKey, $relation);
}


public function belongsToMany($related, $table = null, $foreignKey = null, $otherKey = null, $relation = null)
{
Expand All @@ -72,4 +71,27 @@ public function belongsToMany($related, $table = null, $foreignKey = null, $othe

return new BelongsToMany($query, $this, $table, $foreignKey, $otherKey, $relation);
}

public function getRelationValue($key)
{
$relation = parent::getRelationValue($key);

if ($relation instanceof Collection) {
$relation->each(function ($model) {
$this->setRelationConnection($model);
});
return $relation;
}

$this->setRelationConnection($relation);

return $relation;
}

protected function setRelationConnection($model)
{
if ($model instanceof Eloquent) {
$model->setConnection($this->getConnectionName());
}
}
}
2 changes: 1 addition & 1 deletion src/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ public function setUpdatedAt($value)
$field .= '_gmt';
$this->{$field} = $value;

return parent::setUpdatedAt($value); // TODO: Change the autogenerated stub
return parent::setUpdatedAt($value);
}

/**
Expand Down
11 changes: 1 addition & 10 deletions src/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Contracts\Auth\CanResetPassword;
use Corcel\Password\PasswordService;

class User extends Model implements Authenticatable, CanResetPassword
{
Expand All @@ -27,7 +26,7 @@ class User extends Model implements Authenticatable, CanResetPassword
public function setUpdatedAtAttribute($value) {
}


/**
* The accessors to append to the model's array form.
*
Expand Down Expand Up @@ -279,12 +278,4 @@ public function getRememberTokenName() {
public function getEmailForPasswordReset() {
return $this->user_email;
}


public function resetPassword($password)
{
$passwordService = new PasswordService;

$this->user_pass = $passwordService->wp_hash_password($password);
}
}
7 changes: 3 additions & 4 deletions src/UserMetaCollection.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php
<?php

/**
* Corcel\UserMetaCollection
*
*
* @author Mickael Burguet <www.rundef.com>
*/

Expand All @@ -16,7 +16,7 @@ class UserMetaCollection extends Collection

/**
* Search for the desired key and return only the row that represent it
*
*
* @param string $key
* @return string
*/
Expand Down Expand Up @@ -57,5 +57,4 @@ public function save($userId)
}
});
}

}
34 changes: 27 additions & 7 deletions tests/CommentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,25 @@ public function testCommentConstructor()
$comment = new Comment;
$this->assertTrue($comment instanceof \Corcel\Comment);
}

public function testCommentId()
{
$comment = Comment::find(1);

if ($comment) {
//$this->assertInternalType('integer', $comment->comment_ID);
$this->assertEquals($comment->comment_ID, 1);
} else {
$this->assertNull($comment);
}
}

public function testCommentPost()
{
$comment = Comment::find(1);
$this->assertTrue($comment->Post()->first() instanceof \Corcel\Post);
$this->assertEquals($comment->Post()->first()->ID, 1);

$this->assertTrue($comment->post()->first() instanceof \Corcel\Post);
$this->assertEquals($comment->post()->first()->ID, 1);
}

public function testCommentPostId()
Expand All @@ -42,7 +42,7 @@ public function testCommentPostId()
}

public function testOriginal()
{
{
$comment = Comment::find(2);

$this->assertTrue($comment->original()->first() instanceof \Corcel\Comment);
Expand Down Expand Up @@ -72,4 +72,24 @@ public function testCommentHasReplies()
$this->assertInternalType('boolean', $comment->hasReplies());
$this->assertTrue($comment->hasReplies());
}

public function testCommentEnforceConnection()
{
$comment = new Comment;
$comment->setConnection('no_prefix');
$comment->comment_content = 'Test content';
$comment->comment_author = 1;
$comment->comment_post_ID = 2;
$comment->save();

$post = new Post;
$post->post_content = 'Test';
$post->save();

$comment->post()->associate($post);
$comment->save();

$this->assertEquals('no_prefix', $comment->getConnectionName());
$this->assertEquals('no_prefix', $comment->post->getConnectionName());
}
}
10 changes: 9 additions & 1 deletion tests/PostTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public function testSingleTableInheritance()
$this->assertInstanceOf("\\Corcel\\Page", $page);
}

public function testclearRegisteredPostTypes()
public function testClearRegisteredPostTypes()
{
Post::registerPostType('page', "\\Corcel\\Page");
Post::clearRegisteredPostTypes();
Expand All @@ -168,4 +168,12 @@ public function testclearRegisteredPostTypes()

$this->assertInstanceOf("\\Corcel\\Post", $page);
}

public function testPostRelationConnections()
{
$post = Post::find(1);
$post->setConnection('no_prefix');

$this->assertEquals('no_prefix', $post->author->getConnectionName());
}
}
16 changes: 16 additions & 0 deletions tests/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,20 @@ public function testInsertCustomFields()
$this->assertEquals($user->meta->custom_meta1, 'Hallo');
$this->assertEquals($user->meta->custom_meta2, 'Wereld');
}

public function testUserConnection()
{
$user = new User;
$user->setConnection('no_prefix');
$user->user_login = 'test';
$user->save();

$user->meta->active = 1;
$user->save();

$this->assertEquals('no_prefix', $user->getConnection()->getName());
$user->meta->each(function ($meta) {
$this->assertEquals('no_prefix', $meta->getConnection()->getName());
});
}
}
Loading

0 comments on commit dac2b0f

Please sign in to comment.