PHP code example of mehrshaddarzi / wp-trait

1. Go to this page and download the library: Download mehrshaddarzi/wp-trait 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/ */

    

mehrshaddarzi / wp-trait example snippets


/**
 * Plugin Name:       My Basics Plugin
 * Plugin URI:        https://example.com/plugins/the-basics/
 * Description:       Handle the basics with this plugin.
 * Version:           1.10.3
 * Requires at least: 5.2
 * Requires PHP:      7.2
 * Author:            John Smith
 * Author URI:        https://author.example.com/
 * License:           GPL v2 or later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Update URI:        https://example.com/my-plugin/
 * Text Domain:       my-basics-plugin
 * Domain Path:       /languages
 */

# Load Package

namespace WP_User_Mobile;

use WPTrait\Hook\Notice;
use WPTrait\Model;

class Admin extends Model
{
    use Notice;

    public function __construct($plugin)
    {
        parent::__construct($plugin);
    }

    public function admin_notices()
    {
        $text = __('This Notice is a example from your plugin', $this->plugin->textDomain);
        echo $this->add_alert($text, 'info');
    }
    
    public function method_name()
    {
        return 'Code is Poetry';
    }
}

public function instantiate()
{
    $this->Admin = new \WP_User_Mobile\Admin($this->plugin);
}

echo wp_user_mobile()->Admin->method_name();

gloabl $wp_user_mobile;
echo $wp_user_mobile->Admin->method_name();

new WP_User_Mobile('wp-user-mobile', ['global' => 'my_global']);

echo my_global()->Admin->method_name();

new WP_User_Mobile('wp-user-mobile', ['global' => null]);

$default = [
   'main_file' => '',
   'global' => null,
   'prefix' => null,
   'when_load' => ['action' => 'plugins_loaded', 'priority' => 10]
];

use WPTrait\Model;

class Post extends Model
{
    public $actions = [
        'init' => 'init_check_user',
        'save_post' => ['save_post_view', 10, 3],
        'pre_get_posts' => 'custom_query_action',
        'admin_notices' => [
            ['save_user_address', 12, 1],
            ['disable_plugin_option', 10, 1]
        ]
    ];

    public $filters = [
        'the_content' => 'add_custom_text',
        'show_admin_bar' => '__return_false',
        'rest_enabled' => false,
        'pre_http_request' => ['disable_custom_api', 10, 3]
    ];

    public function add_custom_text($content)
    {
        return $content . 'My Text';
    }

    public function save_post_view($post_ID, $post, $update)
    {
        if (!$update) {
            $this->post($post_ID)->meta->save('views', 1);
        }
    }
    
    public function disable_custom_api($preempt, $parsed_args, $url) {
        if( strpos($url, 'https://any_domain.com') !==false ){
            return new \WP_Error( 'http_request_block', "This request is not allowed" );
        }
        return $preempt;
    }
}

// Get List of students from custom WordPress table
$lists = $this->db->get_results("SELECT ID, first_name FROM {$this->db->prefix}students ORDER BY ID");
foreach ($lists as $student) {
    echo $student->ID;
}

// Insert new item in Database
$this->db->insert($this->db->prefix.'students', [
'name' => 'Mehrshad',
'family' => Darzi
]);
echo $this->db->insert_id;

// Get Plugin Base Url
$this->plugin->url

// Get Plugin Base Path
$this->plugin->path

// Get Plugin TextDomain
$this->plugin->textDomain

// Get Plugin Main PHP File path
$this->plugin->mainFile

// Get Plugin Name
$this->plugin->name

// Get Plugin version
$this->plugin->version

// Get Plugin description
$this->plugin->description

// Get Plugin author name
$this->plugin->author

// Get Plugin Minimum hp
$this->plugin->path('templates/email.php')

// Get All plugins data as Object
$this->plugin->data

// Get ABSPATH (WordPress Root Directory)
$this->constant->root

