Category Archives : PHP


Symlinks on Windows using Composer

I was reminded today that in order to get symlinks to create on a windows machine that you need to “run as administrator” when starting your console, otherwise you’ll get an error like “Warning: symlink(): Cannot create symlink, error code(1314)” . While we’re on the topic, how about I demo how the autoloader is setup in my composer.json file along with the calling of post install and post update scripts.

    "autoload": {
		"psr-4": {
			"Nick\\": "script/"
		}
	},
 
    "require": {
        "php": ">=5.4",
        "wordpress/wordpress": "4.0"
    },
    "scripts": {
        "post-install-cmd": [
		"Nick\\Install::symlinks"
        ],
        "post-update-cmd": [
            "Nick\\Install::symlinks"
        ]
    },

Then, my post install file lives at /script/Install.php

<?php
 
namespace Nick;
 
use Composer\Script\Event;
 
class Install {
 
    public static function symlinks(Event $event) {
    	$rootDir = dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR;
		$wordpressDir = $rootDir . 'vendor' . DIRECTORY_SEPARATOR . 'wordpress' . DIRECTORY_SEPARATOR . 'wordpress' . DIRECTORY_SEPARATOR;
 
		//htaccess file
    	if(! file_exists($wordpressDir . '.htaccess')) {
			symlink($rootDir . '.htaccess', $wordpressDir . '.htaccess');
    	}
 
    	//wordpress config file
    	if(! file_exists($wordpressDir . 'wp-config.php')) {
    		symlink($rootDir . 'config' . DIRECTORY_SEPARATOR . 'wp-config.php', $wordpressDir . 'wp-config.php');
    	}
 
    	//wordpress db config file
    	if(! file_exists($wordpressDir . 'db-config.php')) {
    		symlink($rootDir . 'config' . DIRECTORY_SEPARATOR . 'db-config.php', $wordpressDir . 'db-config.php');
    	}
 
    	//wordpress
    	if(! file_exists($rootDir . 'public' . DIRECTORY_SEPARATOR . 'wp')) {
    		symlink($wordpressDir, $rootDir . 'public' . DIRECTORY_SEPARATOR . 'wp');
    	}
 
    }
}

Note that I’ve chosen to separate the database connection from the main wordpress config file so it can be ignored separately in version control.


MWI Plugin relative path to Mage.php

The MWI plugin http://www.mwi-plugin.com/ is a great plugin to integrate Magento and WordPress. It allows you to pull in Magento blocks, CSS, JS and other Magento functionality into a wordpress blog. However, one thing that bothered me with this wordpress plugin was it required you to enter the full path to the Mage.php file, starting from the server root. This does not allow for seamless movement of the website’s database between environments.

The below solution will allow you to enter a full path on the server if you’d like, or a relative path from the wordpress root to the Mage.php file. Open up file wp-content/plugins/magento-wordpress-integration/mwi.php and within function mwi_admin_page() just below the $magepath declaration right around line 134, add four lines to check for a relative path as below.

// Mage Path
$magepath = self::getValue('magepath');

//check for relative path from blog root
if(file_exists(ABSPATH . $magepath)) {
	$magepath = ABSPATH . $magepath;
}
//check for relative path from blog root

// notification/error messages

How to pre select radioButtonListRow and checkboxListRow with Yii 1

Looking at the code in Yii for radioButtonListRow and checkboxListRow it’s not apparent how to preselect these form elements with previously selected data.

<?php echo $form->checkboxListRow($model, 'id', CHtml::listData(models\web_data\Program::model()->findAll(array('order' => 'name')), 'id', 'name')); ?>

The solution is to set the model’s property to your selected values in the controller. You can use a scalar value or an array of values. For the above line of code, we can use the below -

$model->id = array(2,3);

or

$model->id = 2;

Using the google calendar api to delete an event with zend framework

I’m using zend framework’s GData library to work with google calendar api v2 and I’d implemented a solution to add and delete events. The adding of events works well, but deleting them did not. According to zend, the way to delete an event is like this

// Option 1: Events can be deleted directly
$event->delete();
 
// Option 2: Events can be deleted supplying the edit URL of the event
// to the calendar service, if known
$service->delete($event->getEditLink()->href);

Deleting via option #1

So after adding the event via the api, an object of Zend_Gdata_Calendar_EventEntry is returned and I should be able to simply do $event->delete(); to delete the event, but this does not work and returns the following error: “Expected response code 200, got 405. Http method DELETE is not supported by this URL”. It’s what the documentation says to do and it doesn’t work.

Deleting via option #2

So, let’s try the other way by storing the “edit link”. First we add the calendar entry successfully, then take the instance of Zend_Gdata_Calendar_EventEntry that is returned and call the delete as below

$event = $cal->addEvent(/* my own method that adds the event and returns instance of Zend_Gdata_Calendar_EventEntry */);
 
$link = $event->getEditLink()->href;
 
function deleteEventByEditLink($link) {
	$client = getClient();
	$service = new Zend_Gdata_Calendar($client);
 
	try {
		$response = $service->delete($link);
	} catch(Exception $e) {
		//event most likely didn't exist on the calendar. 
		return false;
	}
	return true;
}
 
function getClient() {
	return Zend_Gdata_ClientLogin::getHttpClient(GOOGLE_CALENDAR_USERNAME, GOOGLE_CALENDAR_PASSWORD, Zend_Gdata_Calendar::AUTH_SERVICE_NAME);
}

The $response variable above results in a NULL value and the entry does not delete.

What does work – Yes, how to actually delete events!

I was able to find a working solution by storing the event id rather than the edit link, then loading the event by id through the api and then calling the delete method as below.

/* after adding, extract and save the event id */
$event = $cal->addEvent();
$eventId = $event->id->text;
 
/* deleting */
$result = deleteEventById($eventId);
 
function deleteEventById($eventId) {
	$event = getEventById($eventId);
	try {
		$response = $event->delete();
	} catch(Exception $e) {
		//event most likely didn't exist on the calendar. 
		return false;
	}
	return true;
}
 
function getEventById($eventId) {
	// Create an authenticated HTTP client
	$client = getClient();
	$service = new Zend_Gdata_Calendar($client);
	try {
		return $service->getCalendarEventEntry($eventId);
	} catch (Zend_Gdata_App_Exception $e) {
		echo "Error: " . $e->getMessage();
	}
}
 
function getClient() {
	return Zend_Gdata_ClientLogin::getHttpClient(GOOGLE_CALENDAR_USERNAME, GOOGLE_CALENDAR_PASSWORD, Zend_Gdata_Calendar::AUTH_SERVICE_NAME);
}

Hope this saves somebody six hours of pain. Google calendar has API V3 out that seems to work pretty well, except for the oauth authentication bit… have fun with that.


WordPress filter when inserting post breaks my html

I’m programmatically inserting posts into wordpress and all of a sudden the drop down box I’d coded into the content was getting changed sometime between when I’d call wp_insert_post and when it gets saved in the database. The select and option tags would get stripped out and a p tag would get tossed in there randomly. The solution was to disable the filtering wordpress does on the $postarr by doing the following:

kses_remove_filters();
wp_insert_post( $postArray );
kses_init_filters();