hints Archives | Lee Willis https://www.leewillis.co.uk/tag/hints/ Tue, 07 Mar 2017 13:46:54 +0000 en-US hourly 1 https://wordpress.org/?v=6.8.2 Custom model events with Laravel 5.4 https://www.leewillis.co.uk/custom-model-events-laravel/ https://www.leewillis.co.uk/custom-model-events-laravel/#comments Tue, 07 Mar 2017 13:46:42 +0000 https://www.leewillis.co.uk/?p=936 Laravel’s eloquent ORM has a nice system for tracking, and responding to events on models. This allows you to create an Observer class that handles code that should run when various actions are performed on a model. There are a … Continue reading

The post Custom model events with Laravel 5.4 appeared first on Lee Willis.

]]>
Laravel’s eloquent ORM has a nice system for tracking, and responding to events on models. This allows you to create an Observer class that handles code that should run when various actions are performed on a model. There are a set of standard events (created, updated, deleted etc.) that can be used to achieve many things. These can be useful if you need to send notifications when a new model is created, or update something when a model is updated.

However, as your models get more complex, you may find yourself needing different events. Imagine you have a content model which has statuses, for example draft, pending review, and published. If you want to perform different actions based on the model status then you can attach an observer to the model, and listed for the created / updated events.

You can then check the new status and fire off the relevant code. This can get messy though, and also doesn’t reflect what happened to the model to move it into it’s current status. For example, is the content in “pending review” because it’s a draft that someone wants to publish, or an article that was published but has been bumped back down for review? The current model status doesn’t tell you that.

Now, you can create your own events in Laravel, and fire them off, however that would mean you have some event listeners in your model observer, and some in a bespoke event listener. Messy.

It’s nicer to be able to handle everything the same way. Fortunately Eloquent’s model event system can be extended to support custom events which you can fire at the appropriate point.

To add a new event, simply set the $observables property on your model.

<?php

use Illuminate\Database\Eloquent\Model;

class MyContentModel extends Model
{
  /**
   * Additional observable events.
   */
  protected $observables = [
    'sentforreview',
    'rejected',
  ];
}

This tells Laravel that there are two new model events that an observer can listen for. In our observer, we can listed to these by defining the appropriate function as we would with any of the standard events:

<?php

namespace App\Observers;

class MyContentObserver
{

  public function sentforreview(MyContent $myContent)
  {
    // Your logic here.
  }

  public function rejected(MyContent $myContent)
  {
    // Your logic here.
  }

}

So far, so good. We have defined our custom observables, and built a method to listed for them. However – the event will never be fired. After all, Laravel doesn’t know when it should be fired. Firing the event is simple though. Within your model you simply call fireModelEvent() and tell it the event you are firing, for example:

<?php

use Illuminate\Database\Eloquent\Model;

class MyContentModel extends Model
{
  public function sendForReview()
  {
     // Do any other updates to the model here.
     // Then fire the event.
     $this->fireModelEvent('sentforreview', false);
  }

  public function reject()
  {
     // Do any other updates to the model here.
     // Then fire the event.
     $this->fireModelEvent('rejected', false);
  }
}

Now, you can have a single model observer that handles standard, and custom events keeping everything nice and tidy.

The post Custom model events with Laravel 5.4 appeared first on Lee Willis.

]]>
https://www.leewillis.co.uk/custom-model-events-laravel/feed/ 4
Default Lowest Shipping Choice on WP e-Commerce https://www.leewillis.co.uk/default-lowest-shipping-choice-on-wp-e-commerce/ https://www.leewillis.co.uk/default-lowest-shipping-choice-on-wp-e-commerce/#respond Wed, 29 Oct 2014 23:01:18 +0000 http://www.leewillis.co.uk/?p=691 The WP e-Commerce plugin no-longer defaults the cheapest shipping option at checkout. This can be great if you want customers to consider other shipping options that may be beneficial for them (For example quicker delivery, insurance etc.). If you do … Continue reading

The post Default Lowest Shipping Choice on WP e-Commerce appeared first on Lee Willis.

]]>
The WP e-Commerce plugin no-longer defaults the cheapest shipping option at checkout. This can be great if you want customers to consider other shipping options that may be beneficial for them (For example quicker delivery, insurance etc.).

If you do want it to default to the cheapest option, try this simple plugin:

WP e-Commerce Default Lowest Shipping Choice
by Lee Willis

A straightforward plugin that makes WP e-Commerce checkout default to the lowest available rate when first populating shipping choices.

The plugin’s available for forking and contribution over on GitHub

Stats:

  • Current version: 1.2
  • Downloaded 976 times

