Overview
  • Package
  • Class

Packages

  • Invoke
    • Helpers

Functions

  • asset
  • auth
  • content
  • copyright_date
  • dd
  • e
  • exceptions_error_handler
  • featured_image
  • is_even
  • is_odd
  • plural_count
  • request
  • str_limit
  • str_slug
  • strip_non_numeric
  • template_is
  • url
  • var_template_include
  • view
  • word_limit
  1 <?php
  2 
  3 /**
  4  * @package Invoke_Helpers
  5  * @version 1.0
  6  */
  7 
  8 /*
  9 Plugin Name: Invoke Helpers
 10 Plugin URI: http://wordpress.org/plugins/invoke-helpers/
 11 Description: Theme and function helpers that make using WordPress more pleasant
 12 Author: Invoke Media
 13 Version: 1.0
 14 Author URI: http://www.invokemedia.com
 15 */
 16 
 17 function var_template_include($t)
 18 {
 19     $GLOBALS['current_theme_template'] = basename($t, '.php');
 20     return $t;
 21 }
 22 
 23 add_filter('template_include', 'var_template_include', 1000);
 24 
 25 if (!function_exists('exceptions_error_handler')) {
 26     /**
 27      * Convert errors into Exceptions
 28      * @param int $severity
 29      * @param string $message
 30      * @param string $filename
 31      * @param int $lineno
 32      * @return Exception
 33      */
 34     function exceptions_error_handler($severity, $message, $filename, $lineno)
 35     {
 36         if (error_reporting() == 0) {
 37             return;
 38         }
 39         if (error_reporting() & $severity) {
 40             throw new ErrorException($message, 0, $severity, $filename, $lineno);
 41         }
 42     }
 43 }
 44 
 45 set_error_handler('exceptions_error_handler');
 46 
 47 if (!function_exists('strip_non_numeric')) {
 48     /**
 49      * Strip characters that are not numbers
 50      * @param string $value
 51      * @return string
 52      */
 53     function strip_non_numeric($value)
 54     {
 55         return preg_replace("/[^0-9]/", "", $value);
 56     }
 57 }
 58 
 59 if (!function_exists('plural_count')) {
 60     /**
 61      * Returns a 's' given a number. Works well for plural and singular words
 62      * @param string $word
 63      * @param int $count
 64      * @return string
 65      */
 66     function plural_count($word, $count)
 67     {
 68         $added_s = $count == 1 ? '': 's';
 69         return $word.$added_s;
 70     }
 71 }
 72 
 73 if (!function_exists('asset')) {
 74     /**
 75      * Returns an url for an asset in the current theme
 76      * @param string $value
 77      * @return string
 78      */
 79     function asset($value)
 80     {
 81         return sprintf("%s", get_template_directory_uri() . '/' . $value);
 82     }
 83 }
 84 
 85 if (!function_exists('copyright_date')) {
 86     /**
 87      * Returns a string for copyright for the difference in year started and current year
 88      * @param string $year
 89      * @return string
 90      */
 91     function copyright_date($year = '2016')
 92     {
 93         return (date('Y') == $year) ? null: '-' . date('Y');
 94     }
 95 }
 96 
 97 if (!function_exists('str_slug')) {
 98     /**
 99      * Slugify a string with certain rules
100      * @param string $string
101      * @param array $replace
102      * @param string $delimiter
103      * @return string
104      */
105     function str_slug($string, $replace = array(), $delimiter = '-')
106     {
107         // https://github.com/phalcon/incubator/blob/master/Library/Phalcon/Utils/Slug.php
108         if (!extension_loaded('iconv')) {
109             throw new Exception('iconv module not loaded');
110         }
111 
112         // Save the old locale and set the new locale to UTF-8
113         $oldLocale = setlocale(LC_ALL, '0');
114         setlocale(LC_ALL, 'en_US.UTF-8');
115         $clean = iconv('UTF-8', 'ASCII//TRANSLIT', $string);
116 
117         if (!empty($replace)) {
118             $clean = str_replace((array) $replace, ' ', $clean);
119         }
120 
121         $clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean);
122         $clean = strtolower($clean);
123         $clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean);
124         $clean = trim($clean, $delimiter);
125 
126         // Revert back to the old locale
127         setlocale(LC_ALL, $oldLocale);
128         return $clean;
129     }
130 }
131 
132 if (!function_exists('auth')) {
133     include 'inc/Auth.php';
134     /**
135      * A helper for the autenticated user
136      * @return Auth
137      */
138     function auth()
139     {
140         return new Auth();
141     }
142 }
143 
144 if (!function_exists('request')) {
145     include 'inc/Request.php';
146     /**
147      * A helper for POST and GET requests
148      * @return Request
149      */
150     function request()
151     {
152         return new Request();
153     }
154 }
155 
156 if (!function_exists('str_limit')) {
157     /**
158      * Limit a string to a certain amount of characters
159      * @param string $value
160      * @param int $limit
161      * @param string $end
162      * @return string
163      */
164     function str_limit($value, $limit = 100, $end = '...')
165     {
166         if (mb_strwidth($value, 'UTF-8') <= $limit) {
167             return $value;
168         }
169         return rtrim(mb_strimwidth($value, 0, $limit, '', 'UTF-8')).$end;
170     }
171 }
172 
173 if (!function_exists('word_limit')) {
174     /**
175      * Limit a sentence to a specific number of words
176      * @param string $text
177      * @param int $limit
178      * @param string $end
179      * @return string
180      */
181     function word_limit($text, $limit = 20, $end = '...')
182     {
183         if (str_word_count($text, 0) > $limit) {
184             $words = str_word_count($text, 2);
185             $pos = array_keys($words);
186             $text = trim(substr($text, 0, $pos[$limit])) . $end;
187         }
188         return $text;
189     }
190 }
191 
192 if (!function_exists('dd')) {
193     /**
194      * Dump and die with nice formatting and styling
195      * @param mixed $data
196      * @return string
197      */
198     function dd($data)
199     {
200         ini_set("highlight.comment", "#969896; font-style: italic");
201         ini_set("highlight.default", "#FFFFFF");
202         ini_set("highlight.html", "#D16568");
203         ini_set("highlight.keyword", "#7FA3BC; font-weight: bold");
204         ini_set("highlight.string", "#F2C47E");
205         $output = highlight_string("<?php\n\n" . var_export($data, true), true);
206         echo "<div style=\"text-align:left; background-color: #1C1E21; padding: 1rem\">{$output}</div>";
207         die();
208     }
209 }
210 
211 if (!function_exists('is_even')) {
212     /**
213      * Tell if an array length or integer is even
214      * @param mixed $value
215      * @return int
216      */
217     function is_even($value)
218     {
219         if (is_array($value)) {
220             return count($value) % 2 !== 0;
221         }
222 
223         return $value % 2 !== 0;
224     }
225 }
226 
227 if (!function_exists('is_odd')) {
228     /**
229      * Tell if an array length or integer is odd
230      * @param mixed $value
231      * @return int
232      */
233     function is_odd($value)
234     {
235         if (is_array($value)) {
236             return count($value) % 2 == 0;
237         }
238 
239         return $value % 2 == 0;
240     }
241 }
242 
243 if (!function_exists('url')) {
244     /**
245      * A wrapper around get_permalink and site_url
246      * @param WP_Post|string $uri
247      * @param string|null $protocol
248      * @return string
249      */
250     function url($uri, $protocol = null)
251     {
252         // calls the correct function is the object is a post
253         if (is_object($uri) && get_class($uri) == 'WP_Post') {
254             return get_permalink($uri);
255         }
256 
257         // just a general site URL from a URI
258         return site_url($uri, $protocol);
259     }
260 }
261 
262 if (!function_exists('e')) {
263     /**
264      * Escape html entities
265      * @param string $value
266      * @return string
267      */
268     function e($value)
269     {
270         echo htmlentities($value, ENT_QUOTES, 'utf-8');
271     }
272 }
273 
274 if (!function_exists('template_is')) {
275     /**
276      * Figure out if a template is the current template of the page
277      * @param array|string $names
278      * @return bool
279      */
280     function template_is($names)
281     {
282         $names = is_array($names) ? $names: [$names];
283         return count(array_filter($names, function ($uri) {
284             return $GLOBALS['current_theme_template'] == $uri;
285         }));
286     }
287 }
288 
289 if (!function_exists('content')) {
290     /**
291      * Get the content for the current page or a given post
292      * @param WP_POST|null $post
293      * @return string
294      */
295     function content($post = null)
296     {
297         $post = is_null($post) ? get_post(): $post;
298 
299         return apply_filters('the_content', $post->post_content);
300     }
301 }
302 
303 if (!function_exists('featured_image')) {
304     /**
305      * Get the featured image of the current post or a given post
306      * @param WP_POST|null $post
307      * @return object
308      */
309     function featured_image($post = null)
310     {
311         $post = is_null($post) ? get_post(): $post;
312 
313         $image = (object)wp_get_attachment_metadata(get_post_thumbnail_id($post));
314 
315         $image->url = sprintf("/wp-content/uploads/%s", $image->file);
316         return $image;
317     }
318 }
319 
320 if (!function_exists('view')) {
321     /**
322      * Render a partial template with a given array of data
323      * @param string $filename
324      * @param array $vars
325      * @return string
326      */
327     function view($filename, $vars = null)
328     {
329         try {
330             if (is_array($vars) && !empty($vars)) {
331                 extract($vars);
332             }
333 
334             ob_start();
335 
336             include(get_template_directory() . '/' . $filename);
337 
338             return ob_get_clean();
339         } catch (Exception $e) {
340             dd(sprintf("%s in %s:%s when using the `view` function.", $e->getMessage(), $filename, $e->getLine()));
341         }
342     }
343 }
344 
API documentation generated by ApiGen