Archive for January, 2011

Rails Git Heroku Terminal command notes

Wednesday, January 19th, 2011

first cd to the project directory:

git init

git add .

git commit master

heroku create “NewApplicationName”

git push heroku master

heroku rake db:schema:load

rake db:schema:dump (handy to dump local db if you’ve been modifying it w/o migrations…)

heroku rake db:migrate

WordPress affect categories search form searches – solution 2

Tuesday, January 18th, 2011

Please see this post if you’re only wanting to limit the form to a single category, as it’s as simple as adding a hidden form field.

This solution uses functions.php, adding something like the following:

function SearchFilter($query)
{
if($query->is_search){
$query->set(‘cat’,’0′);
}
}
add_filter(‘pre_get_posts’,’SearchFilter’);

similar post:
http://alexyz.com/wordpress-rss-feed-exclude-categories-or-only-serve-one-particular-category/

http://alexyz.com/wordpress-limit-search-form-to-a-category-2/

similar reference:

http://speckyboy.com/2010/09/19/10-useful-wordpress-search-code-snippets/

Design Patterns Reference: Visitor

Monday, January 3rd, 2011

“The Visitor Design Pattern constructs distinct objects containing an algorithm that, when consumed by a parent object in a standard way, apply that algorithm to the parent object.”

 

class CD
{
public $band;
public $title;
public $price;

 

public function __construct($band, $title, $price)
{
$this->band = $band;
$this->title = $title;
$this->price = $price;
}

 

public function buy()
{
// stub
}

 

public function acceptVisitor($visitor)
{
$visitor->visitCD($this);
}
}

 

class CDVisitorLogPurchase
{
public function visitCD($cd)
{
$logline = “{$cd->title} by {$cd->band} was purchased for {$cd->price} “;
$logline .= “at ” . sdate(‘r’) . “\n”;

 

file_put_contents(‘/logs/purchases.log’, $logline, FILE_APPEND);
}
}

 

// usage
$externalBand = ‘Never Again’;
$externalTitle = ‘Waste of a Rib’;
$externalPrice = 9.99;

 

$cd = new CD($externalBand, $externalTitle, $externalPrice);
$cd->buy();
$cd->acceptVisitor(new CDVisitorLogPurchase());

 

// adding a new Visitor to accomplish a new functionality is straight forward
class CDVisitorPopulateDiscountList
{
public function visitCD($cd)
{
if ($cd->price < 10)
{
$this->_populateDiscountList($cd);
}
}

 

protected function _populateDiscountList($cd)
{
// stub connects to sqlite and logs etc…
}
}

 

// again, usage
$cd = new CD($externalBand, $externalTitle, $externalPrice);
$cd->buy();
$cd->acceptVisitor(new CDVisitorLogPurchase());
$cd->acceptVisitor(new CDVisitorPopulateDiscountList());

 

source: http://www.wiley.com/WileyCDA/WileyTitle/productCd-0470496703,descCd-DOWNLOAD.html

 

 

WordPress creating users in MySQL via script wp_user_level wp_capabilities wp_users

Monday, January 3rd, 2011

Scenario:
Creating a WordPress blog with an entire company of users.
We want employees to already have an account, so that registration may be turned OFF otherwise.
From an excel list of employees, we’ll extract their email, make an account for each email in the WordPress database, and then they may change their passwords themselves. You can imagine what the php script will look like, to extract the email, and foreach insert the following (uniqueusername and 5 replaced of course with incrementing id numbers and changing emailnames@ourcompany.com):

 

INSERT INTO wp_users
(id,user_login,user_pass,user_email,display_name)
VALUES
(‘5′,’uniqueusername’,md5(‘pass’),’uniqueusername@ourcompany.com’,’uniqueusername’);

 

// to note, this below is simultaneously a nice example of how to insert multiple rows with one MySQL INSERT statement

 

INSERT INTO wp_usermeta
(user_id,meta_key,meta_value)
VALUES
(‘5′,’wp_capabilities’,’a:1:{s:6:”author”;s:1:”1″;}’),
(‘5′,’wp_user_level’,’10’);

 

Thus, the full script looks something like this:

 

$employees = array(’employee1@somesite.com’,’employee2@somesite.com’,’employee3@somesite.com’,’andsoon…’);

 

$startNumber = 10; // if you already have users, pick the next highest number, as it uses this for several tables below, we need them to happen at the same time so that attributes belong to specific users…

 

$db_name = “databasename”;
$db_host = “localhost”;
$db_username = “username”;
$db_password = “password”;

 

// note the nice error reporting so that we can troubleshoot connections, users, table selections, and so forth…
$link = mysql_connect($db_host, $db_username, $db_password);
if (!$link) {
die(‘Could not connect: ‘ . mysql_error());
}
else
{
echo ‘Connected successfully<br />’;
}

 

$db_selected = mysql_select_db($db_name, $link);
if (!$db_selected) {
die (‘Can\’t use foo : ‘ . mysql_error());
}
else
{
echo ‘Selected manyvoices database successfully<br />’;
}

 

foreach($employees as $employee)
{
list($user_name, $therest) = explode(‘@’,$employee);

 

$query1 = “INSERT INTO wp_users
(id,user_login,user_pass,user_email,display_name)
VALUES
(‘” . $startNumber . “‘,'” . $user_name . “‘,md5(‘pass’),'” . $employee . “‘,'” . $user_name . “‘)
“;
//echo $query1 . “<br />”;

 

$result1 = mysql_query($query1);
if(!result1){
die(‘Error: ‘ . mysql_error());
}
else{
echo “User: ” . $user_name . ” : Added<br />”;
}

 

$query2 = “INSERT INTO wp_usermeta
(user_id,meta_key,meta_value)
VALUES
(‘” . $startNumber . “‘,’wp_capabilities’,’a:1:{s:6:\”author\”;s:1:\”1\”;}’),
(‘” . $startNumber . “‘,’wp_user_level’,’10’)
“;
//echo $query2 . “<br />”;

 

$result2 = mysql_query($query2);
if(!result2){
die(‘Error: ‘ . mysql_error());
}
else
{
echo “User Credentials: ” . $user_name . ” : Updated<br />”;
}

 

$startNumber++;
}

 

mysql_close($link);