Nasrul Hazim Bin Mohamad

WordPress: Message Class

Sep
20

A simple class for displaying admin messages – error, notice and successful.


class Message {
	public static function error($message) {
		return '<div class="error"><p>'.$message.'</p></div>';
	}

	public static function success($message) {
		return '<div class="updated"><p>'.$message.'</p></div>';
	}

	public static function notice($message) {
		return '<div class="update-nag">'.$message.'</div>';
	}
}

/** 
 * Usage
 **/
Message::error('Hutang 2.6Bil! Bayar!');
Message::notice('Kacing! (PM: eh..cam bunyi duit masuk je');
Message::success('PM: Hasil derma orang bagi. Syukur lahhhh..boleh byr hutang');
wp-admin-message-error

wp-admin-message-error

wp-admin-message-notice

wp-admin-message-notice

wp-admin-message-success

wp-admin-message-success

How to Include PHP ActiveRecord in WordPress as Global Feature

Sep
12

This post for developers who used to ORM/ActiveRecord and not familiar with WordPress Query (like me) and want the ORM/ActiveRecord to be available in WordPress.

As per post’s title, this post regarding how you can include PHP ActiveRecord for global use – let say you have multiple plugins that use PHP ActiveRecord, you can’t load same PHP ActiveRecord classes / redeclared classed in an application or system – to be precise in PHP.

So the solution is to add in PHP ActiveRecord in {wordpress_installation}/wp-includes

Configurations

  1. Download PHP ActiveRecord and extract to {wordpress_installation}/wp-includes
  2. Create a new file in {wordpress_installation}/wp-includes/php-activerecord/ called AR.php and use the codes below
    <?php
    
    class AR {
    	static $conn;
    	static $model_directories;
    
    	public static function add_model_directories($list) {
    		if(!is_array($list)) {
    			return false;
    		}
    
    		if(!is_array(self::$model_directories)) {
    			self::$model_directories = array();
    		}
    
    		foreach ($list as $key => $value) {
    			if(!in_array($value, self::$model_directories)) {
    				self::$model_directories[] = $value;
    			}
    		}
    
    		return true;
    	}
    }
    
    function ar_init() {
    	$connections = array(
    		'development' => 'mysql://'.DB_USER.':'.DB_PASSWORD.'@'.DB_HOST.'/'.DB_NAME,
    	);
    
    	AR::$conn = ActiveRecord\Config::instance();
    	AR::$conn->set_model_directory(AR::$model_directories);
    	AR::$conn->set_connections($connections);
    }
    
  3. Update ActiveRecord\Config class in php-activerecord/lib/Config.php for method set_model_directory and get_model_directory. This to ensure we can include multiple model directories from multiple plugins.:
    	public function set_model_directory($dir)
    	{
    		if( is_array($dir) ) {
    			foreach( $dir as $k => $path ) {
    				if( !file_exists($path ) ) {
    					throw new ConfigException('Invalid or non-existent model directory: '.$path);
    				}
    			}
    		} else if( $dir  && !file_exists( $dir) ) {
    			throw new ConfigException('Invalid or non-existent model directory: '. $dir);
    		}
    
    		$this->model_directory = $dir;
    	}
    
    	public function get_model_directory()
    	{
    		return $this->model_directory;
    	}
    
  4. Include ActiveRecord.php and AR.php in {wordpress_installation}/wp-settings.php after Line 155:
    require( ABSPATH . WPINC . '/php-activerecord/ActiveRecord.php' );
    require( ABSPATH . WPINC . '/php-activerecord/AR.php' );
    
  5. Add the following codes after Line 241 to initialize PHP ActiveRecord:
    /**
     * Initialize PHP ActiveRecord
     */
    ar_init();
    

How to Use in Your Plugin

  1. Create your WordPress Plugin (you can use wppb.me)
  2. Open up your WordPress Plugin main file and add in the following code in order to include your model directory in AR class:
    AR::add_model_directories(array(
    	PLUGIN_PATH . 'includes' . DIRECTORY_SEPARATOR . 'models'
    ));
    
  3. Now create a sample model file which extend ActiveRecord\Model in your plugin’s model directory. Following are the sample codes:
    <?php
    
    class GTState extends ActiveRecord\Model {
    	static $table_name = 'gt_states';
    }
    
  4. Now you’re ready to use the GTState class in your view / controller.

I hope this trick will speed up the development progress.

Sorry WordPress Developers, I need a little hack to develop fast with PHP ActiveRecord, because I’m a beginner with WordPress, more things to learn.

References

  1. Load Multiple Model Directories
  2. PHP ActiveRecord Wiki

Git: Rename Tag

Sep
03

Git, rename your tag.

git tag new old
git tag -d old
git push origin :refs/tags/old
git push --tags

Reference: How do you rename a Git tag?

7z: Zip MySQL Database Dump File via Batch File

Sep
01

First, get 7zip Command Line here.

Next, unzip to your desired directory – mine is in C:\7za920.

Based on my previous post, MySQL: Backup Database using Batch File. Add the following lines after echo Database Backups Completed and before pause.

 
cd C:\7za920
7za a myapp_db_%datestr%.zip D:\BACKUPS\myapp_%datestr%.sql >Log.txt 2>&1
echo Zipping Backups Completed

Now you are done! Execute the batch file to see the result!

p/s: >Log.txt 2>&1 used for creating a log file for all the process to zip the file.