The post Default Lowest Shipping Choice on WP e-Commerce appeared first on Lee Willis.

]]>
https://www.leewillis.co.uk/default-lowest-shipping-choice-on-wp-e-commerce/feed/ 0
Change order of payment gateways in Easy Digital Downloads https://www.leewillis.co.uk/change-order-payment-gateways-easy-digital-downloads/ https://www.leewillis.co.uk/change-order-payment-gateways-easy-digital-downloads/#comments Tue, 28 Oct 2014 22:24:00 +0000 http://www.leewillis.co.uk/?p=682 Easy Digital Downloads lets you enable multiple payment gateways to give customers a choice about how they want to pay. The core plugin also lets you choose a default option, but it doesn’t let you choose the order that options are … Continue reading

The post Change order of payment gateways in Easy Digital Downloads appeared first on Lee Willis.

]]>
Easy Digital Downloads lets you enable multiple payment gateways to give customers a choice about how they want to pay. The core plugin also lets you choose a default option, but it doesn’t let you choose the order that options are presented at checkout. That can lead to odd UX where the default selected option appears second, or third in the list:

Screenshot from 2014-10-28 22:10:46

Fortunately, it’s pretty easy to change this order – simply paste the following function into your theme’s functions.php file, or use a functionality plugin. Just change the $gateway_order array to the gateways you have enabled, in the order you want them, and you’re good to go.

function lw_edd_enabled_payment_gateways($gateways) {
	$gateway_order = array(
		'stripe',
		'paypal',
		'manual',
	);
	$new_gateways = array();
	foreach ( $gateway_order as $gateway ) {
		if ( ! empty( $gateways[$gateway] ) ) {
			$new_gateways[$gateway] = $gateways[$gateway];
		}
	}
	return $new_gateways;
}
add_filter(
	'edd_enabled_payment_gateways',
	'lw_edd_enabled_payment_gateways',
	10,
	1
);

Here’s what our revised default checkout looks like – much neater.
Screenshot from 2014-10-28 22:15:48

The post Change order of payment gateways in Easy Digital Downloads appeared first on Lee Willis.

]]>
https://www.leewillis.co.uk/change-order-payment-gateways-easy-digital-downloads/feed/ 3
Processing feed imports in bigger chunks https://www.leewillis.co.uk/processing-feed-imports-in-bigger-chunks/ https://www.leewillis.co.uk/processing-feed-imports-in-bigger-chunks/#respond Wed, 26 Jun 2013 09:51:03 +0000 http://www.leewillis.co.uk/?p=568 Just leaving this here for next time I need this. The change below allows you to change the number of items that Drupal’s feeds importer will process at once. /* Set feeds importer to import in chunks of 500, rather … Continue reading

The post Processing feed imports in bigger chunks appeared first on Lee Willis.

]]>
Just leaving this here for next time I need this. The change below allows you to change the number of items that Drupal’s feeds importer will process at once.

/* Set feeds importer to import in chunks of 500, rather than 50
 * Ref: http://drupalcode.org/project/feeds.git/blob/b39b547768abbda3b0d7b5425593c0e7c05800f2:/README.txt#l185
 */
$conf['feeds_process_limit'] = 500;

The post Processing feed imports in bigger chunks appeared first on Lee Willis.

]]>
https://www.leewillis.co.uk/processing-feed-imports-in-bigger-chunks/feed/ 0
Adding SKU to email notifications in WP e-Commerce https://www.leewillis.co.uk/adding-sku-to-email-wp-e-commerce/ https://www.leewillis.co.uk/adding-sku-to-email-wp-e-commerce/#comments Sat, 11 May 2013 19:18:51 +0000 http://www.leewillis.co.uk/?p=561 Adding the SKU (Or other information) to order emails in WP e-Commerce has been an often-requested feature. Up until recently it meant editing plugin files to apply the changes, and remembering to re-apply them on every update. Thanks to the … Continue reading

The post Adding SKU to email notifications in WP e-Commerce appeared first on Lee Willis.

]]>
Adding the SKU (Or other information) to order emails in WP e-Commerce has been an often-requested feature. Up until recently it meant editing plugin files to apply the changes, and remembering to re-apply them on every update.

Thanks to the great work that’s been going on in WP e-Commerce core – there are now a bunch of useful hooks and filters that allow the content to be changed by filters rather than by editing the plugin direct.

So – if you want to add the SKU to your order emails you can just install and enable the plugin below to add it for you.

