WordPress.org

Ready to get started?Download WordPress

Forums

Minimalistic Event Manager
Very useful (2 posts)

4 stars
  1. rolfo
    Member
    Posted 5 months ago #

    Just what I was looking for. Great if you could add "place", but hey I can just encode that as part of start/repeat dates with the 'alpha' option.

    I use this in function.php to have an easy shortcode for it:


    // Minimalistic Event Manager
    function my_mem_settings() {
      mem_plugin_settings( array( 'page' ), 'alpha' );
    }
    add_action( 'mem_init', 'my_mem_settings' );
    
    /**
    Find Date in a String
    
        01/01/2012 will return > 01-01-2012
        30-12-11 will return > 30-12-2011
        1 2 1985 will return > 01-02-1985
        10.12.1850 will return > 10-12-1850
        5 12 85 will return > 05-12-1985
        5 October 2012 will return > 02-10-2012
        MAY 5th 1985 will return > 05-05-1985
        1st March 81 will return > 01-03-1981
    
    */
    function find_date( $string ) {
    
            //Define month name:
            $month_names = array(
                    "january",
                    "february",
                    "march",
                    "april",
                    "may",
                    "june",
                    "july",
                    "august",
                    "september",
                    "october",
                    "november",
                    "december"
            );
    
            $month_names_short = array(
                    "jan",
                    "feb",
                    "mar",
                    "apr",
                    "may",
                    "jun",
                    "jul",
                    "aug",
                    "sep",
                    "oct",
                    "nov",
                    "dec"
            );
    
            $month_number=$month=$matches_year=$year=$matches_month_number=$matches_month_word=$matches_day_number="";
    
            //Match dates: 01/01/2012 or 30-12-11 or 1 2 1985
            preg_match( '/([0-9]?[0-9])[\.|\-|\/| ]([0-1]?[0-9])[\.|\-|\/| ]([0-9]{2,4})/', $string, $matches );
            if ( $matches ) {
                    if ( $matches[1] )
                            $day = $matches[1];
    
                    if ( $matches[2] )
                            $month = $matches[2];
    
                    if ( $matches[3] )
                            $year = $matches[3];
            }
    
            if ( empty( $month ) ) {
    			$s = " ".$string." ";
    			$s = str_replace(array('.', ',', ';'), " ", $s);
    			//Match month name:
    			preg_match( '/(' . implode( '|', $month_names ) . ')/i', $s, $matches_month_word );
    
    			if ( $matches_month_word ) {
    					if ( $matches_month_word[1] )
    							$month = array_search( strtolower( $matches_month_word[1] ), $month_names ) + 1;
    			}
    
    			//Match short month name:
    			preg_match( '/(' . implode( '|', $month_names_short ) . ')/i', $s, $matches_month_word );
    
    			if ( $matches_month_word ) {
    					if ( $matches_month_word[1] )
    							$month = array_search( strtolower( $matches_month_word[1] ), $month_names_short ) + 1;
    			}
    		}
    
            //Match 5th 1st 7, day:
            preg_match( '/([0-9]?[0-9])(st|nd|th|,)/', $string, $matches_day );
            if ( $matches_day ) {
                    if ( $matches_day[1] )
                            $day = $matches_day[1];
            }
    
            //Match Year if not already setted:
            if ( empty( $year ) ) {
                    preg_match( '/[0-9]{4}/', $string, $matches_year );
                    if ( $matches_year[0] )
                            $year = $matches_year[0];
            }
            if ( ! empty ( $day ) && ! empty ( $month ) && empty( $year ) ) {
                    preg_match( '/[0-9]{2}/', $string, $matches_year );
                    if ( $matches_year[0] )
                            $year = $matches_year[0];
            }
    
            //Check year:
            if ( 2 == strlen( $year ) && $year > 20 )
                    $year = '19' . $year;
            else if ( 2 == strlen( $year ) && $year < 20 )
                    $year = '20' . $year;
    
            $date = array(
                    'year'  => $year,
                    'month' => $month,
                    'day'   => $day
            );
    
            //Return today if nothing is found
            if ( empty( $year ) || empty( $month ) || empty( $day ) )
                    return time();
            else
                    return mktime(0, 0, 0, $month, $day, $year);
    }
    
    function sort_dates(&$dates) {
    	if (!is_array($dates)) return false;
    	foreach ($dates as &$val) {
    		$sortkey = date('Y.m.d', find_date($val));
    		$val = $sortkey.' '.$val;
    	}
    	sort($dates);
    	$left = strlen($sortkey)+1;
    	foreach ($dates as &$val) {
    		$val = substr($val, $left);
    	}
    	return $true;
    }
    
    /*
    Add this handy shortcode
    
    [mem-this-dates] = List dates for this post (start + all repeats)
    [mem-this-dates seperator=" & "] = same, but put & between (default = <br/>)
    [mem-this-dates id=117 seperator=" & "] = same, but for post ID number 117
    [mem-this-dates max=3] = List max 3 dates
    */
    function mem_list_dates_function($atts, $content = null) {
    	extract(shortcode_atts(array(
    		'seperator' => '<br/>',
    		'id' => -1,
    		'max' => 99
    	), $atts));
    	if ($id < 0) $id = get_the_ID();
    
    	$mem_start_date = get_post_meta($id, '_mem_start_date', true);
    	if ($mem_start_date == "" ) {
    		$s = "Please ask us ";
    	} else {
    		$mem_repeat_date = get_post_meta($id, '_mem_repeat_date', false);
    		$mem_repeat_date[] = $mem_start_date;
    		sort_dates($mem_repeat_date);
    		$s = "";
    		foreach ($mem_repeat_date as $val) {
    			if ($max > 0) {
    				if ($s) $s .= $seperator;
    				$s .= $val;
    				$max -= 1;
    			}
    		}
    	}
    	return $s;
    }
    add_shortcode('mem-list-dates', 'mem_list_dates_function');

  2. Manuel Schmalstieg
    Member
    Plugin Author

    Posted 4 months ago #

    Thanks for the first rating :)

    About the place: I would recommend to use a custom taxonomy for that, rather than a Custom Field, since a taxonomy will make it much easier to link events taking place at the same location.

Reply

You must log in to post.

About this Plugin

About this Topic

Tags

No tags yet.