PHP code example of pollora / colt

1. Go to this page and download the library: Download pollora/colt library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

pollora / colt example snippets


use Pollora\Colt\Model\Post;

// All published posts
$posts = Post::published()->get();

// A specific post, its title and a custom field
$post = Post::find(31);
echo $post->title;      // alias of post_title
echo $post->meta->link; // value of the "link" post meta

// config/database.php

'connections' => [

    'mysql' => [ // Laravel database
        'driver'    => 'mysql',
        'host'      => 'localhost',
        'database'  => 'mydatabase',
        'username'  => 'admin',
        'password'  => 'secret',
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
        'strict'    => false,
        'engine'    => null,
    ],

    'wordpress' => [ // WordPress database, used by Colt
        'driver'    => 'mysql',
        'host'      => 'localhost',
        'database'  => 'mydatabase',
        'username'  => 'admin',
        'password'  => 'secret',
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => 'wp_',
        'strict'    => false,
        'engine'    => null,
    ],
],

'connection' => 'wordpress',



Pollora\Colt\Database::connect([
    'database' => 'database_name',
    'username' => 'username',
    'password' => 'pa$$word',
    'prefix'   => 'wp_', // default is 'wp_'
]);

'driver'    => 'mysql',
'host'      => 'localhost',
'charset'   => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix'    => 'wp_',

// All published posts
$posts = Post::published()->get();
$posts = Post::status('publish')->get();

// A specific post
$post = Post::find(31);
echo $post->post_title;

namespace App\Models;

use Pollora\Colt\Model\Post as ColtPost;

class Post extends ColtPost
{
    protected $connection = 'foo-bar';

    public function customMethod()
    {
        //
    }
}

$posts = App\Models\Post::all(); // uses the 'foo-bar' connection

$post = Post::find(31);
echo $post->meta->link; // or
echo $post->fields->link; // or
echo $post->link;

$post = Post::find(1);
$post->saveMeta('username', 'jgrossi');

// Several at once
$post->saveMeta([
    'username' => 'jgrossi',
    'url' => 'http://jgrossi.com',
]);

$post = Post::find(1);
$postMeta = $post->createMeta('foo', 'bar'); // PostMeta instance
$trueOrFalse = $post->saveMeta('foo', 'baz'); // bool

// A published post that has a meta key
$post = Post::published()->hasMeta('featured_article')->first();

// Matching both key and value
$post = Post::published()->hasMeta('username', 'jgrossi')->first();

// Several fields, or just several keys
$post = Post::hasMeta(['username' => 'jgrossi'])->first();
$post = Post::hasMeta(['username' => 'jgrossi', 'url' => 'jgrossi.com'])->first();
$post = Post::hasMeta(['username', 'url'])->first();

// Matches 'J Grossi', 'J GROSSI' and 'j grossi'
$post = Post::published()->hasMetaLike('author', 'J GROSSI')->first();

// Also matches 'Junior Grossi'
$post = Post::published()->hasMetaLike('author', 'J%GROSSI')->first();

$post = Post::find(1);
$post->title === $post->post_title; // true

class A extends \Pollora\Colt\Model\Post
{
    protected static $aliases = [
        'foo' => 'post_foo',
    ];
}

$a = A::find(1);
echo $a->foo;
echo $a->title; // from Post

$newest = Post::newest()->first();
$oldest = Post::oldest()->first();

$posts = Post::published()->paginate(5);

// With type()
$videos = Post::type('video')->status('publish')->get();

// With your own class
class Video extends \Pollora\Colt\Model\Post
{
    protected $postType = 'video';
}

$videos = Video::status('publish')->get();

$stores = Post::type('store')->status('publish')->take(3)->get();

foreach ($stores as $store) {
    $storeAddress = $store->address; // or $store->meta->address, or $store->fields->address
}

'post_types' => [
    'video' => App\Models\Video::class,
    'foo' => App\Models\Foo::class,
],

Post::registerPostType('video', App\Models\Video::class);

// Every item is now a Video instance
$videos = Post::type('video')->status('publish')->get();

use Pollora\Colt\Model\Page;

$page = Page::slug('about')->first(); // or
$page = Post::type('page')->slug('about')->first();
echo $page->post_title;