The post Adding SKU to email notifications in WP e-Commerce appeared first on Lee Willis.

]]>
https://www.leewillis.co.uk/adding-sku-to-email-wp-e-commerce/feed/ 1
Adding body classes based on Drupal role / user ID https://www.leewillis.co.uk/body-classes-drupal-role-user-id/ https://www.leewillis.co.uk/body-classes-drupal-role-user-id/#comments Sun, 13 Jan 2013 15:28:05 +0000 http://www.leewillis.co.uk/?p=481 I’m writing this down here quickly because it strikes me that it may well come in handy to others. This little module snippet that will add classes to Drupal’s html (On the body element) to indicate the current user’s role, … Continue reading

The post Adding body classes based on Drupal role / user ID appeared first on Lee Willis.

]]>
I’m writing this down here quickly because it strikes me that it may well come in handy to others. This little module snippet that will add classes to Drupal’s html (On the body element) to indicate the current user’s role, and user ID.

Before:

After:

Handy if for example you want to style things differently based on the user’s role. Just drop it into your module, and replace MODULE with your module name.

The code:

function MODULE_preprocess_html ( &$variables ) {

  global $user;

  foreach ( $user->roles as $role_id => $role ) {
    $variables['classes_array'][] = "role-id-".$role_id;
    $variables['classes_array'][] = "role-".strtolower(drupal_clean_css_identifier($role));
  }

  $variables['classes_array'][] = "user-uid-".$user->uid;

}

The post Adding body classes based on Drupal role / user ID appeared first on Lee Willis.

]]>
https://www.leewillis.co.uk/body-classes-drupal-role-user-id/feed/ 4
Including a git repo as an SVN external https://www.leewillis.co.uk/including-git-repo-svn-external/ https://www.leewillis.co.uk/including-git-repo-svn-external/#respond Fri, 13 Apr 2012 20:03:18 +0000 http://www.leewillis.co.uk/?p=436 I have a couple of bits of code that I keep in a local SVN repo1 Some of that codes extend existing projects, and include external projects in subfolders. SVN has a great piece of functionality that allows you to … Continue reading

The post Including a git repo as an SVN external appeared first on Lee Willis.

]]>
I have a couple of bits of code that I keep in a local SVN repo1

Some of that codes extend existing projects, and include external projects in subfolders. SVN has a great piece of functionality that allows you to embed other SVN repositories in sub-folders in a project, and have them automatically updated.

Unfortunately – while I’m using SVN locally, the code I’m including is hosted in GIT repositories.

Natch.

Fortunately, the Internet is a wonderful place, and after a woeful tweet, the ever-helpful @tarendai came up with the spectacularly useful observation:

Turns out that github implemented SVN access to their repos. So – you can include github2 projects into SVN repositories as SVN externals – hurrah!

To get it set up, it’s just the same as a standard SVN external, e.g.

$ svn propedit svn:externals .

and then give the folder you want it put in, and the github SVN url, e.g. to include the Campaign Monitor PHP API:

createsend-php https://github.com/campaignmonitor/createsend-php/

#win

1. Note: I’ll probably move to git shortly, tempted by the fact I use it at work, and by the awesomeness that is git-flow
2. Yes, I know that doesn’t solve it for the general git case, but seriously – how cool anyway 🙂

The post Including a git repo as an SVN external appeared first on Lee Willis.

]]>
https://www.leewillis.co.uk/including-git-repo-svn-external/feed/ 0
Sorting the list of downloadable files in WP e-Commerce https://www.leewillis.co.uk/sorting-the-list-of-downloadable-files-in-wp-e-commerce/ https://www.leewillis.co.uk/sorting-the-list-of-downloadable-files-in-wp-e-commerce/#comments Mon, 02 Jan 2012 11:19:39 +0000 http://www.leewillis.co.uk/?p=417 Those of you who run stores with downloadable products may well have struggled with the following problem. If you try an attach an existing file to a product (Either because the same file is supplied with different products, or you … Continue reading

The post Sorting the list of downloadable files in WP e-Commerce appeared first on Lee Willis.

]]>
Those of you who run stores with downloadable products may well have struggled with the following problem. If you try an attach an existing file to a product (Either because the same file is supplied with different products, or you have product variants that you need to set downloads against), then you may well have run into the following:

This list is a bit of a nightmare to find the file you want. It’s not sorted by anything obvious – not name, and not date-modified as far as I can tell. Finding a file is a bit of a scroll-and-hope affair. Much better to have a nice, ordered list like this:

This is an itch I’ve been wanting to scratch for a while now, and it turns out the code is pretty simple. Just drop the following in your theme’s functions.php file and revel in the glory of a nice, ordered file list.

function site_plugin_custom_sort_function ( $a, $b ) {
  return strtolower($a['display_filename']) > strtolower($b['display_filename']);
}