// Get WP-Content Dir Path
$this->constant->content_dir

// Get WP-Content Dir Url
$this->constant->content_url

// Get Plugins Dir Path
$this->constant->plugin_dir

// Get Plugins Dir Url
$this->constant->plugin_url

// Get Uploads Directory in WordPress
// Object {basedir|baseurl|subdir|path|url}
$this->constant->uploads

// Get Base Uploads dir path
$this->constant->uploads->basedir;

// Get Base Uploads dir url
$this->constant->uploads->baseurl;

// Get Active Theme path
$this->constant->template_path;

// Get Active Theme url
$this->constant->template_url;

// Get themes dir path
$this->constant->theme_root;

// Get themes directory url
$this->constant->theme_root_url;

// Get Mu-Plugins dir path
$this->constant->mu_plugin_dir;

// Get Mu-Plugins dir url
$this->constant->mu_plugin_url;

// Check WP_DEBUG is true (boolean)
$this->constant->debug

// Get WordPress environment type
$this->constant->environment

// Check SCRIPT_DEBUG is true (boolean)
$this->constant->script_debug

// Check WP_CACHE is true (boolean)
$this->constant->cache

// Get Time in Seconds
$this->constant->minute
$this->constant->hour
$this->constant->day 
$this->constant->week
$this->constant->month
$this->constant->year

// WordPress Current global $post data
$this->global->post

// WordPress Current global $wp_query data
$this->global->query

// WordPress Current Version
$this->global->version

// WordPress Current db Version
$this->global->db_version

// WordPress WP Request Object
$this->global->wp

// WordPress Rewrite Object Request
$this->global->rewrite

// WordPress User Roles list
$this->global->roles

// WordPress Locale
$this->global->locale

// WordPress AdminBar Object
$this->global->admin_bar

// WordPress Current Admin Page Now
$this->global->page_now

// Get Current Admin Screen detail
$this->global->screen

// Get List Of Admin Menu
$this->global->menu

// Get List Of Submenus in Admin
$this->global->submenu

// Get List Of registered Sidebars
$this->global->sidebars

// Get List of Registered Meta Boxes by current Screen
$this->global->meta_boxes

// WordPress Home Url
$this->url->home

// WordPress Site Url
$this->url->site

// WordPress Site Url with Custom Path and queries
// Site.Com/blog?filter=category&ids=3,4
$this->url->get('/blog', ['filter' => 'category', 'ids' => '3,4'])

// WordPress Admin Url
// Site.com/wp-admin/users.php?sort=ID
$this->url->admin('users.php', ['sort' => 'ID'])

// WordPress Admin Ajax Url
// Site.com/wp-admin/admin-ajax.php?action=new_user&cache=no
$this->url->ajax('new_user', ['cache' => 'no'])

// WordPress REST API Url
// Site.com/wp-json/wp/v2/search?filter=name&s=Mehrshad
$this->url->rest('wp/v2/search', ['filter' => 'name', 's' => 'Mehrshad'])

// WordPress REST API Prefix
// Default is: wp-json
$this->url->restPrefix()

// WordPress CronJob Url
// Site.com/wp-cron.php?doing_wp_cron
$this->url->cron()

// Generate Url
// https://site.com?query=value
$this->url->generate('https://site.com', ['query' => 'value']);

// Parse URl
// @see https://www.php.net/manual/en/function.parse-url.php
$this->url->parse('https://site.com?query=value');

// Sanitize Url
$this->url->sanitize('https://site.com?query=value');

// Validate Url
$this->url->isValidate('https://site.com<script>alert("xss")<script>?query=value');

// Escape Url
$this->url->esc('https://site.com?query=value');

// Get Current User ID
// You Can Access All Object From WP_User Class
$this->user->id;

// Get Current User Email
$this->user->email;

// Get Current User Role
$this->user->roles;

// Get All User Meta
// Check Meta Collection Class
$this->user->meta->all();

// Get Post
$this->post(1)->get();