// Taxonomies of a post
$post = Post::find(1);
$taxonomy = $post->taxonomies()->first();
echo $taxonomy->taxonomy;

// Posts in a term
$post = Post::taxonomy('category', 'php')->first();

// A category and its posts
$category = Taxonomy::category()->slug('uncategorized')->first();
$category->posts->each(function ($post) {
    echo $post->post_title;
});

// All categories with their posts
$categories = Taxonomy::where('taxonomy', 'category')->with('posts')->get();

echo $post->getFormat(); // 'video', etc.

$page = Page::slug('about')->with('attachment')->first();
print_r($page->attachment); // featured image

$post = Post::slug('test')->with('revision')->first();
print_r($post->revision); // all revisions

$post = Post::find(1);

// A Pollora\Colt\Model\Meta\ThumbnailMeta instance
print_r($post->thumbnail);

// Cast to string, it is the URL of the original image
echo $post->thumbnail;

if ($post->thumbnail !== null) {
    /**
     * [
     *     'file' => 'filename-300x300.jpg',
     *     'width' => 300,
     *     'height' => 300,
     *     'mime-type' => 'image/jpeg',
     *     'url' => 'http://localhost/wp-content/uploads/filename-300x300.jpg',
     * ]
     */
    print_r($post->thumbnail->size(Pollora\Colt\Model\Meta\ThumbnailMeta::SIZE_THUMBNAIL));

    // http://localhost/wp-content/uploads/filename.jpg
    print_r($post->thumbnail->size('invalid_size'));
}

'shortcodes' => [
    'foo' => App\Shortcodes\FooShortcode::class,
    'bar' => App\Shortcodes\BarShortcode::class,
],

use Thunder\Shortcode\Shortcode\ShortcodeInterface;

class FooShortcode implements \Pollora\Colt\Shortcode
{
    public function render(ShortcodeInterface $shortcode)
    {
        return sprintf(
            'html-for-shortcode-%s-%s',
            $shortcode->getName(),
            $shortcode->getParameter('one')
        );
    }
}

// [gallery id="1"]
Post::addShortcode('gallery', function ($shortcode) {
    return $shortcode->getName() . '.' . $shortcode->getParameter('id');
});

$post = Post::find(1);
echo $post->content;

'shortcode_parser' => Thunder\Shortcode\Parser\RegularParser::class,
// 'shortcode_parser' => Thunder\Shortcode\Parser\WordpressParser::class,

$post->setShortcodeParser(new WordpressParser());
echo $post->content; // parsed with WordpressParser

$siteUrl = Option::get('siteurl');

Option::add('foo', 'bar'); // stored as a string
Option::add('baz', ['one' => 'two']); // serialized

$options = Option::asArray();
echo $options['siteurl'];

$options = Option::asArray(['siteurl', 'home', 'blogname']);
echo $options['home'];

$menu = Menu::slug('primary')->first();

foreach ($menu->items as $item) {
    echo $item->instance()->title;     // a Post
    echo $item->instance()->name;      // a Term
    echo $item->instance()->link_text; // a custom link
}

$items = Menu::slug('foo')->first()->items;
$parent = $items->first()->parent();

$users = User::get();

$user = User::find(1);
echo $user->user_login;

'providers' => [
    'users' => [
        'driver' => 'colt',
        'model'  => Pollora\Colt\Model\User::class,
    ],
],

Auth::validate([
    'email' => '[email protected]', // or 'username'
    'password' => 'secret',
]);

use Pollora\Colt\Laravel\Auth\ResetsPasswords as ColtResetsPasswords;

class ResetPasswordController extends Controller
{
    use ResetsPasswords, ColtResetsPasswords {
        ColtResetsPasswords::resetPassword insteadof ResetsPasswords;
    }
}

$userProvider = new Pollora\Colt\Laravel\Auth\AuthUserProvider;
$user = $userProvider->retrieveByCredentials(['username' => 'admin']);

if (! is_null($user) && $userProvider->validateCredentials($user, ['password' => 'admin'])) {
    // logged in
}
bash
php artisan vendor:publish --provider="Pollora\Colt\Laravel\ColtServiceProvider"