/**
  * This can be called to turn on/off the filters applied in cnSEO.
  *
  * @access public
  * @since  0.7.8
  * @static
  *
  * @param  bool $do
  *
  * @return void
  */
 public static function doFilterPermalink($do = TRUE)
 {
     self::$filterPermalink = $do;
 }
 /**
  * Create a permalink.
  *
  * @todo Should check to ensure require params are set and valid and pass back WP_Error if they are not.
  * @todo The type case statement should have a default with an filter attached so this can be pluggable.
  *
  * NOTE: When the `name` permalink is being requested, use the entry slug.
  *       It is saved id the db as URL encoded. If any other strings pass
  *       for the `name` permalink must be URL encoded.
  *       All the other permalink types will be URL encoded in this method
  *       so pass stings without URL encoding.
  *
  * @access private
  * @since  0.7.3
  * @static
  *
  * @global $wp_rewrite
  * @global $post
  *
  * @uses   is_admin()
  * @uses   wp_parse_args()
  * @uses   get_option()
  * @uses   in_the_loop()
  * @uses   is_page()
  * @uses   trailingslashit()
  * @uses   get_permalink()
  * @uses   add_query_arg()
  * @uses   is_front_page()
  *
  * @param array $atts {
  *     Optional. An array of arguments.
  *
  *     @type string $class      The class to give the anchor.
  *                              Default: ''
  *     @type string $text       The anchor text.
  *                              Default: ''
  *     @type string $title      The anchor title attribute.
  *                              Default: ''
  *     @type bool   $follow     Whether or not the anchor rel should be follow or nofollow
  *                              Default: TRUE
  *     @type string $rel        The rel attribute.
  *                              Default: ''
  *     @type string $slug       The slug of the object to build the permalink for. ie. the entry slug or term slug.
  *                              Default: ''
  *     @type string $on_click   The inline javascript on_click attribute.
  *                              Default: ''
  *     @type string $type       The type of permalink to create. ie. name, edit, home
  *                              Default: 'name'
  *     @type int    $home_id    The page ID of the directory home.
  *                              Default: The page set as the Directory Home Page.
  *     @type bool   $force_home Whether or not to for the page ID to the page ID of the page set as the Directory Home Page.
  *                              Default: FALSE
  *     @type string $data       What to return.
  *                              Default: tag
  *                              Accepts: tag | url
  *     @type bool   $return     Whether or not to return or echo the permalink.
  *                              Default: FALSE
  * }
  *
  * @return string
  */
 public static function permalink($atts)
 {
     /** @var $wp_rewrite WP_Rewrite */
     global $wp_rewrite, $post;
     // The class.seo.file is only loaded in the frontend; do not attempt to remove the filter
     // otherwise it'll cause an error.
     if (!is_admin()) {
         cnSEO::doFilterPermalink(FALSE);
     }
     // The anchor attributes.
     $piece = array();
     $defaults = array('class' => '', 'text' => '', 'title' => '', 'follow' => TRUE, 'rel' => '', 'slug' => '', 'on_click' => '', 'type' => 'name', 'home_id' => cnSettingsAPI::get('connections', 'connections_home_page', 'page_id'), 'force_home' => FALSE, 'data' => 'tag', 'return' => FALSE);
     $atts = wp_parse_args($atts, $defaults);
     // Get the directory home page ID.
     $homeID = $atts['force_home'] ? cnSettingsAPI::get('connections', 'connections_home_page', 'page_id') : $atts['home_id'];
     // Get the settings for the base of each data type to be used in the URL.
     $base = get_option('connections_permalink');
     // Create the permalink base based on context where the entry is being displayed.
     if (in_the_loop() && is_page()) {
         if ($wp_rewrite->using_permalinks()) {
             // Only slash it when using pretty permalinks.
             $permalink = trailingslashit(get_permalink($homeID));
         } else {
             $permalink = get_permalink($homeID);
             // If the current page is the front page, the `page_id` query var must be added because without it, WP
             // will redirect to the blog/posts home page.
             if (is_front_page()) {
                 $permalink = add_query_arg('page_id', $post->ID, $permalink);
             }
         }
     } else {
         // If using pretty permalinks get the directory home page ID and slash it, otherwise just add the page_id to the query string.
         if ($wp_rewrite->using_permalinks()) {
             $permalink = trailingslashit(get_permalink($homeID));
         } else {
             $permalink = add_query_arg(array('page_id' => $homeID, 'p' => FALSE), get_permalink($homeID));
         }
     }
     if (!empty($atts['class'])) {
         $piece['class'] = 'class="' . $atts['class'] . '"';
     }
     // if ( ! empty( $atts['slug'] ) ) $piece['id']        = 'id="' . $atts['slug'] .'"';
     if (!empty($atts['title'])) {
         $piece['title'] = 'title="' . $atts['title'] . '"';
     }
     if (!empty($atts['target'])) {
         $piece['target'] = 'target="' . $atts['target'] . '"';
     }
     if (!$atts['follow']) {
         $piece['follow'] = 'rel="nofollow"';
     }
     if (!empty($atts['rel'])) {
         $piece['rel'] = 'rel="' . $atts['rel'] . '"';
     }
     if (!empty($atts['on_click'])) {
         $piece['on_click'] = 'onClick="' . $atts['on_click'] . '"';
     }
     switch ($atts['type']) {
         case 'home':
             break;
         case 'all':
             if ($wp_rewrite->using_permalinks()) {
                 $permalink = trailingslashit($permalink . 'view/all/');
             } else {
                 $permalink = add_query_arg('cn-view', 'all', $permalink);
             }
             break;
         case 'submit':
             if ($wp_rewrite->using_permalinks()) {
                 $permalink = trailingslashit($permalink . 'submit/');
             } else {
                 $permalink = add_query_arg('cn-view', 'submit', $permalink);
             }
             break;
         case 'edit':
             if ($wp_rewrite->using_permalinks()) {
                 // The entry slug is saved in the db urlencoded so we'll expect when the permalink for entry name is
                 // requested that the entry slug is being used so urlencode() will not be use as not to double encode it.
                 $permalink = trailingslashit($permalink . $base['name_base'] . '/' . $atts['slug'] . '/edit');
             } else {
                 $permalink = add_query_arg(array('cn-entry-slug' => $atts['slug'], 'cn-view' => 'detail', 'cn-process' => 'edit'), $permalink);
             }
             break;
         case 'name':
             if ($wp_rewrite->using_permalinks()) {
                 // The entry slug is saved in the db urlencoded so we'll expect when the permalink for entry name is
                 // requested that the entry slug is being used so urlencode() will not be use as not to double encode it.
                 $permalink = trailingslashit($permalink . $base['name_base'] . '/' . $atts['slug']);
             } else {
                 $permalink = add_query_arg(array('cn-entry-slug' => $atts['slug'], 'cn-view' => 'detail'), $permalink);
             }
             break;
         case 'detail':
             if ($wp_rewrite->using_permalinks()) {
                 $permalink = trailingslashit($permalink . $base['name_base'] . '/' . $atts['slug'] . '/detail');
             } else {
                 $permalink = add_query_arg(array('cn-entry-slug' => $atts['slug'], 'cn-view' => 'detail'), $permalink);
             }
             break;
         case 'department':
             if ($wp_rewrite->using_permalinks()) {
                 $permalink = trailingslashit($permalink . $base['department_base'] . '/' . urlencode($atts['slug']));
             } else {
                 $permalink = add_query_arg('cn-department', urlencode($atts['slug']), $permalink);
             }
             break;
         case 'organization':
             if ($wp_rewrite->using_permalinks()) {
                 $permalink = trailingslashit($permalink . $base['organization_base'] . '/' . urlencode($atts['slug']));
             } else {
                 $permalink = add_query_arg('cn-organization', urlencode($atts['slug']), $permalink);
             }
             break;
         case 'category':
             if ($wp_rewrite->using_permalinks()) {
                 $permalink = trailingslashit($permalink . $base['category_base'] . '/' . $atts['slug']);
             } else {
                 $permalink = add_query_arg('cn-cat-slug', $atts['slug'], $permalink);
             }
             break;
         case 'district':
             if ($wp_rewrite->using_permalinks()) {
                 $permalink = trailingslashit($permalink . $base['district_base'] . '/' . urlencode($atts['slug']));
             } else {
                 $permalink = add_query_arg('cn-district', urlencode($atts['slug']), $permalink);
             }
             break;
         case 'county':
             if ($wp_rewrite->using_permalinks()) {
                 $permalink = trailingslashit($permalink . $base['county_base'] . '/' . urlencode($atts['slug']));
             } else {
                 $permalink = add_query_arg('cn-county', urlencode($atts['slug']), $permalink);
             }
             break;
         case 'locality':
             if ($wp_rewrite->using_permalinks()) {
                 $permalink = trailingslashit($permalink . $base['locality_base'] . '/' . urlencode($atts['slug']));
             } else {
                 $permalink = add_query_arg('cn-locality', urlencode($atts['slug']), $permalink);
             }
             break;
         case 'region':
             if ($wp_rewrite->using_permalinks()) {
                 $permalink = trailingslashit($permalink . $base['region_base'] . '/' . urlencode($atts['slug']));
             } else {
                 $permalink = add_query_arg('cn-region', urlencode($atts['slug']), $permalink);
             }
             break;
         case 'postal_code':
             if ($wp_rewrite->using_permalinks()) {
                 $permalink = trailingslashit($permalink . $base['postal_code_base'] . '/' . urlencode($atts['slug']));
             } else {
                 $permalink = add_query_arg('cn-postal-code', urlencode($atts['slug']), $permalink);
             }
             break;
         case 'country':
             if ($wp_rewrite->using_permalinks()) {
                 $permalink = trailingslashit($permalink . $base['country_base'] . '/' . urlencode($atts['slug']));
             } else {
                 $permalink = add_query_arg('cn-country', urlencode($atts['slug']), $permalink);
             }
             break;
         case 'character':
             if ($wp_rewrite->using_permalinks()) {
                 $permalink = trailingslashit($permalink . $base['character_base'] . '/' . urlencode($atts['slug']));
             } else {
                 $permalink = add_query_arg(array('cn-char' => urlencode($atts['slug'])), $permalink);
             }
             break;
         default:
             if (has_filter("cn_permalink-{$atts['type']}")) {
                 /**
                  * Allow extensions to add custom permalink types.
                  *
                  * The variable portion of the filter name if the permalink type being created.
                  *
                  * @since 8.5.17
                  *
                  * @param string $permalink The permalink.
                  * @param array  $atts      The attributes array.
                  */
                 $permalink = apply_filters("cn_permalink-{$atts['type']}", $permalink, $atts);
             }
             break;
     }
     $permalink = apply_filters('cn_permalink', $permalink, $atts);
     if ('url' == $atts['data']) {
         $out = esc_url($permalink);
     } else {
         $piece['href'] = 'href="' . esc_url($permalink) . '"';
         $out = '<a ' . implode(' ', $piece) . '>' . $atts['text'] . '</a>';
     }
     // The class.seo.file is only loaded in the frontend; do not attempt to add the filter
     // otherwise it'll cause an error.
     if (!is_admin()) {
         cnSEO::doFilterPermalink(TRUE);
     }
     if ($atts['return']) {
         return $out;
     }
     echo $out;
 }
 /**
  * Creates the pagination controls.
  *
  * @access public
  * @since  0.7.3
  * @static
  *
  * @param array $atts {
  *     Optional. Array or string of arguments for generating paginated links for archives.
  *
  *     @type int    $limit              The number of entries per page to be displayed.
  *                                      Default: 20
  *     @type bool   $show_all           Whether to show all pages.
  *                                      Default: FALSE
  *     @type int    $end_size           How many numbers on either the start and the end list edges.
  *                                      Default: 2
  *     @type int    $mid_size           How many numbers to either side of the current pages.
  *                                      Default: 2
  *     @type bool   $prev_next          Whether to include the previous and next links in the list.
  *                                      Default: TRUE
  *     @type bool   $prev_text          The previous page text.
  *                                      Default: '«'
  *     @type bool   $next_text          The next page text.
  *                                      Default: '«'
  *     @type string $add_fragment       A string to append to each link.
  *                                      Default: empty
  *     @type string $before_page_number A string to appear before the page number.
  *                                      Default: empty
  *     @type string $after_page_number  A string to append after the page number.
  *                                      Default: empty
  *     @type bool   $return             Whether or not to return or echo the pagination control. Set to TRUE to return instead of echo.
  *                                      Default: FALSE
  * }
  *
  * @return string
  */
 public static function pagination($atts = array())
 {
     /**
      * @var WP_Rewrite $wp_rewrite
      * @var WP_Query $wp_query
      * @var connectionsLoad $connections
      */
     global $wp_rewrite, $wp_query, $connections;
     // The class.seo.file is only loaded in the frontend; do not attempt to remove the filter
     // otherwise it'll cause an error.
     if (!is_admin()) {
         cnSEO::doFilterPermalink(FALSE);
     }
     $out = '';
     $defaults = array('limit' => 20, 'show_all' => FALSE, 'end_size' => 2, 'mid_size' => 2, 'prev_next' => TRUE, 'prev_text' => __('&laquo;', 'connections'), 'next_text' => __('&raquo;', 'connections'), 'add_fragment' => '', 'before_page_number' => '', 'after_page_number' => '', 'return' => FALSE);
     /**
      * Filter the default attributes array.
      *
      * @since 8.5.14
      */
     $defaults = apply_filters('cn_pagination_default_atts', $defaults);
     $atts = wp_parse_args($atts, $defaults);
     /**
      * Filter the user supplied attributes.
      *
      * @since 8.5.14
      */
     $atts = apply_filters('cn_pagination_atts', $atts);
     $total = $connections->retrieve->resultCountNoLimit;
     $pageCount = absint($atts['limit']) ? ceil($total / $atts['limit']) : 0;
     if ($pageCount > 1) {
         $current = 1;
         $queryVars = array();
         // Get page/post permalink.
         // Only slash it when using pretty permalinks.
         $permalink = $wp_rewrite->using_permalinks() ? trailingslashit(get_permalink()) : get_permalink();
         // Get the settings for the base of each data type to be used in the URL.
         $base = get_option('connections_permalink');
         // Store the query vars
         if (cnQuery::getVar('cn-s')) {
             $queryVars['cn-s'] = urlencode(cnQuery::getVar('cn-s'));
         }
         if (cnQuery::getVar('cn-char')) {
             $queryVars['cn-char'] = cnQuery::getVar('cn-char');
         }
         if (cnQuery::getVar('cn-cat')) {
             $queryVars['cn-cat'] = cnQuery::getVar('cn-cat');
         }
         if (cnQuery::getVar('cn-organization')) {
             $queryVars['cn-organization'] = cnQuery::getVar('cn-organization');
         }
         if (cnQuery::getVar('cn-department')) {
             $queryVars['cn-department'] = cnQuery::getVar('cn-department');
         }
         if (cnQuery::getVar('cn-locality')) {
             $queryVars['cn-locality'] = cnQuery::getVar('cn-locality');
         }
         if (cnQuery::getVar('cn-region')) {
             $queryVars['cn-region'] = cnQuery::getVar('cn-region');
         }
         if (cnQuery::getVar('cn-postal-code')) {
             $queryVars['cn-postal-code'] = cnQuery::getVar('cn-postal-code');
         }
         if (cnQuery::getVar('cn-country')) {
             $queryVars['cn-country'] = cnQuery::getVar('cn-country');
         }
         if (cnQuery::getVar('cn-near-coord')) {
             $queryVars['cn-near-coord'] = cnQuery::getVar('cn-near-coord');
         }
         if (cnQuery::getVar('cn-radius')) {
             $queryVars['cn-radius'] = cnQuery::getVar('cn-radius');
         }
         if (cnQuery::getVar('cn-unit')) {
             $queryVars['cn-unit'] = cnQuery::getVar('cn-unit');
         }
         if (is_front_page() && cnQuery::getVar('page_id')) {
             $queryVars['page_id'] = cnQuery::getVar('page_id');
         }
         // Current page
         if (cnQuery::getVar('cn-pg')) {
             $current = absint(cnQuery::getVar('cn-pg'));
         }
         /*
          * Create the page permalinks. If on a post or custom post type, use query vars.
          */
         if (is_page() && $wp_rewrite->using_permalinks()) {
             // Add the category base and path if paging thru a category.
             if (cnQuery::getVar('cn-cat-slug')) {
                 $permalink = trailingslashit($permalink . $base['category_base'] . '/' . cnQuery::getVar('cn-cat-slug'));
             }
             // Add the organization base and path if paging thru a organization.
             if (cnQuery::getVar('cn-organization')) {
                 $permalink = trailingslashit($permalink . $base['organization_base'] . '/' . cnQuery::getVar('cn-organization'));
             }
             // Add the department base and path if paging thru a department.
             if (cnQuery::getVar('cn-department')) {
                 $permalink = trailingslashit($permalink . $base['department_base'] . '/' . cnQuery::getVar('cn-department'));
             }
             // Add the locality base and path if paging thru a locality.
             if (cnQuery::getVar('cn-locality')) {
                 $permalink = trailingslashit($permalink . $base['locality_base'] . '/' . cnQuery::getVar('cn-locality'));
             }
             // Add the region base and path if paging thru a region.
             if (cnQuery::getVar('cn-region')) {
                 $permalink = trailingslashit($permalink . $base['region_base'] . '/' . cnQuery::getVar('cn-region'));
             }
             // Add the postal code base and path if paging thru a postal code.
             if (cnQuery::getVar('cn-postal-code')) {
                 $permalink = trailingslashit($permalink . $base['postal_code_base'] . '/' . cnQuery::getVar('cn-postal-code'));
             }
             // Add the country base and path if paging thru a country.
             if (cnQuery::getVar('cn-country')) {
                 $permalink = trailingslashit($permalink . $base['country_base'] . '/' . cnQuery::getVar('cn-country'));
             }
             $args = array('base' => $permalink . '%_%', 'format' => user_trailingslashit('pg/%#%', 'cn-paged'), 'total' => $pageCount, 'current' => $current, 'show_all' => $atts['show_all'], 'end_size' => $atts['end_size'], 'mid_size' => $atts['mid_size'], 'prev_next' => $atts['prev_next'], 'prev_text' => $atts['prev_text'], 'next_text' => $atts['next_text'], 'type' => 'array', 'add_args' => $queryVars, 'add_fragment' => $atts['add_fragment'], 'before_page_number' => $atts['before_page_number'], 'after_page_number' => $atts['after_page_number']);
             $links = paginate_links($args);
         } else {
             if ($wp_rewrite->using_permalinks()) {
                 $atts['format'] = '?cn-pg=%#%';
             } elseif (isset($wp_query->query) && !empty($wp_query->query) && !is_front_page()) {
                 $atts['format'] = '&cn-pg=%#%';
             } elseif (isset($wp_query->query) && !empty($wp_query->query) && is_front_page()) {
                 $atts['format'] = '?cn-pg=%#%';
             } else {
                 $atts['format'] = '?cn-pg=%#%';
             }
             $args = array('base' => $permalink . '%_%', 'format' => $atts['format'], 'total' => $pageCount, 'current' => $current, 'show_all' => $atts['show_all'], 'end_size' => $atts['end_size'], 'mid_size' => $atts['mid_size'], 'prev_next' => $atts['prev_next'], 'prev_text' => $atts['prev_text'], 'next_text' => $atts['next_text'], 'type' => 'array', 'add_args' => $queryVars, 'add_fragment' => $atts['add_fragment'], 'before_page_number' => $atts['before_page_number'], 'after_page_number' => $atts['after_page_number']);
             $links = paginate_links($args);
         }
         $out .= '<span class="cn-page-nav" id="cn-page-nav">';
         $out .= join(PHP_EOL, $links);
         $out .= '</span>';
     }
     // The class.seo.file is only loaded in the frontend; do not attempt to add the filter
     // otherwise it'll cause an error.
     if (!is_admin()) {
         cnSEO::doFilterPermalink();
     }
     return self::echoOrReturn($atts['return'], $out);
 }
 /**
  * Outputs the vCard download permalink.
  *
  * Accepted attributes for the $atts array are:
  *  class (string) The link class attribute.
  *  text (string) The anchor text.
  *  title (string) The link title attribute.
  *  format (string) The tokens to use to display the vcard link block parts.
  *   Permitted Tokens:
  *    %text%
  *    %icon%
  *  follow (bool) Add add the rel="nofollow" attribute if set to FALSE
  *  size (int) The icon size. Valid values are: 16, 24, 32, 48
  *  slug (string) The entry's slug ID.
  *  before (string) HTML to output before the email addresses.
  *  after (string) HTML to after before the email addresses.
  *  return (bool) Return string if set to TRUE instead of echo string.
  *
  * @access public
  * @since 0.7.3
  * @version 1.0
  * @uses wp_parse_args()
  * @param array   $atts [optional]
  * @return string
  */
 public function vcard($atts = array())
 {
     /**
      * @var wp_rewrite $wp_rewrite
      * @var connectionsLoad $connections
      */
     global $wp_rewrite, $connections;
     // The class.seo.file is only loaded in the frontend; do not attempt to remove the filter
     // otherwise it'll cause an error.
     if (!is_admin()) {
         cnSEO::doFilterPermalink(FALSE);
     }
     $base = get_option('connections_permalink');
     $name = $base['name_base'];
     $homeID = $connections->settings->get('connections', 'home_page', 'page_id');
     // Get the directory home page ID.
     $piece = array();
     $id = FALSE;
     $token = FALSE;
     $iconSizes = array(16, 24, 32, 48);
     $search = array('%text%', '%icon%');
     $replace = array();
     // These are values will need to be added to the query string in order to download unlisted entries from the admin.
     if ('unlisted' === $this->getVisibility()) {
         $id = $this->getId();
         $token = wp_create_nonce('download_vcard_' . $this->getId());
     }
     $defaults = array('class' => '', 'text' => __('Add to Address Book.', 'connections'), 'title' => __('Download vCard', 'connections'), 'format' => '', 'size' => 24, 'follow' => FALSE, 'slug' => '', 'before' => '', 'after' => '', 'return' => FALSE);
     $atts = wp_parse_args($atts, $defaults);
     /*
      * Ensure the supplied size is valid, if not reset to the default value.
      */
     $iconSize = in_array($atts['size'], $iconSizes) ? $atts['size'] : 32;
     // Create the permalink base based on context where the entry is being displayed.
     if (in_the_loop() && is_page()) {
         $permalink = trailingslashit(get_permalink());
     } else {
         $permalink = trailingslashit(get_permalink($homeID));
     }
     if (!empty($atts['class'])) {
         $piece[] = 'class="' . $atts['class'] . '"';
     }
     if (!empty($atts['slug'])) {
         $piece[] = 'id="' . $atts['slug'] . '"';
     }
     if (!empty($atts['title'])) {
         $piece[] = 'title="' . $atts['title'] . '"';
     }
     if (!empty($atts['target'])) {
         $piece[] = 'target="' . $atts['target'] . '"';
     }
     if (!$atts['follow']) {
         $piece[] = 'rel="nofollow"';
     }
     if ($wp_rewrite->using_permalinks()) {
         $piece[] = 'href="' . esc_url(add_query_arg(array('cn-id' => $id, 'cn-token' => $token), $permalink . $name . '/' . $this->getSlug() . '/vcard/')) . '"';
     } else {
         $piece[] = 'href="' . esc_url(add_query_arg(array('cn-entry-slug' => $this->getSlug(), 'cn-process' => 'vcard', 'cn-id' => $id, 'cn-token' => $token), $permalink)) . '"';
     }
     $out = '<span class="vcard-block">';
     $replace[] = '<a ' . implode(' ', $piece) . '>' . $atts['text'] . '</a>';
     $replace[] = '<a ' . implode(' ', $piece) . '><image src="' . esc_url(CN_URL . 'assets/images/icons/vcard/vcard_' . $iconSize . '.png') . '" height="' . $iconSize . 'px" width="' . $iconSize . 'px"/></a>';
     $out .= str_ireplace($search, $replace, empty($atts['format']) ? '%text%' : $atts['format']);
     $out .= '</span>';
     // The class.seo.file is only loaded in the frontend; do not attempt to add the filter
     // otherwise it'll cause an error.
     if (!is_admin()) {
         cnSEO::doFilterPermalink();
     }
     $out = $atts['before'] . $out . $atts['after'] . PHP_EOL;
     return $this->echoOrReturn($atts['return'], $out);
 }