// Get Post Meta
$this->post(1)->meta->all();

// Get Custom Meta
$this->post(1)->meta->get('key');

// Get Multiple Custom Meta Keys
$this->post(1)->meta->only(['key_1', 'key_2']);

// Save Post Meta
$this->post(1)->meta->save('key', 'value');

// Delete Post
$this->post(1)->delete();

// Get List Of post
$this->post->list(['type' => 'post', 'status' => 'publish', 'cache' => false]);

// Get Only SQL Query
$this->post->toSql([
    'type' => 'post',
    'status' => 'publish',
    'meta' => [
        'key' => 'is_active',
        'value' => 'yes',
        'compare' => '='
    ]
]);

// Get Post Thumbnail
$this->post(1)->thumbnail()->url

// Add Post
$insert_post = $this->post->add(['title' => '', 'content' => '']);
if($this->error->has($insert_post)){
    echo $this->error->message($insert_post);
}

// Edit Post
$this->post(38)->update(['title' => '']);

// Permalink
$this->post(1)->permalink();

// Check Exist
$this->post(53)->exists();

// Post Terms
$this->post(1)->terms('category');

// Post Comments
$this->post(1)->comments();

// Collection { Post + Meta + Terms }
$this->post(1)->collection(['meta_1', 'meta_2'], ['category', 'post_tag']);



// Get Attachment
$attachment = $this->attachment(1)->get();

// Get Meta
$this->attachment(1)->meta->all();

// Delete Attachment
$this->attachment(1)->delete();

// Get Url
$this->attachment(1)->url();

// Get Image Src in Custom image size
$this->attachment(1)->src('thumbnail');

// Get Attachment File Path
$this->attachment(1)->path();

// Get Attachment Meta Data
$this->attachment(1)->metadata();

// Auto Upload File in WordPress Library
$attachment_id = $this->attachment->upload('image'); // <input type="file" name="image" />

// Regenerate Attachment image Size
$this->attachment(1)->generate_thumbnail();

// Get List Of WordPres Image Sizes
$this->attachment->get_wordpress_image_sizes();

// Get Uploads Dir
$this->attachment->upload_dir();

// Check Attachment type File (image or video or audio or other)
$this->attachment(1)->is('image');

// Get Size Of Attachment
$this->attachment(1)->size();

// Get User
$user = $this->user(1)->get();
/**
* List of object return:
* 
* $user->ID
* $user->user_login
* $user->user_pass
* $user->user_nicename
* $user->user_email
* $user->user_url
* $user->user_registered
* $user->user_activation_key
* $user->user_status
* $user->display_name
* $user->first_name
* $user->last_name
* $user->caps
* $user->roles
* $user->allcaps
*/

// Get All Meta
$this->user(1)->meta->all();

// Get Custom Meta
$this->user(1)->meta->get('meta_name');

// Save Meta
$this->user(1)->meta->update('phone', '09xxxxxxxx');

// Delete User
$this->user(1)->delete();

// Update User
$this->user(1)->update(['name' => 'Mehrshad Darzi', 'password' => '12345']);

// Add User
$this->user->add(['email' => '[email protected]', 'username' => 'mehrshad']);

// Get Current User
$this->user->current();

// Check User is Login
$this->user->auth();

// Get current User id
$this->user->id();

// Check User Has Role
$this->user->has_role('administrator');

// Check User Has Capability
$this->user(1)->can('manage_options');

// Check Exist User Id
$this->user->exists(12);

// Login User
$this->user->login($username, $password, $remember = true);

// Authenticate User [Useful for REST-API or Ajax Without set any Cookie]
$this->user->authenticate($username, $password);

// Set New Password For User
$this->user(1)->password->set('new_password');

// Check User Password
$this->user(1)->password->check($this->request->input('password', 'trim'), $hash);

// Convert PlainText Password To Hash
$this->user->password->hash('123456');

// Generate Password With custom length
$this->user->password->generate(8, $special_chars = false);

