Created
October 22, 2019 04:57
-
-
Save johnalarcon/941eed5902406ff38846a3c8ff2ad3d0 to your computer and use it in GitHub Desktop.
Shortcode to create a linked list of site policies.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Site policies shortcode. | |
* | |
* This shortcode creates a linked list of site policies. Any page that uses the | |
* word "policy" in the title or slug is deemed a policy to be listed. Using the | |
* shortcode [site-policies] in any post or page (or anywhere shortcodes work on | |
* your particular site,) will output a linked list the site policies. | |
* | |
* The following attributes are supported; include as many or as few as you like | |
* to get the desired result. The default behavior is to display all policies in | |
* an unordered list that has a class name of "policy-list". The default sorting | |
* is by policy title, ascending. | |
* | |
* Supported attributes: | |
* | |
* orderby: post_title or post_date; default is post_title | |
* sort: asc or desc; default is asc | |
* format: ul, ol, span, p, div; default is ul | |
* class: additional class to add to the container | |
* divider: the divider between links; for p, span, div formats | |
* exclude: comma-separated list of page slugs to exclude | |
* | |
* Examples: | |
* | |
* List all policies, sorted by title/asc, in an unordered list. | |
* [site-policies] | |
* | |
* List all policies, except those with slugs policy-one and policy-two. | |
* [site-policies exclude="policy-one,policy-two"] | |
* | |
* List all policies, sorted by date/desc, in an ordered list. | |
* [site-policies format="ol" orderby="post_date" sort="desc"] | |
* | |
* List all policies in a <div> with pipes dividing the links. | |
* [site-policies format="div" divider="|"] | |
* | |
* List all policies in a <p> and pass in an extra class. | |
* [site-policies format="p" class="my-class-name"] | |
* | |
* ...or any combinations of the above. | |
* | |
* @author John Alarcon | |
* | |
* @since 1.0.0 | |
* | |
* @param array $atts | |
* @return void|string Marked up list of site policies. | |
*/ | |
function codepotent_policies_shortcode($atts) { | |
// Bring database object into scope. | |
global $wpdb; | |
// SQL order. | |
$orderby = 'post_title'; | |
if (!empty($atts['orderby']) && $atts['orderby'] !== 'post_title') { | |
$orderby = 'post_date'; | |
} | |
// SQL sort. | |
$sort = 'ASC'; | |
if (!empty($atts['sort'])) { | |
if (strtolower($atts['sort']) !== 'asc') { | |
$sort = 'DESC'; | |
} | |
} | |
// Page slugs to exclude, if any. | |
$exclude = (!empty($atts['exclude'])) ? explode(',', $atts['exclude']) : []; | |
// SQL query to grab pages with "policy" in the title or slug. | |
$sql = "SELECT * FROM $wpdb->posts | |
WHERE ($wpdb->posts.post_title LIKE %s OR $wpdb->posts.post_name LIKE %s) | |
AND $wpdb->posts.post_type = 'page' | |
AND $wpdb->posts.post_status = 'publish' | |
ORDER BY $wpdb->posts.$orderby $sort"; | |
// Execute the query. | |
$result = $wpdb->get_results($wpdb->prepare($sql, '%policy%', '%policy%')); | |
// Nothing to show? Return. | |
if (is_wp_error($result) || empty($result)) { | |
return; | |
} | |
// Output format. | |
$format = 'ul'; | |
$formats = ['ul', 'ol', 'span', 'div', 'p']; | |
if (!empty($atts['format']) && in_array($atts['format'], $formats, true)) { | |
$format = $atts['format']; | |
} | |
// Container. | |
$markup = '<'.$format.' class="policy-list'.(!empty($atts['class']) ? ' '.sanitize_title($atts['class']) : '').'">'."\n"; | |
// Policy links. | |
switch($format) { | |
case 'ul': | |
case 'ol': | |
foreach ($result as $policy) { | |
if (!in_array($policy->post_name, $exclude)) { | |
$markup .= '<li><a href="'.get_permalink($policy->ID).'">'.$policy->post_title.'</a></li>'."\n"; | |
} | |
} | |
break; | |
case 'span': | |
case 'p': | |
case 'div': | |
$divider = (!empty($atts['divider'])) ? esc_html($atts['divider']) : '|'; | |
$str = ''; | |
foreach ($result as $policy) { | |
if (!in_array($policy->post_name, $exclude)) { | |
$str .= '<a href="'.get_permalink($policy->ID).'">'.$policy->post_title.'</a>'; | |
$str .= ' '.$divider.' '; | |
} | |
} | |
$markup .= rtrim($str, ' '.$divider.' '); | |
break; | |
} | |
// Container. | |
$markup .= '</'.$format.'><!-- .policy-list'.(!empty($atts['class']) ? '.'.sanitize_title($atts['class']) : '').' -->'."\n"; | |
// Return the markup. | |
return $markup; | |
} | |
// Add the shortcode. | |
add_shortcode('site-policies', 'codepotent_policies_shortcode'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment