It’s not much, but it’s my first little scriptlet with Node.JS,
specific purpose is to move files from one directory tree to an empty directory, one at a time, while another script processes each file as it enters the empty directory.
It’s not much, but it’s my first little scriptlet with Node.JS,
specific purpose is to move files from one directory tree to an empty directory, one at a time, while another script processes each file as it enters the empty directory.
Populating the BuddyPress user Profile page’s title, meta description, and meta keywords with some of the user-generated content, delivered via xprofile fields, seems so much better than the default options, or even what’s available in seopress, the only current BuddyPress SEO plugin.
While we’re at it, setting the on-page SEO data for Activities based on the content is a great move too.
You’ll have to modify the fields based on the data you have available, and what fields feed into which page data, but this code, placed in your theme’s functions.php file, is a good start.
Want to make a plugin based on this code for the lay-men? Feel free.
Note: extractCommonWords function based on the function here.
function bp_xprofile_title($title=null, $without_site_name=null){
global $bp, $current_blog;
if(defined('BP_ENABLE_MULTIBLOG')){
$blog_title=get_blog_option($current_blog->blog_id,'blogname');
}else {
$blog_title=get_blog_option(BP_ROOT_BLOG,'blogname');
}
if(bp_is_user_profile()){
$title_array = array();
if($data = xprofile_get_field_data( 'Company Name', $bp->displayed_user->id))
$title_array[] = "$data ({$bp->displayed_user->fullname})";
if($data = xprofile_get_field_data( 'Business Overview', $bp->displayed_user->id))
$title_array[] = $data;
$title_array[] = $blog_title;
$title = mb_strimwidth(strip_tags( esc_attr( implode( ' | ', $title_array))), 0, 70, '...');
}
if (bp_is_activity_component() && is_numeric( $bp->current_action )) {
$activity = bp_activity_get_specific( array( 'activity_ids' => $bp->current_action ) );
if ( $activity = $activity['activities'][0])
if(!empty($activity->content))
$title = mb_strimwidth(preg_replace("/[^A-Za-z0-9\s\s+\-]/", "", strip_tags( $activity->content)), 0, 70-3-3-strlen($blog_title), '...') . " | $blog_title";
}
return $title;
}
add_filter('bp_page_title', 'bp_xprofile_title',0);
function extractCommonWords($string){
$stopWords = array('i','a','about','an','and','are','as','at','be','by','com','de','en','for','from','how','in','is','it','la','of','on','or','that','the','this','to','was','what','when','where','who','will','with','und','the','www');
$string = strip_tags($string);
$string = trim($string); // trim the string
$string = preg_replace('/[^a-zA-Z0-9\s\s+\-]/', '', $string); // only take alphanumerical characters, but keep the spaces and dashes too…
$string = strtolower($string); // make it lowercase
preg_match_all('/\b.*?\b/i', $string, $matchWords);
$matchWords = $matchWords[0];
foreach ( $matchWords as $key=>$item ) {
if ( $item == '' || in_array(strtolower($item), $stopWords) || strlen($item) <= 3 ) {
unset($matchWords[$key]);
}
}
$wordCountArr = array();
if ( is_array($matchWords) ) {
foreach ( $matchWords as $key => $val ) {
$val = strtolower($val);
if ( isset($wordCountArr[$val]) ) {
$wordCountArr[$val]++;
} else {
$wordCountArr[$val] = 1;
}
}
}
arsort($wordCountArr);
$wordCountArr = array_slice($wordCountArr, 0, 10);
return array_keys($wordCountArr);
}
function bp_xprofile_meta()
{
global $bp;
if(bp_is_user_profile()){
$array = array();
if($data = xprofile_get_field_data( 'Business Category', $bp->displayed_user->id))
$array[] = htmlspecialchars_decode($data);
if($data = xprofile_get_field_data( 'Years of Experience', $bp->displayed_user->id))
$array[] = "$data years experience";
if($data = xprofile_get_field_data( 'Location', $bp->displayed_user->id))
$array[] = $data;
if($data = xprofile_get_field_data( 'Business Overview', $bp->displayed_user->id))
$array[] = $data;
$desc = mb_strimwidth(preg_replace("/[^A-Za-z0-9\s\s+\-&]/", "", strip_tags( implode( ' - ', $array))), 0, 150, '...');
echo '<meta name="description" content="'.$desc.'" />';
if($data = xprofile_get_field_data( 'Profile Details', $bp->displayed_user->id))
$array[] = $data;
if($data = xprofile_get_field_data( 'Company Name', $bp->displayed_user->id))
$array[] = $data;
$array[] = $bp->displayed_user->fullname;
$key_text = preg_replace("/[^A-Za-z0-9\s\s+\-]/", "", strip_tags( implode( ' ', $array)));
$keys = extractCommonWords($key_text);
echo '<meta name="keywords" content="'.implode(',', $keys).'" />';
}
if (bp_is_activity_component() && is_numeric( $bp->current_action )) {
$activity = bp_activity_get_specific( array( 'activity_ids' => $bp->current_action ) );
if ( $activity = $activity['activities'][0])
if(!empty($activity->content)){
$content = preg_replace("/[^A-Za-z0-9\s\s+\-]/", "", strip_tags( $activity->content));
echo '<meta name="description" content="'. mb_strimwidth($content, 0, 150, '...').'" />';
$keys = extractCommonWords($content);
echo '<meta name="keywords" content="'.implode(',', $keys).'" />';
}
}
}
add_action('wp_head', 'bp_xprofile_meta',1);
I’m getting really excited to use WPMU‘s MarketPress e-commerce plugin on the new JobShuk website.
As I wrote in this Tweet to the guy behind WordPress’ most popular e-commerce plugin for standalone installs, the 2 most exciting features of MarketPress are the unified marketplace across all users and revenue sharing. That means visitors can search/browse products contained in all the individual stores. Additionally, the Marketplace admin (me) can take a small percentage of the purchase price without getting into drop-shipping, affiliate payments, etc. That’s some easy money.
Now if only I could get MarketPress free for life. That would be a dream…
This might be obvious, if you’re bothering to read this, but when populating an Android Spinner with an ArrayAdapter, sourced by an array, the source array must not contain uninitialized elements.
For example, if you’re doing this:
String[] colors = new String[colors_json.length()+1];
for(int m=1; m<=colors_json.length(); m++){
if(colors_json.has("label")){
colors[m] = colors_json.getString("label");
}
}
then you might encounter a case when there are uninitialized elements in colors.
Instead, you can try something like this:
String[] colors0 = new String[colors_json.length()+1];
int index = 1;
for(int m=1; m<=colors_json.length(); m++){
if(colors_json.has("label")){
colors0[index] = colors_json.getString("label");
index++;
}
}
colors = new String[index];
System.arraycopy(colors0, 1, colors, 1, index-1);
You can then continue safely to set:
colors[0] = "Color"; color_adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, colors); color_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); color_spinner.setAdapter(color_adapter);
without concern of hitting a nasty NullPointerException.
My website uses the PHP function get_browser to see if a client is Javascript enabled. (I use this primarily to see if the visitor is a search engine bot and should see a long list of data or a real person and can handle AJAX-loaded data in tabs – most real people these days are using Javascript-enabled browsers.)
get_browser relies on the php_browscap.ini file, and the problem is that keeping the php_browscap.ini file up-to-date can be a chore, and should certainly be done automatically. (I notice the problem now that I use Chrome, and with every major release, the browscap file has no clue about my new browser, which is silly.)
So I finally sat down and wrote a basic script to fetch the updated file and restart my web server, which I’ll run weekly. This works for my FreeBSD box – change the paths and download command to fit your installation.
/usr/local/bin/wget -Ophp_browscap.ini "http://browsers.garykeith.com/stream.asp?PHP_BrowsCapINI" mv -f /usr/local/etc/php/php_browscap.ini /usr/local/etc/php/php_browscap.old.ini mv -f php_browscap.ini /usr/local/etc/php/ /usr/local/etc/rc.d/apache22 restart
I saved to: usr/local/www/mysite.com/get_php_browscap.sh
Then add it to your crontab:
# php_browscap update 5 4 * * 6 root /usr/local/www/mysite.com/get_php_browscap.sh >> /var/log/cron.log
I run it Saturday morning.
Any questions?
I’ve had the pleasure recently of working with some complex object oriented PHP with massive objects or lists of objects. The easiest way to have a look at your data for analyzing and debugging is to print_r it. Unfortunately, skimming through this data can be tedious, especially if you want to skip a couple of large, nested objects that are irrelevant to your data analysis.
When programming, I always keep Notepad++ open so I can keep a bunch of data accesible in tabs, such as notes, texts, data files, etc (and my tabs of saved files are seamlessly preserved between sessions, which is critical, of course). Notepad++ can handle bunches of different file formats out of the box, but PHP print_r isn’t one of them.
So I’ve pasted my print_r output into Notepad++, and the new file language is “Normal Text”. Immediately, bracket matching/highlighting works, which is great. I can go to an opening parens and see where it ends. So that’s it, right? Well, Notepad++ can also do code folding, like when you’re browsing a class, and you want to see all the class functions without all that pesky function code cluttering your view. So if you’ve got a PHP file open, for example, you can either click the [-] to the left of the code to fold that function, or go to View->Fold All, and then just click [+] to open the class and see the top-level items in the class in plain view.
So what about the print_r? Matlab! I haven’t used Matlab since my first or second year in engineering school, and never thought it would useful, but here it is. I don’t remember what the code looks like, and I don’t care. All I know is that when I select Language->Matlab, it let’s me do code folding on my print_r output, and that’s all that matters. A bunch of the other languages work well too, or the same as Matlab, but it was the first and best for my needs.
FYI, this breaks if you have variable data containing a chunk of text with newlines and whatnot, but so it goes.
That title long enough? It’s hard to describe this function, but you get it now, right?
Based on this function here.
/**
* getTimePlusOffsetDuringWork
*
* The function returns the timestamp of a date + business minutes
* @param timestamp|string startDate Beginning date/time as base for offset
* @param numeric offsetMinutes Work minutes to add to the startDate
* @param mixed Array of holiday dates, preferrably in the 'MM-DD' format
* @return integer Timestamp for the resulting time during Work hours,
* given the simple equation StartDate + offsetMinutes.
* Ignores after-hours, weekends, and holidays [for the first year after the startDate].
* Limitations: too many to list entirely.
* 9am-5pm work day only
* 1 year of holidays only
* no week/weekday dependent holidays
* Need to set timezone prior to calling this function if used outside of the server's timezone
* Relies on English locale setting
* @author Zvi Landsman
**/
function getTimePlusOffsetDuringWork($startDate,$offsetMinutes,$holidays=array("12-25","12-26","01-01","07-04")){
$j = $i = 0; //changed this to 0 or you were always starting one day ahead
$tmp1 = validation::is_timestamp($startDate)?$startDate:strtotime($startDate); //worked out a timstamp to start with
$offsetMinutes = round($offsetMinutes);
$simple_time = strtotime("+$offsetMinutes minutes", $tmp1);
if($simple_time <= strtotime('17:00', $tmp1))
{
//offset is still today
return $simple_time;
}
//Check if start time is after-hours
if ($tmp1 > strtotime('17:00', $tmp1))
{
$tmp1 = strtotime("09:00 +1 day", $tmp1);
}
//This checks if the minute offset puts us into tomorrow
$newdate = $tmp1;
$minutes_left = $offsetMinutes%480;
$newtime = strtotime("+ $minutes_left minutes", $newdate);
if($newtime > strtotime('17:00', $newdate))
{
$j++;
$minutes_left = round(($newtime - strtotime('17:00', $newdate))/60);
$tmp1 = strtotime("+ $minutes_left minutes", strtotime("09:00 +1 day", $tmp1));
}
else
{
$tmp1 = strtotime("+ $minutes_left minutes", $tmp1);
}
//create the holiday list for the first upcoming occurances only
$holidayList = array();
foreach ($holidays as $holiday)
{
$time_stamp=strtotime($holiday, $tmp1);
$date=($time_stamp < $tmp1)?
strftime("%d-%m-%Y",strtotime($holiday.' +1 year')):
strftime("%d-%m-%Y",$time_stamp);
$holidayList[] = $date;
}
$days_to_offset = floor($offsetMinutes/480);
while($i < $days_to_offset)
{
$tmp2 = strtotime("+$j day", $tmp1);
$day = strftime("%A",$tmp2);
$tmp = strftime("%d-%m-%Y",$tmp2);
if(($day != "Sunday") && ($day != "Saturday" )&&(!in_array($tmp, $holidayList)))
{
$i = $i + 1;
$j = $j + 1;
}
else
{
$j = $j + 1;
}
}
//$j = $j -1;
return strtotime("+$j days",$tmp1);
}
I’m completely open to improvements in the comments.