// Set Role and Capability for User
$user = $this->user(1)->get();
$user->set_role('author');
$user->add_cap('cap_name');
$user->remove_cap('cap_name');
$user->add_role('role_name');
$user->remove_role('role_name');
$user->remove_all_caps();

// Get Term
$this->term(1)->get();

// Get Meta
$this->term(1)->meta->all();

// Save Meta
$this->term(1)->meta->update('key', 'value');

// Delete Term
$this->term(1)->delete();

// Update Term
$this->term(1)->update(['name' => 'New name']);

// Add Term
$this->term->add('term name', ['parent' => 4, 'description' => ''], 'post_tag');

// Get List Terms
$this->term->list(['taxonomy' => 'product_cat', 'return' => 'id']);

// Get All Taxonomies in WordPress
$this->terms->get_taxonomies();

// Get Option
$this->option('name')->get();

// Get default Value if Not Found
$this->option('name')->get($default);

// Get Nested Array Option Value With dot
$this->option('settings.user.id')->get();

// Save Option
$this->option('name')->save('value');

// Delete Options
$this->option('name')->delete();

// Add Option
$this->option->add('name', 'value', 'no');

// Get Comment
$this->comment(1)->get();

// Get Meta
$this->comment(1)->meta->all();

// Save Meta
$this->comment(1)->meta->update('key', 'value');

// Delete Meta
$this->comment(1)->meta->delete('key');

// Delete Comment
$this->comment(1)->delete();

// Update Comment
$this->comment(1)->update(['name' => 'Ali', 'approved' => true]);

// Add Comment
$this->comment->add(['post_id' => 1, 'name' => 'Mehrshad Darzi', 'content' => '']);

// Get List Comments
$this->comment->list(['post_id' => 1, 'nested' => true]);

// Get All Meta From Object
$this->post(1)->meta->all();

// Get Custom Meta
$this->user(1)->meta->get('first_name');

// Get Multiple Custom Meta Keys
$this->post(1)->meta->only(['key_1', 'key_2']);

// Get All Meta Key Except Custom keys
$this->post(1)->meta->except(['_edit_lock', '_edit_last']);

// Delete Meta
$this->user(1)->meta->delete('mobile');

// Save Meta
$this->term(1)->meta->save('key', 'value');

// Remove all Meta from Object
$this->comment(1)->meta->clean();

// Get Request fields { GET + POST }
$this->request->input('first_name');

// only `GET` fields
$this->request->query('email');

// Get Field with Custom filter e.g. trim value
$this->request->input('name', 'trim');

// Get field with multiple filter
$this->request->input('post_excerpt', ['trim', 'strip_tags']);

// Check Has exists input
$this->request->has('first_name');

// Check Exist and Not Empty fields
$this->request->filled('first_name');

// Check Exist and is Numeric value
$this->request->numeric('year');

// Check exists fields and Equal with value 
$this->request->equal('first_name', 'mehrshad');

// Check is Numeric value and is positive Number (x >0)
$this->request->numeric('age', true);

// Check value is Enum list
$this->request->enum('post_status', ['publish', 'draft']);

// Get Custom Fields From Request
$this->request->only(['email', 'last_name']);

// Redirect in WordPress
$this->request->redirect('https://google.com', 302);

// Get $_FILES by id
// From Html Form Input: <input type="file" name="image" />
$this->request->file('image'); 

// Check Exists File
$this->request->hasFile('image');

// Get Cookie
$this->request->cookie('name');

// Get $_SERVER params
$this->request->server('REQUEST_URI');

// Check is REST API request
$this->request->is_rest();

// Check is Ajax Request
$this->request->is_ajax();

// Check is CronJob Request
$this->request->is_cron();

// Check is XML-RPC Request
$this->request->is_xmlrpc();

// Check is WP-CLI Request
$this->request->is_cli();

// Get Method Of Request
$this->request->get_method();