function site_plugin_sort_download_list ( $download_list ) {
  // Sort the multidimensional array
  usort($download_list, "site_plugin_custom_sort_function");
  return $download_list;
}
add_filter ( 'wpsc_downloadable_file_list', 'site_plugin_sort_download_list' );

The post Sorting the list of downloadable files in WP e-Commerce appeared first on Lee Willis.

]]>
https://www.leewillis.co.uk/sorting-the-list-of-downloadable-files-in-wp-e-commerce/feed/ 3
Always Show The Admin Bar in WordPress https://www.leewillis.co.uk/always-show-admin-bar-wordpress/ https://www.leewillis.co.uk/always-show-admin-bar-wordpress/#comments Thu, 24 Feb 2011 19:48:06 +0000 http://www.leewillis.co.uk/?p=350 WordPress 3.1 is now out and one of the most talked about features is the “admin bar”. WordPress.com users have had something similar for a while, and it seems to be one of those “love it or hate it” features. … Continue reading

The post Always Show The Admin Bar in WordPress appeared first on Lee Willis.

]]>
WordPress 3.1 is now out and one of the most talked about features is the “admin bar”. WordPress.com users have had something similar for a while, and it seems to be one of those “love it or hate it” features. (If you’re not a fan of the admin bar – then check out Scott Kingsley’s excellent Admin Bar Disabler.)

Personally I’m a big fan of the feature, it makes the whole system just that little bit easier to use for non technical people with it’s simple menus for adding new posts, managing comments, and being alerted to updates:

However, the admin bar only shows up when you’re logged in – which means that your users have to go to login before they’ll see it. That’s fine if you tick the “Remember Me” box when logging in – but I’m not sure I’m happy to recommend to people that they stay permanently logged in.

So, one of the first things I thought of with the new feature was “Wouldn’t it be great if the admin bar was always there”.

Announcing the “Always Show The Admin Bar” plugin.

This handily ensures that the admin bar is always visible for anyone who’s previously logged into the site. It’s a neat solution for providing site editors / admins with an easy to use way to log in, while not cluttering up your site for everyday visitors with an intrusive login form.

If you like it – you should donate here:


The post Always Show The Admin Bar in WordPress appeared first on Lee Willis.

]]>
https://www.leewillis.co.uk/always-show-admin-bar-wordpress/feed/ 1
Post Thumbnails only for Custom Post Types https://www.leewillis.co.uk/post-thumbnails-custom-post-types/ https://www.leewillis.co.uk/post-thumbnails-custom-post-types/#comments Thu, 26 Aug 2010 19:19:32 +0000 http://www.leewillis.co.uk/?p=287 The guys over at getshopped.org are working on a pretty major revision to their WP e-Commerce plugin. Part of this is migrating “products” into custom post types, including using post thumbnails for the product images. One of the problems that … Continue reading

The post Post Thumbnails only for Custom Post Types appeared first on Lee Willis.

]]>
The guys over at getshopped.org are working on a pretty major revision to their WP e-Commerce plugin. Part of this is migrating “products” into custom post types, including using post thumbnails for the product images. One of the problems that has cropped up in testing was where user’s themes didn’t support post thumbnails. The first fix for was for the plugin to force “theme support” for post thumbnails by calling

add_theme_support( 'post-thumbnails' );

This worked, in that it solved the problem – all of the post-thumbnail function calls worked. However, it left a bug, and one that isn’t a software, or a techie bug – it was the worst kind – a bad user experience. Suddenly “posts” and “pages” would allow you to set featured images when authoring, or editing, but because the theme didn’t really support post thumbnails, those images would never show leaving a confused, bewildered user. So, the solution is to enable the support in general, but disable it for specific post types:

function check_thumbnail_support() {
 if (!current_theme_supports('post-thumbnails')) {
   add_theme_support( 'post-thumbnails' );
   add_action('init','remove_posttype_thumbnail_support');
 }
}
add_action('after_setup_theme','check_thumbnail_support',99);

function remove_posttype_thumbnail_support() {
 remove_post_type_support('post','thumbnail');
 remove_post_type_support('page','thumbnail');
}

This does three important things:
1. Check to see if the theme supports thumbnails already – if so, do nothing
2. If not, then turn on post thumbnail support, but also …
3. Remove support from “post” and “page”

The “gotcha” if you’re trying to come up with this yourself is that theme support is for “post-thumbnails”, but post_type_support is for the more generic “thumbnails”. Hope this comes in handy some someone.

The post Post Thumbnails only for Custom Post Types appeared first on Lee Willis.

]]>
https://www.leewillis.co.uk/post-thumbnails-custom-post-types/feed/ 3