Nasrul Hazim Bin Mohamad

Training: Web Development with Laravel at ejoeSolutions

Feb
29

Registration Open Now! Please register at http://bit.ly/laravel-at-ejoesolutions

Time: 9:00 AM to 4:00 PM

Date: 11th & 12th March 2016

Location: ejoeSolutions, Kota Bharu, Kelantan.

Cleanique Coders Resources (002507773-X)

Cleanique Coders Resources, it’s founder Nasrul Hazim (blog.nasrulhazim.com) is a company focusing on software development, solving developers problems.

Cleanique Coders also expanding it’s expertise on software developments skills training courses for wide range – either from education, private or public sectors.

Cleanique Coders also provide personal coaching and group coaching.

Course Details

Date: 5th & 6th March 2016

Location: ejoeSolutions, Kota Bharu, Kelantan – https://goo.gl/maps/JV24qF2UMWz

Fee: RM 300

Syllabus Details

Setup

The Basic
– Routing
– Middleware
– Controllers
– Requests
– Responses
– Views
– Blade Templates

Database
– Migrations
– Seeding

Eloquent ORM
– CRUD
– Relationship

Services
– Authentication
– Authorization

 

WordPress: Handling Plugin Activation

Feb
27

A simple way to handle WordPress plugin activation.


function activate_background_slideshow() {
	// do something during activation
	$plugin_data = get_plugin_data(WP_PLUGIN_DIR . '/background-slideshow/background-slideshow.php',false,false);
    
    // WordPress Version
    $version = $plugin_data['Version'];
    if ( version_compare( get_bloginfo( 'version' ), '3.1', '<' ) )
    {
      wp_die("You must use at least WordPress 4.0 to use this plugin!");
    }

    // Plugin Version
    if ( get_option( 'dk_op_version' ) === false )
    {
      // initial state
      add_option( 'dk_op_version', $version );
    }
    else if(get_option( 'dk_op_version') < $version)
    {
      update_option( 'dk_op_version', $version );
      // do update based on version
    }

    // Database version
    if( get_option('dk_op_db') === false ) {
      add_option('dk_op_db', $version);
     
    } else if( get_option('dk_op_db') != $version ) {
      // add more upgrade database if needed
      update_option( 'dk_op_db', $version );
    }
}

WordPress: ORM Model Class

Feb
17

Object Relational Mapping or ORM is something that WordPress is missing, but that doesn’t mean we can’t create an abstract Model that act similar to ORM behavior.

Here are the first version to implement the ORM in WordPres – far from completions.

<?php 

abstract class Model {

	static $table_name = '';

	public static function table() {
		global $wpdb;

		return $wpdb->prefix.static::$table_name;
	}
	
	public static function all($conditions = '') {

		if(!empty(self::table())) {
			global $wpdb;

			$sql = "SELECT * FROM ".self::table() . $conditions;

			return $wpdb->get_results($sql);
		}

		return false;
	}

	public function row($conditions = '') {

		if(!empty(self::table())) {
			global $wpdb;

			$sql = "SELECT * FROM ".self::table() . $conditions;

			return $wpdb->get_row($sql);
		}

		return false;
	}

	public function findById($id) {
		global $wpdb;

		$sql = "SELECT * FROM ".self::table() . " WHERE id = '".$id."'";

		return $wpdb->get_row($sql);
	}

	public static function find($type, $parameters) {
		if(empty($type)) {
			$type = 'all';
		}

		$conditions = isset($parameters['conditions']) ? (' WHERE ' . join(' AND ',$parameters['conditions'])) : null;

		if($type == 'all') {
			return self::all($conditions);
		} else if($type == 'row') {
			return self::row($conditions);
		}

		return false;
	}

	public static function create($data) {
		global $wpdb;

		if($wpdb->insert(self::table(),$data)) {
			$id = $wpdb->insert_id;

			$data = self::findById($id);

			return $data;	
		}

		return false;		
	}

	public static function update($data, $where, $format = null, $where_format = null) {
		global $wpdb;

		return $wpdb->update( self::table(), $data, $where, $format, $where_format );
	}

	public static function delete($data) {
		global $wpdb;

		return $wpdb->delete( self::table(), $data );	
	}
}

Here are the implementation:

<?php

// make sure to include the abstract class first before extending the Model Class

class State extends Model {
	static $table_name = 'states';
}

The usage:

<?php

// include the State class in your plugin first

$states = State::all(); // fetch all available states
$state = State::findById(1); // find state with id = 1
$deleted = State::delete(['id' => 1]); // delete state record with id = 1

More features to add in – to have columns properties, what to hide, what to show, column alias and many more. But this is my first attempt to make the WordPress Plugin Development much easier (an attempt to replicate any famous PHP framework such Laravel, CakePHP and so on. Prefer to have something like Eloquent ORM)