// Check Method Of Request {boolean}
$this->request->is_method('PUT');

// Return Json Response
$this->response->json(['data' => 'value'], 200);

$input_email = $this->request->input('email');

// Define new error Handle system
$error = $this->error->new();

if(empty($input_email)) {
    $error->add('empty_email', __('Please Fill Your Email', 'my-plugin'));
}

if(!is_email($input_email)){
    $error->add('valid_email', __('Please Fill valid Email', 'my-plugin'));
}

if($this->error->has($error)){
    return $error; # Or use $error->get_error_messages();
} else {
    return true;
}

// Remember Cache last Post in One Hour
$this->cache->remember('latest_post', function(){
    return $this->post->list(['type' => 'product', 'return' => 'id'])
}, 'cache_group_name', $this->constant->hour);

// Delete Cache
$this->cache->delete('cache_name', 'group');

// Add Cache
$this->cache->add('cache_name', $value, 'group_name', 5 * $this->constant->minute);

// Get Cache
$this->cache->get('name', 'group');

// Remember Transient
$this->transient->remember('latest_users', function(){
    return $this->user->list(['role' => 'subscriber', 'return' => 'id'])
}, $this->constant->hour);

// Delete transient
$this->transient->delete('name');

// Add Transient
$this->transient->add('name', $value, $this->constant->day);

// Get Transient
$this->transient->get('name');

// Get REST API prefix url
$this->rest->prefix();

// get REST API url
$this->rest->url('namespace/endpoint');

// Making WordPress REST API Calls Internally
$this->rest->request('GET', 'wp/v2/posts', [ 'per_page' => 12 ]);

// Define New Custom EndPoint in WordPress REST API
$this->route->add('form', 'contact', [
    'method' => 'post',
    'function' => 'send_form',
    'arg' => [
        'title' => [
            'e' => function ($param, $request, $key) {
                        return is_numeric($param);
                    }
                ],
                'name' => [
                    'Register', 'id' => $this->db->insert_id],
            200,
            ['X-Custom-Header' => 'value']
        );
    }
}

// Remove Route
$this->route->remove('/wp/v2/posts');

// Get List WordPress REST API routes
$list = $this->route->all();
$list->namespaces;
$list->routes;

// set new Cookie for One Hour
$this->cookie->set('user_data', ['name' => 'Mehrshad', 'family' => 'Darzi'], $this->constant->hour);

// Check exist cookie {boolean}
$this->cookie->has('user_data');

// Get cookie Value { auto convert json to Array }
$this->cookie->get('user_data');

// Remove Cookie
$this->cookie->delete('user_data');

// Get All Cookie in WordPress Site
$this->cookie->all();

// set new session
$this->session->set('redirect_from', add_query_arg( 'type', 'error', $this->constant->home ));

// Check exist session {boolean}
$this->session->has('redirect_from');

// Get session Value
$this->session->get('redirect_from');

// Remove Session
$this->session->delete('redirect_from');

// Get All Session in WordPress Site
$this->session->all();

// Get Session ID
$this->session->id();

// Destroy All Sessions
$this->session->destroy();

add_action('init', 'register_session');
public function register_session()
{
    if (session_status() == PHP_SESSION_NONE) {
        session_start([
            'read_and_close' => true
        ]);
    }
}

// Define single Event
$this->event->single($this->constant->week, 'action_name');

// Define recurring Event
$this->event->add(time(), 'hourly', 'action_name', []);

// Delete Event
$this->event->delete('action_name');

// Retrieve supported event recurrence schedules
$this->event->schedules();

// Get List Current CrobJobs
$this->event->list();

// Create new Nonce
$this->nonce->create('_my_nonce');

// Verify Nonce Field {boolean}
$this->nonce->verify('input_name', '_my_nonce');

// Generate Html Input Hidden nonce Field for Using form
$this->nonce->input('_my_nonce', 'nonce_input_name');

// Get Example file Path
$path = path_join($this->constant->content_dir, 'object.php');

