/**
  * Create excerpt from the supplied text. Default is the description.
  *
  * Filters:
  *   cn_cat_excerpt_length => change the default excerpt length of 55 words.
  *   cn_cat_excerpt_more  => change the default more string of …
  *   cn_trim_cat_excerpt  => change returned string
  *
  * @access public
  * @since  0.7.8
  *
  * @param  array  $atts [optional]
  * @param  string $text [optional]
  *
  * @return string
  */
 public function getExcerpt($atts = array(), $text = '')
 {
     $defaults = array('length' => apply_filters('cn_cat_excerpt_length', 55), 'more' => apply_filters('cn_cat_excerpt_more', '…'));
     $atts = $this->validate->attributesArray($defaults, $atts);
     $text = cnString::excerpt(empty($text) ? $this->getDescription() : $text, $atts);
     return apply_filters('cn_trim_cat_excerpt', $text);
 }
    /**
     * Returns as an array of objects containing the dates per the defined options.
     *
     * $atts['id'] (int) Retrieve the dates of the specified entry by entry id.
     * $atts['preferred'] (bool) Retrieve the preferred date; id must be supplied.
     * $atts['type'] (array) || (string) Retrieve specific date types, id must be supplied.
     *
     * @param array   $suppliedAttr Accepted values as noted above.
     * @param bool    $returnIDs    Query just the entry IDs or not. If set to FALSE, only the entry IDs would be returned as an array. If set TRUE, the date data will be returned.
     * @return array
     */
    public function dates($suppliedAttr, $returnData = TRUE)
    {
        /** @var wpdb $wpdb */
        global $wpdb, $connections, $current_user;
        get_currentuserinfo();
        $validate = new cnValidate();
        $where[] = 'WHERE 1=1';
        /*
         * // START -- Set the default attributes array. \\
         */
        $defaultAttr['id'] = NULL;
        $defaultAttr['preferred'] = NULL;
        $defaultAttr['type'] = NULL;
        $atts = $validate->attributesArray($defaultAttr, $suppliedAttr);
        /*
         * // END -- Set the default attributes array if not supplied. \\
         */
        extract($atts);
        if (!empty($id)) {
            $where[] = $wpdb->prepare('AND `entry_id` = "%d"', $id);
            if (!empty($preferred)) {
                $where[] = $wpdb->prepare('AND `preferred` = %d', (bool) $preferred);
            }
            if (!empty($type)) {
                if (!is_array($type)) {
                    $type = explode(',', trim($type));
                }
                $where[] = stripslashes($wpdb->prepare('AND `type` IN (\'%s\')', implode("', '", (array) $type)));
            }
        }
        // Set query string for visibility based on user permissions if logged in.
        if (is_user_logged_in()) {
            if (!isset($atts['visibility']) || empty($atts['visibility'])) {
                if (current_user_can('connections_view_public')) {
                    $visibility[] = 'public';
                }
                if (current_user_can('connections_view_private')) {
                    $visibility[] = 'private';
                }
                if (current_user_can('connections_view_unlisted') && is_admin()) {
                    $visibility[] = 'unlisted';
                }
            } else {
                $visibility[] = $atts['visibility'];
            }
        } else {
            if ($connections->options->getAllowPublic()) {
                $visibility[] = 'public';
            }
            if ($atts['allow_public_override'] == TRUE && $connections->options->getAllowPublicOverride()) {
                $visibility[] = 'public';
            }
            if ($atts['private_override'] == TRUE && $connections->options->getAllowPrivateOverride()) {
                $visibility[] = 'private';
            }
        }
        if (!empty($visibility)) {
            $where[] = 'AND `visibility` IN (\'' . implode("', '", (array) $visibility) . '\')';
        }
        if ($returnData) {
            $sql = 'SELECT SQL_CALC_FOUND_ROWS DISTINCT ' . CN_ENTRY_DATE_TABLE . '.*

					FROM ' . CN_ENTRY_DATE_TABLE . ' ' . ' ' . implode(' ', $where) . ' ' . 'ORDER BY `order`';
            //print_r($sql);
            $results = $wpdb->get_results($sql);
        } else {
            $sql = 'SELECT SQL_CALC_FOUND_ROWS DISTINCT ' . CN_ENTRY_DATE_TABLE . '.entry_id

					FROM ' . CN_ENTRY_DATE_TABLE . ' ' . ' ' . implode(' ', $where);
            //print_r($sql);
            $results = $wpdb->get_col($sql);
        }
        if (empty($results)) {
            return array();
        }
        //print_r($results);
        return $results;
    }
 public static function sanitizeImageSettings($settings)
 {
     $validate = new cnValidate();
     $defaults = array('quality' => 80, 'height' => 150, 'width' => 225, 'ratio' => 'crop');
     // Use this instead of wp_parse_args since it doesn't drop invalid atts. NOTE: could use shortcode_atts() instead, I suppose.
     $settings = $validate->attributesArray($defaults, $settings);
     // Ensure positive int values
     $settings['quality'] = absint($settings['quality']);
     $settings['height'] = absint($settings['height']);
     $settings['width'] = absint($settings['width']);
     // If the values is empty, set a default.
     $settings['quality'] = empty($settings['quality']) ? 80 : $settings['quality'];
     $settings['height'] = empty($settings['height']) ? 150 : $settings['height'];
     $settings['width'] = empty($settings['width']) ? 225 : $settings['width'];
     // The valid ratio options
     $ratio = array('crop', 'fill', 'fit', 'none');
     // Make sure the value is one of the permitted options and if it is not, set it to the 'crop' value.
     $settings['ratio'] = in_array($settings['ratio'], $ratio) ? $settings['ratio'] : 'crop';
     return $settings;
 }
