private function editform($quote_id = 0) { $form_name = "quotescollection_addquote"; $action_url = $this->admin_add_new_url; // If the new quote submitted is added to the database, leave the fields blank if (!$quote_id && $this->quote_added) { $quote = $author = $source = $tags = ""; $public_selected = ' checked="checked"'; } else { $quote = isset($_REQUEST['quote']) && trim($_REQUEST['quote']) ? stripslashes(htmlspecialchars(trim($_REQUEST['quote']))) : ""; $author = isset($_REQUEST['author']) && trim($_REQUEST['author']) ? stripslashes(htmlspecialchars(trim($_REQUEST['author']))) : ""; $source = isset($_REQUEST['source']) && trim($_REQUEST['source']) ? stripslashes(htmlspecialchars(trim($_REQUEST['source']))) : ""; $tags = isset($_REQUEST['tags']) && trim($_REQUEST['tags']) ? stripslashes(htmlspecialchars(trim($_REQUEST['tags']))) : ""; $public_selected = !isset($_REQUEST['public']) && ($quote || $author || $source || $tags) ? "" : " checked=\"checked\""; } $submit_button = get_submit_button(_x('Add Quote', 'submit button text', 'quotes-collection'), 'primary large', 'submit', false); $nonce_action_name = 'add_quote'; $hidden_input = ""; if ($quote_id && is_numeric($quote_id)) { $form_name = "quotescollection_editquote"; if (!$quote && !$author && !$source && !$tags) { $quote_data = Quotes_Collection_Quote::with_id($quote_id); $quote = htmlspecialchars($quote_data->quote); $author = htmlspecialchars($quote_data->author); $source = htmlspecialchars($quote_data->source); $tags = implode(', ', explode(',', $quote_data->tags)); if ('no' == $quote_data->public) { $public_selected = ""; } } $hidden_input = "<input type=\"hidden\" name=\"quote_id\" value=\"{$quote_id}\" />"; $submit_button = get_submit_button(_x('Save Changes', 'submit button text', 'quotes-collection'), 'primary large', 'submit', false); $action_url = $this->admin_url . '&action=edit&id=' . $quote_id; $nonce_action_name = 'save_changes_' . $quote_id; } $hidden_input .= wp_nonce_field($nonce_action_name, 'quotescollection_nonce', true, false); $quote_label = __('Quote', 'quotes-collection'); $author_label = __('Author', 'quotes-collection'); $source_label = __('Source', 'quotes-collection'); $tags_label = __('Tags', 'quotes-collection'); $public_label = __('Public?', 'quotes-collection'); $optional_text = __('optional', 'quotes-collection'); $comma_separated_text = __('comma separated', 'quotes-collection'); $display = <<<EDITFORM \t\t<div class="form-wrap"> <form name="{$form_name}" method="post" action="{$action_url}"> \t{$hidden_input} \t\t<div class="form-field form-required"> \t\t\t<label for="quotescollection_quote"><strong>{$quote_label}</strong></label> \t\t\t<textarea id="quotescollection_quote" name="quote">{$quote}</textarea> \t\t</div> \t\t<div class="form-field"> \t\t\t<label for="quotescollection_author"><strong>{$author_label}</strong></label> \t\t\t<input type="text" id="quotescollection_author" name="author" value="{$author}" /> \t\t\t<p>{$optional_text}</p> \t\t</div> \t\t<div class="form-field"> \t\t\t<label for="quotescollection_source"><strong>{$source_label}</strong></label> \t\t\t<input type="text" id="quotescollection_source" name="source" value="{$source}" /><br /> \t\t\t<p>{$optional_text}</p> \t\t</div> \t\t<div class="form-field"> \t\t\t<label for="quotescollection_tags"><strong>{$tags_label}</strong></label> \t\t\t<input type="text" id="quotescollection_tags" name="tags" value="{$tags}" /><br /> \t\t\t<p>{$optional_text}, {$comma_separated_text}</p> \t\t</div> \t\t<div class="form-field"> \t\t\t<label for="quotescollection_public"><strong>{$public_label}</strong> \t\t\t<input type="checkbox" id="quotescollection_public" name="public"{$public_selected} /> \t\t\t</label> \t\t</div> \t<div class="form-field">{$submit_button}</div> </form> </div> EDITFORM; return $display; }
function output_format($items, $highlight = "") { foreach ($items as $key => $quote_data) { $quote = new Quotes_Collection_Quote($quote_data); $quote->prepare_data(); $quote_array = (array) $quote; if ($highlight) { $quote_array = preg_replace("/{$highlight}/i", "<span class='highlight'>\$0</span>", $quote_array); unset($quote_array['quote_id']); unset($quote_array['public']); unset($quote_array['time_added']); unset($quote_array['time_updated']); } $items[$key] = array_merge((array) $quote, $quote_array); } return $items; }
/** * Function to display a random (or not so random) quote with (or without) * ajax refresh functionality. Used by the 'quotescollection_quote' template * function and the 'Random Quote' widget. */ public function quote($args = array()) { global $quotescollection_db; $args_default = array('instance' => '', 'show_author' => 1, 'show_source' => 0, 'ajax_refresh' => 1, 'random' => 1, 'auto_refresh' => 0, 'tags' => '', 'char_limit' => 500, 'echo' => 1); // Merge with default values $args = array_merge($args_default, $args); $instance = is_string($args['instance']) ? $args['instance'] : ''; $show_author = false == $args['show_author'] ? 0 : 1; $show_source = true == $args['show_source'] ? 1 : 0; $ajax_refresh = false == $args['ajax_refresh'] ? 0 : 1; $auto_refresh = 0; if ($args['auto_refresh']) { if (is_numeric($args['auto_refresh'])) { $auto_refresh = $args['auto_refresh']; } else { if (true === $args['auto_refresh']) { $auto_refresh = 5; } } } $random = false == $args['random'] ? 0 : 1; // Tags can only be passed as a string, comma separated $tags = is_string($args['tags']) ? $args['tags'] : ''; // Only a numeral can be passed as char_limit $char_limit = is_numeric($args['char_limit']) ? $args['char_limit'] : 500; // By default, we fetch a random quote $orderby = 'random'; if (!$random) { $orderby = 'quote_id'; } // Frame the conditions to fetch the quote $condition = array('orderby' => $orderby, 'order' => 'DESC', 'tags' => $tags, 'char_limit' => $char_limit, 'public' => 'yes'); if (!($count = $quotescollection_db->count($condition))) { return ""; } $dynamic_fetch = 0; if ('random' == $orderby && ($options = get_option('quotescollection')) && isset($options['dynamic_fetch']) && $options['dynamic_fetch'] == 'on' && $count > 1) { $dynamic_fetch = 1; } $display = ""; // And fetch the quote, only if dynamic fetch is off if (!$dynamic_fetch) { if ($quote = Quotes_Collection_Quote::with_condition($condition)) { $curr_quote_id = $quote->quote_id; $display = $quote->output_format(array('show_author' => $show_author, 'show_source' => $show_source, 'before' => '', 'after' => '')); if (!$display) { return ""; } } else { return ""; } } else { $curr_quote_id = 0; } // Refresh functionality is included only when there is more than one quote for the condition // And instance ID is required for the refresh functionality if ($count > 1 && $instance) { if ($dynamic_fetch || $ajax_refresh) { $display .= "<script type=\"text/javascript\">\n"; $display .= 'var args_' . $instance . ' = {' . '"instanceID":"' . $instance . '", ' . '"currQuoteID":' . $curr_quote_id . ', ' . '"showAuthor":' . $show_author . ', ' . '"showSource":' . $show_source . ', ' . '"tags":"' . $tags . '", ' . '"charLimit":' . $char_limit . ', ' . '"orderBy":"' . $orderby . '", ' . '"ajaxRefresh":' . $ajax_refresh . ', ' . '"autoRefresh":' . $auto_refresh . ', ' . '"dynamicFetch":' . $dynamic_fetch . '};'; if ($dynamic_fetch) { $display .= 'quotescollectionRefresh(args_' . $instance . ');'; } else { if ($ajax_refresh && $auto_refresh) { $display .= 'quotescollectionTimer(args_' . $instance . ');'; } else { if ($ajax_refresh && !$auto_refresh) { $display .= "\n<!--\ndocument.write(\"" . '<footer class=\\"navigation\\">' . '<div class=\\"nav-next\\">' . '<a class=\\"next-quote-link\\" style=\\"cursor:pointer;\\" onclick=\\"quotescollectionRefresh(args_' . $instance . ')\\">' . html_entity_decode($this->refresh_link_text) . '</a>' . '</div>' . '</footer>' . "\")\n//-->\n"; } } } $display .= "</script>\n"; } } $instance_id = ' id="' . $args['instance'] . '"'; $display = '<div class="quotescollection-quote"' . $instance_id . '>' . $display . '</div>'; // If 'echo' argument is false, we return the display. if (isset($args['echo']) && !$args['echo']) { return $display; } else { // Else we simply output the display echo $display; } return true; }
public function do_shortcode($atts = array()) { $db = new Quotes_Collection_DB(); extract(shortcode_atts(array('limit' => 0, 'id' => 0, 'author' => '', 'source' => '', 'tags' => '', 'orderby' => 'quote_id', 'order' => 'ASC', 'paging' => false, 'limit_per_page' => 10), $atts)); // Initialize the variable that holds args to be passed to the DB function to get the quotes // And set 'public' argument to 'yes', because we don't want to display private quotes in public $db_args = array('public' => 'yes'); // 'quote_id' is also a valid shortcode parameter synonymous to 'id' if (isset($quote_id) && is_numeric($quote_id)) { $id = $quote_id; } // If an id is specified, no need to process other attributes. Simply get the quote and return. if ($id && is_numeric($id)) { $db_args['quote_id'] = $id; if ($quote = Quotes_Collection_Quote::with_condition($db_args)) { return $quote->output_format(); } else { return ""; } } if ($author) { $db_args['author'] = $author; } if ($source) { $db_args['source'] = $source; } if ($tags) { $db_args['tags'] = $tags; } switch ($orderby) { case 'quote_id': case 'author': case 'source': case 'time_added': case 'random': $db_args['orderby'] = $orderby; break; case 'date_added': $db_args['orderby'] = 'time_added'; break; case 'rand': $db_args['orderby'] = 'random'; break; default: $db_args['orderby'] = 'quote_id'; } if ($order == 'DESC' || $order == 'desc' || $order == 'Desc') { $db_args['order'] = 'DESC'; } else { $db_args['order'] = 'ASC'; } $page_nav = ""; if ($paging == true || $paging == 1) { $num_quotes = $db->count($db_args); $total_pages = ceil($num_quotes / $limit_per_page); if (!isset($_GET['quotes_page']) || !$_GET['quotes_page'] || !is_numeric($_GET['quotes_page'])) { $page = 1; } else { $page = $_GET['quotes_page']; } if ($page > $total_pages) { $page = $total_pages; } if ($page_nav = $this->pagenav($total_pages, $page, 0, 'quotes_page')) { $page_nav = '<div class="quotescollection_pagenav">' . $page_nav . '</div>'; } $start = ($page - 1) * $limit_per_page; $db_args['num_quotes'] = $limit_per_page; $db_args['start'] = $start; } else { if ($limit && is_numeric($limit)) { $db_args['num_quotes'] = $limit; } } if ($quotes_data = $db->get_quotes($db_args)) { return $page_nav . $this->output_format($quotes_data) . $page_nav; } else { return ""; } }