// Check file exits
$this->file($path)->exists();

// Check file missing
$this->file($path)->missing();

// Get File Content
$this->file($path)->get();

// Delete File
$this->file($path)->delete();

// Create File with Content
$content = ' echo "Hi"; 

// Send Html Body Mail
$this->email('[email protected]')->send('Subject', '<p>Message Body</p>');

// Send to Multiple Email With Custom Header and Attachment
$headers = [
    'Content-Type: text/html; charset=UTF-8',
    'From: Site name <[email protected]>'
];
$attachment = [$this->constant->uploads->basedir.'/file.zip'];
$this->email(['[email protected]', '[email protected]'])
     ->send('Subject', 'Message Body', $headers, $attachment);

// Add text log
# wp-content/debug.log
$this->log('text log', 'debug');

// Add Array log
$this->log(['user_id' => 1, 'status' => true], 'debug');

// Custom Log File
# wp-content/db.log
$this->log('text log', 'db');

// Custom Condition
# By Default when WP_DEBUG_LOG === true
# wp-content/plugin-slug.log
$is_active_plugin_log = get_option('my_plugin_active_log');
$this->log('text log', 'plugin-slug', $is_active_plugin_log);

// Change Datetime in Log File
# By default the dates are saved in the log file based on `UTC`
add_filter('wp_trait_log_date', function ($date, $type) {
    if ($type == "my-plugin-slug") {
        return date_i18n(get_option( 'date_format' ), current_time('timestamp')) . ' UTC+3.5';
    }
    return $date;
});

# ~/wp-content/plugins/{plugin-slug}/templates/students/table.php
<p><?= $title; 

$data = [
    'title' => 'Students List',
    'students' => [
        ['name' => 'Mehrshad', 'family' => 'Darzi'],
        ['name' => 'John', 'family' => 'Smith'],
    ]
];

echo $this->view->render('students.table', $data);

# ~/wp-content/themes/twentyeleven/{plugin-slug}/students/table.php
<div class="text-right bg-black text-white"><?= $title; 

$this->view->render('students.table', $data, [], $canOverride = false);

// First Way
$content = $this->view()
  ->attribute('text', $text)
  ->attribute([
      'description' => __('This is the description', $this->plugin->textDomain)
  ])
  ->render('notice');
echo $this->add_alert($content, 'info');

// Second Way
$content = $this->view()->render('notice', [
  'text' => $text
], [
  'text' => __('This is the description', $this->plugin->textDomain)
]);
echo $this->add_alert($content, 'info');

// Third Way
$content = $this->view();
$content->text = $text;
echo $this->add_alert($content('notice'), 'info');

// Property Way
$view = $this->view->render('notice', [
  'text' => $text . ' with property way'
]);
echo $this->add_alert($view, 'info');

use Init;

public function init(){
  // Code Here
}

public function init_check_user_login(){
  // Code Here
}

public function init_save_form_data() {
  // Code Here
}

use WPTrait\Hook\Ajax;

class Admin extends Model
{
    use Ajax;

    public $ajax = [
        'methods' => ['signup_user']
    ];

    public function admin_ajax_signup_user()
    {
        # Check User is Auth
        if ($this->user->auth()) {
            $this->response->json(['message' => __('You are a user of the site', 'wp-plugin')], 400);
        }

        # Get Input Email
        $email = $this->request->input('email');

        # Create User
        $user_id = $this->user->add([
            'email' => $email,
            'username' => $email
        ]);
        if ($this->error->has($user_id)) {
            $this->response->json(['message' => $this->error->message($user_id)], 400);
        }

        # Return Success
        $this->response->json(['user_id' => $user_id], 200);
    }
}

// Class which uses singleton trait.
class MyClass {
    use Singleton;
}

// To get the instance of the class.
$instance = MyClass::instance();

http://site.com/wp-admin/admin-ajax.php?action=signup_user&[email protected]