Exemple #4
0
    /**
     * @return array
     */
    public function entries($suppliedAttr = array())
    {
        global $wpdb, $connections;
        $validate = new cnValidate();
        $join = array();
        $where[] = 'WHERE 1=1';
        $permittedEntryTypes = array('individual', 'organization', 'family', 'connection_group');
        /*
         * // START -- Set the default attributes array. \\
         */
        $defaultAttr['list_type'] = NULL;
        $defaultAttr['id'] = NULL;
        $defaultAttr['category'] = NULL;
        $defaultAttr['exclude_category'] = NULL;
        $defaultAttr['category_name'] = NULL;
        $defaultAttr['wp_current_category'] = FALSE;
        $defaultAttr['visibility'] = NULL;
        $defaultAttr['allow_public_override'] = FALSE;
        $defaultAttr['private_override'] = FALSE;
        $defaultAttr['limit'] = NULL;
        $defaultAttr['offset'] = NULL;
        $defaultAttr['order_by'] = NULL;
        $atts = $validate->attributesArray($defaultAttr, $suppliedAttr);
        /*
         * // END -- Set the default attributes array if not supplied. \\
         */
        /*
         * If in a post get the category names assigned to the post.
         */
        if ($atts['wp_current_category'] && !is_page()) {
            // Get the current post categories.
            $wpCategories = get_the_category();
            // Build an array of the post categories.
            foreach ($wpCategories as $wpCategory) {
                $wpCategoryNames[] = $wpCategory->cat_name;
            }
        }
        /*
         * Build and array of the supplied category names and their children.
         */
        if (!empty($atts['category_name'])) {
            // If value is a string convert to an array.
            if (!is_array($atts['category_name'])) {
                $atts['category_name'] = explode(',', $atts['category_name']);
            }
            foreach ($atts['category_name'] as $categoryName) {
                // Add the parent category to the array and remove any whitespace from the begining/end of the name in case the user added it when using the shortcode.
                $categoryNames[] = trim($categoryName);
                // Retrieve the children categories
                $results = $this->categoryChildren('name', $categoryName);
                foreach ((array) $results as $term) {
                    $categoryNames[] = $term->name;
                }
            }
        }
        /*
         * Create the string to be used to query entries based on category names. This will merge the
         * category names from the current post categories and the supplied category names and their
         * respective children categories.
         */
        $catNameString = implode("', '", array_merge((array) $wpCategoryNames, (array) $categoryNames));
        unset($wpCategoryNames);
        unset($categoryNames);
        /*
         * Build an array of all the categories and their children based on the supplied category IDs.
         */
        if (!empty($atts['category'])) {
            // If value is a string, string the white space and covert to an array.
            if (!is_array($atts['category'])) {
                $atts['category'] = str_replace(' ', '', $atts['category']);
                $atts['category'] = explode(',', $atts['category']);
            }
            foreach ($atts['category'] as $categoryID) {
                // Add the parent category ID to the array.
                $categoryIDs[] = $categoryID;
                // Retrieve the children categories
                $results = $this->categoryChildren('term_id', $categoryID);
                foreach ((array) $results as $term) {
                    $categoryIDs[] = $term->term_id;
                }
            }
            /*
             * Exclude the specified categories by ID.
             */
            if (!empty($atts['exclude_category'])) {
                // If value is a string, string the white space and covert to an array.
                if (!is_array($atts['exclude_category'])) {
                    $atts['exclude_category'] = str_replace(' ', '', $atts['exclude_category']);
                    $atts['exclude_category'] = explode(',', $atts['exclude_category']);
                }
                $categoryIDs = array_diff($categoryIDs, $atts['exclude_category']);
            }
            /*
             * Create the string to be used to query entries based on category IDs.
             */
            $catIDString = implode("', '", $categoryIDs);
            unset($categoryIDs);
        }
        if (!empty($catIDString) || !empty($catNameString)) {
            // Set the query string to INNER JOIN the term relationship and taxonomy tables.
            $join[] = 'INNER JOIN ' . CN_TERM_RELATIONSHIP_TABLE . ' ON ( ' . CN_ENTRY_TABLE . '.id = ' . CN_TERM_RELATIONSHIP_TABLE . '.entry_id )';
            $join[] = 'INNER JOIN ' . CN_TERM_TAXONOMY_TABLE . ' ON ( ' . CN_TERM_RELATIONSHIP_TABLE . '.term_taxonomy_id = ' . CN_TERM_TAXONOMY_TABLE . '.term_taxonomy_id )';
            $join[] = 'INNER JOIN ' . CN_TERMS_TABLE . ' ON ( ' . CN_TERMS_TABLE . '.term_id = ' . CN_TERM_TAXONOMY_TABLE . '.term_id )';
            // Set the query string to return entries within the category taxonomy.
            $where[] = 'AND ' . CN_TERM_TAXONOMY_TABLE . '.taxonomy = \'category\'';
            if (!empty($catIDString)) {
                $where[] = 'AND ' . CN_TERM_TAXONOMY_TABLE . '.term_id IN (\'' . $catIDString . '\')';
            }
            if (!empty($catNameString)) {
                $where[] = 'AND ' . CN_TERMS_TABLE . '.name IN (\'' . $catNameString . '\')';
            }
        }
        // Convert the supplied IDs $atts['id'] to an array.
        if (!is_array($atts['id']) && !empty($atts['id'])) {
            // Trim the space characters if present.
            $atts['id'] = str_replace(' ', '', $atts['id']);
            // Convert to array.
            $atts['id'] = explode(',', $atts['id']);
        }
        // Set query string to return specific entries.
        if (!empty($atts['id'])) {
            $where[] = 'AND `id` IN (\'' . implode("', '", (array) $atts['id']) . '\')';
        }
        // Convert the supplied entry types $atts['list_type'] to an array.
        if (!is_array($atts['list_type']) && !empty($atts['list_type'])) {
            // Trim the space characters if present.
            $atts['list_type'] = str_replace(' ', '', $atts['list_type']);
            // Convert to array.
            $atts['list_type'] = explode(',', $atts['list_type']);
        }
        // Set query string for entry type.
        if (!empty($atts['list_type']) && (bool) array_intersect($atts['list_type'], $permittedEntryTypes)) {
            // Capatibility code to make sure any ocurrences of the deprecated entry type connections_group is changed to entry type family
            $atts['list_type'] = str_replace('connection_group', 'family', $atts['list_type']);
            $where[] = 'AND `entry_type` IN (\'' . implode("', '", (array) $atts['list_type']) . '\')';
        }
        // Set query string for visibility based on user permissions if logged in.
        if (is_user_logged_in()) {
            if (!$atts['visibility']) {
                if (current_user_can('connections_view_public')) {
                    $visibility[] = 'public';
                }
                if (current_user_can('connections_view_private')) {
                    $visibility[] = 'private';
                }
                if (current_user_can('connections_view_unlisted') && is_admin()) {
                    $visibility[] = 'unlisted';
                }
            } else {
                $visibility[] = $atts['visibility'];
            }
        } else {
            if ($connections->options->getAllowPublic()) {
                $visibility[] = 'public';
            }
            if ($atts['allow_public_override'] == TRUE && $connections->options->getAllowPublicOverride()) {
                $visibility[] = 'public';
            }
            if ($atts['private_override'] == TRUE && $connections->options->getAllowPrivateOverride()) {
                $visibility[] = 'private';
            }
        }
        $where[] = 'AND `visibility` IN (\'' . implode("', '", (array) $visibility) . '\')';
        empty($atts['limit']) ? $limit = NULL : ($limit = ' LIMIT ' . $atts['limit'] . ' ');
        empty($atts['offset']) ? $offset = NULL : ($offset = ' OFFSET ' . $atts['offset'] . ' ');
        $sql = 'SELECT DISTINCT ' . CN_ENTRY_TABLE . '.*,
				
				CASE `entry_type`
				  WHEN \'individual\' THEN `last_name`
				  WHEN \'organization\' THEN `organization`
				  WHEN \'connection_group\' THEN `family_name`
				  WHEN \'family\' THEN `family_name`
				END AS `sort_column`
				 
				FROM ' . CN_ENTRY_TABLE . ' ' . implode(' ', $join) . ' ' . implode(' ', $where) . ' ' . 'ORDER BY `sort_column`, `last_name`, `first_name`  ' . $limit . $offset;
        $results = $wpdb->get_results($sql);
        $connections->lastQuery = $wpdb->last_query;
        $connections->lastQueryError = $wpdb->last_error;
        $connections->lastInsertID = $wpdb->insert_id;
        $connections->resultCount = $wpdb->num_rows;
        $connections->recordCount = $this->recordCount($atts['allow_public_override'], $atts['private_override']);
        return $this->orderBy($results, $atts['order_by'], $atts['id']);
    }