DonkieQuote: donkieQuote.php

File donkieQuote.php, 18.4 KB (added by GURT, 6 years ago)

donkieQuote 0.2.2a

Line 
1<?php
2/*
3Plugin Name: Donkie Quote
4Plugin URI: http://dev.wp-plugins.org/wiki/DonkieQuote
5Description: Displays a random quote.  You must place the <code>do_random_quote();</code> template tag somwhere in you template to actually see the quote.
6Author: Chris Shaffer
7Version: 0.2.2
8Author URI: http://kickthedonkey.net
9*/
10
11$dq_quote = false;
12
13/*
14        this function is called during the wp_head action to setup the random quote
15*/
16function dq_assign_quote()
17{
18        global $wpdb;
19        $num_quotes = $wpdb->get_var('SELECT MAX(quote_id) FROM ' . get_option('dq_table_nm'));
20        srand((double)microtime()*1000000);
21        $rand_quote = rand(1,$num_quotes);
22        $flag = false;
23        while( !$flag )
24        {
25                $dq_quote = fetchQuote($rand_quote);
26                if($dq_quote == false )
27                {
28                        $flag = false;
29                        //pull a new random number incase the other one was empty.
30                        $rand_quote = rand(1,$num_quotes);
31                }
32                else
33                {
34                        $flag = true;
35                }
36        }
37        return $dq_quote;
38}
39
40
41/*
42        template tag to display random quote.
43*/
44function do_random_quote($preTag='<div class="dq_quote">', $postTag = '</div>' , $sepTag = ' -')
45{
46        global $dq_quote;
47
48        if ($dq_quote === false)
49        {
50                $dq_quote = dq_assign_quote();
51        }
52
53        $output = $preTag;
54
55        $output .= $dq_quote['text'];
56
57        $output .= $sepTag;
58
59        if(!empty($dq_quote['src']) && isset($dq_quote['src']) )
60        {
61                if(!empty($dq_quote['src_url']) && isset($dq_quote['src_url']) )
62                {
63                        $output .= '<a href="' . $dq_quote['src_url'] . '">';
64                }
65
66                $output .= $dq_quote['src'];
67
68                if(!empty($dq_quote['src_url']) && isset($dq_quote['src_url']) )
69                {
70                        $output .= '</a>';
71                }
72        }
73        else
74        {
75                $output .= '<em>Unknown</em>';
76        }
77
78        $output .= $postTag;
79
80        print $output;
81}
82
83// dq_add_pages() is the sink function for the 'admin_menu' hook
84function dq_add_pages()
85{
86        // Add a new menu under Manage:
87        add_management_page('Quotes', 'Quotes', 5, __FILE__, 'dq_manage_page');
88
89        //Add subment to 'write' page:
90        add_submenu_page('post.php', 'Quote', 'Quote', 5, __FILE__, 'dq_write_quote');
91
92        // Add a new menu under Options:
93    add_options_page('Quotes', 'Quotes', 8, __FILE__, 'dq_options_page');
94}
95
96/*
97        Handles the 'Write' menu functionality for this plugin
98*/
99function dq_write_quote()
100{
101        global $wpdb, $user_ID;
102
103        $quote_id = isset($_GET['quote']) ? $_GET['quote'] : 0;
104
105        $messages[1] = __('Quote updated');
106
107        //what am I doing?
108        if($_GET['action'] == 'edit')
109        {
110                if($quote_id < 1)
111                {
112                        //no quote provided!
113                        $error = true;
114                        $displayForm = false;
115                        $feedbackMsg = 'You did not provide a Quote to edit!  Perhaps you should look at the <a href="edit.php?page=donkieQuote.php">Manage Quotes</a> page?';
116                }
117                else
118                {
119                        //make sure the quote exists!
120                        $dq_quote = fetchQuote($quote_id);
121                        $displayForm = true;
122                        if($dq_quote == false)
123                        {
124                                $error = true;
125                                $displayForm = false;
126                                $feedbackMsg = 'Listen, I looked all over the database, but I just couldn\'t find the quote (ID \'' . $quote_id . '\') you were wanting to'
127                                                                .' edit.  Maybe you should look at the <a href="edit.php?page=donkieQuote.php">Manage Quotes</a> page, and select one from that list?';
128                        }
129                        //setup to display the edit form...
130                        $form_action = 'edit';
131                        $form_extra = "<input type='hidden' name='quote_id' value='$quote_id' />";
132                }
133        }
134        elseif($_POST['action'] == 'edit')
135        {
136                //they've already seen the form...  actually update the quote...
137                $form_action = 'update';
138                $quote_id = isset($_POST['quote_id']) ? $_POST['quote_id'] : 0;
139
140                $quotetxt = $_POST['quotetxt'];
141                $quote_source = $_POST['quote_source'];
142                $quote_source_url = $_POST['quote_source_url'];
143
144                $displayForm = false;
145
146                if(strlen($quotetxt) < 1)
147                {
148                        $error = true;
149                        $displayForm = true;
150                        $feedbackMsg = 'Looks like you didn\'t fill in the Quote field.  Care to try again?';
151                        $dq_quote['text'] = $quotetxt;
152                        $dq_quote['src'] = $quote_source;
153                        $dq_quote['src_url'] = $quote_source_url;
154                        $form_action = 'edit';
155                        $form_extra = "<input type='hidden' name='quote_id' value='$quote_id' />";
156                }
157                elseif($quote_id < 1)
158                {
159                        $error = true;
160                        $displayForm = false;
161                        $feedbackMsg = 'You did not provide a Quote to edit!  Perhaps you should look at the <a href="edit.php?page=donkieQuote.php">Manage Quotes</a> page?';
162                }
163                else
164                {
165                        //they've made it this far.  Write the quote to the database.
166                        $quote_source = strlen($quote_source) < 1 ? 'NULL' : '\'' . $quote_source . '\'';
167                        $quote_source_url = strlen($quote_source_url) < 1 ? 'NULL' : '\'' . $quote_source_url . '\'';
168                        $quotetxt = apply_filters('content_save_pre',  $quotetxt);
169                        $quotetxt = '\'' . $quotetxt . '\'';
170                        $query = "UPDATE " . get_option('dq_table_nm') . " SET quote_text = $quotetxt, quote_src = $quote_source, quote_src_url = $quote_source_url WHERE quote_id = $quote_id";
171                        //execute query, and see what happened:
172                        $wpdb->query($query);
173                        $error = false;
174                        $displayForm = false;
175                        $feedbackMsg = "Quote '$quote_id' successfully updated!</p><p><a href=\"edit.php?page=donkieQuote.php\">Manage Quotes</a><br/><a href=\"post.php?page=donkieQuote.php\">Create New Quote</a>";
176                }
177        }
178        elseif($_POST['action'] == 'new')
179        {
180                //They've filled in the form for a new quote.  Add it.
181                $form_action = 'writenew';
182                $quote_source = strlen($_POST['quote_source']) < 1 ? 'NULL' : '\'' . $_POST['quote_source'] . '\'';
183                $quote_source_url = strlen($_POST['quote_source_url']) < 1 ? 'NULL' : '\'' . $_POST['quote_source_url'] . '\'';
184                $displayForm = false;
185
186                if(strlen($quotetxt) < 1)
187                {
188                        $error = true;
189                        $displayForm = true;
190                        $feedbackMsg = 'Looks like you didn\'t fill in the Quote field.  Care to try again?';
191                        $dq_quote['src'] = $quote_source;
192                        $dq_quote['text'] = $quotetxt;
193                        $dq_quote['src_url'] = $quote_source_url;
194                        $form_action = 'new';
195                }
196                $quotetxt = apply_filters('content_save_pre',  $_POST['quotetxt']);
197                $quotetxt = '\'' . $quotetxt . '\'';
198                $id_result = $wpdb->get_row("SHOW TABLE STATUS LIKE '" . get_option('dq_table_nm') . "'");
199                $quote_id = $id_result->Auto_increment;
200                $query = "INSERT INTO " . get_option('dq_table_nm') . " VALUES($quote_id,$user_ID,$quotetxt,$quote_source, $quote_source_url, NOW())";
201                //execute query, and see what happened:
202                $wpdb->query($query);
203                $error = false;
204                $displayForm = false;
205                $feedbackMsg = "Quote successfully added as ID '$quote_id'!</p><p><a href=\"edit.php?page=donkieQuote.php\">Manage Quotes</a><br/><a href=\"post.php?page=donkieQuote.php\">Create New Quote</a>";
206        }
207        else
208        {
209                //display write new quote form.
210                $form_action = 'new';
211                $displayForm = true;
212        }
213
214        ?>
215        <?php if (isset($feedbackMsg)) : ?>
216        <div class="updated"><p><?php if($error) { echo '<strong>ERROR:</strong> '; } echo $feedbackMsg; ?></p></div>
217        <?php endif; ?>
218
219        <?php if ($displayForm) : ?>
220        <form name="quote" action="post.php?page=donkieQuote.php" method="post" id="quote">
221        <div class="wrap">
222        <h2><?php _e('Write Quote'); ?></h2>
223        <p><?php _e(get_option('dq_quote_desc')); ?></p>
224
225        <input type="hidden" name="user_ID" value="<?php echo $user_ID ?>" />
226        <input type="hidden" name="action" value="<?php echo $form_action ?>" />
227        <?php echo $form_extra; ?>
228
229        <?php if (isset($_GET['message']) && 1 > $_GET['message']) : ?>
230        <script type="text/javascript">
231        <!--
232        function focusit() {
233                // focus on first input field
234                document.quote.title.focus();
235        }
236        window.onload = focusit;
237        //-->
238        </script>
239        <?php endif; ?>
240        <div id="quotestuff">
241                <fieldset id="quotetxtdiv">
242                        <legend>
243                                <!--<a href="http://wordpress.org/docs/reference/post/#excerpt" title="<?php _e('Help with excerpts') ?>">-->
244                                        <?php _e('Quote') ?>
245                                <!--</a>-->
246                        </legend>
247                        <div>
248                                <textarea name="quotetxt" rows="2" cols="60" name="excerpt" tabindex="1" id="quotetxt"><?php echo $dq_quote['text'] ?></textarea>
249                        </div>
250                </fieldset>
251                <br/>
252                <fieldset id="quotesrcdiv">
253                <legend>
254                                <!--<a href="http://wordpress.org/docs/reference/post/#excerpt" title="<?php _e('Help with excerpts') ?>">-->
255                                        <?php _e('Quote Source') ?>
256                                <!--</a>-->
257                        </legend>
258                        <div>
259                                <p>Who said this quote (if you don't know, leave blank).</p>
260                                <input type="text" name="quote_source" size="30" tabindex="2" value="<?php echo $dq_quote['src']; ?>" id="quotesrc" />
261                        </div>
262                </fieldset>
263                <br />
264                <fieldset id="quotesrcurldiv">
265                        <legend>
266                                <!--<a href="http://wordpress.org/docs/reference/post/#excerpt" title="<?php _e('Help with excerpts') ?>">-->
267                                        <?php _e('Quote Source URL') ?>
268                                <!--</a>-->
269                        </legend>
270                        <div>
271                                <p>Where you found this quote (if you don't know, leave blank).</p>
272                                <input type="text" name="quote_source_url" size="30" tabindex="3" value="<?php echo $dq_quote['src_url']; ?>" id="quotesrcurl" />
273                        </div>
274            </fieldset>
275                <br />
276        <?php
277        ?>
278        <script type="text/javascript">
279        <!--
280        edCanvas = document.getElementById('content');
281        //-->
282        </script>
283
284        <p class="submit">
285                <input type="submit" name="submit" value="<?php _e('Save') ?>" style="font-weight: bold;" tabindex="6" />
286        </p>
287
288        </div>
289
290        <h3><a href="edit.php?page=donkieQuote.php"><?php _e('Manage Quotes'); ?> &raquo;</a></h3>
291
292        </div>
293
294
295        </form>
296        <?php endif; ?>
297        <?php
298}
299
300// dq_manage_page() displays the page content for the Manage->Quote submenu
301function dq_manage_page()
302{
303        global $wpdb, $dq_table_nm, $user_ID, $user_level;
304
305        if($_GET['action'] == 'delete')
306        {
307                if(isset($_GET['quote']))
308                {
309                        //delete the puppy!
310                        $result = $wpdb->query("DELETE FROM " . get_option('dq_table_nm') . " WHERE quote_id = " . $_GET['quote']);
311                        //check to see what happened...
312                        if($result)
313                        {
314                                $error = false;
315                                $feedbackMsg = 'Quote \''. $_GET['quote'] . '\' successfully removed!  You deserve a cookie.';
316                        }
317                        else
318                        {
319                                $error = true;
320                                $feedbackMsg = 'Listen, I looked all over the database, but I just couldn\'t find the quote (ID \'' . $_GET['quote'] . '\') you where wanting to'
321                                                                .' delete.  Sorry.  Its probably my fault.  However, just select another quote from the list below '
322                                                                . 'if you\'re still in a deleting mood.';
323                        }
324                }
325                else
326                {
327                        $error = true;
328                        $feedbackMsg = 'You did not provide a Quote to delete!  Perhaps you should look at the <a href="edit.php?page=donkieQuote.php">Manage Quotes</a> page?';
329                }
330        }
331
332
333        ?>
334
335        <?php if (isset($feedbackMsg)) : ?>
336        <div class="updated"><p><?php if($error) { echo '<strong>ERROR:</strong> '; } echo $feedbackMsg; ?></p></div>
337        <?php endif; ?>
338
339        <div class="wrap">
340        <h2><?php _e('Quote Management'); ?></h2>
341        <?php
342
343        //if they're using wp 2 or greater, use first query.
344        if(get_bloginfo('version') >= 2)
345        {
346                $query = "SELECT quote_id, user_login, quote_text, quote_src, quote_src_url, quote_added, meta_value AS user_level FROM wp_quotes, wp_users, wp_usermeta WHERE wp_quotes.quote_author = wp_users.ID AND wp_usermeta.user_id = wp_users.ID AND wp_usermeta.meta_key = 'wp_user_level' ORDER BY quote_id DESC ";
347        }
348        else
349        {       
350                $query = "SELECT quote_id, user_login, quote_text, quote_src, quote_src_url, quote_added, user_level FROM " . get_option('dq_table_nm') . ", $wpdb->users WHERE " . get_option('dq_table_nm') . ".quote_author = $wpdb->users.ID ORDER BY quote_id DESC";
351        }
352
353        if (isset($user_ID) && ('' != intval($user_ID))) {
354                $quotes = $wpdb->get_results($query);
355        } else {
356                $quotes = $wpdb->get_results($query);
357        }
358
359        if ($quotes)
360        {
361                ?>
362                <table width="100%" cellpadding="3" cellspacing="3">
363                  <tr>
364                    <th scope="col"><?php _e('ID') ?></th>
365                    <th scope="col"><?php _e('Quote Text') ?></th>
366                    <th scope="col"><?php _e('Source') ?></th>
367                    <th scope="col"><?php _e('Owner') ?></th>
368                        <th scope="col"><?php _e('Added') ?></th>
369                        <th scope="col"></th>
370                    <th scope="col"></th>
371                  </tr>
372
373                <?php
374
375                quote_rows($quotes);
376                ?>
377        </table>
378        <?php
379        }
380        else
381        {
382                ?>
383                <p><?php _e('No quotes yet.') ?></p>
384                <?php
385        } // end if ($quotes)
386        ?>
387        <p><?php _e(get_option('dq_quote_desc')); ?></p>
388        <h3><a href="post.php?page=donkieQuote.php"><?php _e('Create New Quote'); ?> &raquo;</a></h3>
389        </div>
390        <?php
391}
392
393/*
394        Options->Quotes submenu handler.
395*/
396function dq_options_page() {
397    global $wpdb;
398
399    if($_POST['action'] == 'update')
400    {
401            //update the data.
402            //check the data.
403            $error = false;
404
405            $quote_table = $_POST['quote_table'];
406            $quote_preview_len = $_POST['quote_preview_len'];
407            $quote_desc = $_POST['quote_desc'];
408
409            if(strlen($quote_table) < 1)
410            {
411                        $errorMsg .= 'You must fill in the \'Quote Table\' field!<br/>';
412                        $error = true;
413            }
414            if(!is_numeric($quote_preview_len))
415            {
416                    $errorMsg .= 'You must provide an integer in the \'Preview Length\' field!<br/>';
417                    $error = true;
418            }
419
420            $displayForm = true;
421
422            if($error)
423            {
424                    $feedbackMsg = "$errorMsg</p>Please correct the above errors and re-submit the form.";
425            }
426            else
427            {
428                    $feedbackMsg = "Options Updated!";
429                    update_option('dq_table_nm', $quote_table);
430                    update_option('dq_previewTextLength', $quote_preview_len);
431                    update_option('dq_quote_desc', $quote_desc);
432            }
433
434    }
435    else
436    {
437            $displayForm = true;
438            $quote_table = get_option('dq_table_nm');
439                $quote_preview_len = get_option('dq_previewTextLength');
440                $quote_desc = get_option('dq_quote_desc');
441    }
442
443    if($displayForm)
444    {
445        $res = $wpdb->get_results('SELECT option_name, option_description FROM ' . $wpdb->options . ' WHERE option_name = \'dq_table_nm\' OR option_name = \'dq_previewTextLength\' OR option_name = \'dq_quote_desc\'');
446
447                if($res)
448                {
449                        //there should only be one quote here...
450                        foreach($res as $option)
451                        {
452                                $opt_desc[$option->option_name] = $option->option_description;
453                        }
454                }
455        }
456
457    ?>
458<div class="wrap">
459<h2><?php _e('Quote Options') ?></h2>
460<?php if (isset($feedbackMsg)) : ?>
461<div class="updated"><p><?php if($error) { echo '<strong>ERROR:</strong> '; } echo $feedbackMsg; ?></p></div>
462<?php endif; ?>
463        <?php if ($displayForm) : ?>
464<form name="quoteoptions" method="post" action="options-general.php?page=donkieQuote.php">
465        <input type="hidden" name="action" value="update" />
466        <!--<input type="hidden" name="page_options" value="'hack_file','use_fileupload','fileupload_realpath','fileupload_url','fileupload_allowedtypes','fileupload_maxk','fileupload_maxk','fileupload_minlevel','use_geo_positions','use_linksupdate'" /> -->
467        <!--<fieldset class="options">
468        <legend>
469        <input name="use_fileupload" type="checkbox" id="use_fileupload" value="1" <?php checked('1', get_settings('use_fileupload')); ?> />
470        <label for="use_fileupload"><?php _e('Allow File Uploads') ?></label></legend>-->
471        <table width="100%" cellspacing="2" cellpadding="5" class="editform">
472                <tr>
473                        <th width="33%" valign="top" scope="row"><?php _e('Quote Table:') ?> </th>
474                        <td>
475                                <input name="quote_table" type="text" id="quote_table" value="<?php echo $quote_table; ?>" size="50" /><br />
476                                <?php echo $opt_desc['dq_table_nm'] ?>
477                        </td>
478                </tr>
479                <tr>
480                        <th valign="top" scope="row"><?php _e('Preview Length:') ?> </th>
481                        <td>
482                                <input name="quote_preview_len" type="text" id="quote_preview_len" value="<?php echo $quote_preview_len; ?>" size="5" /><br />
483                                <?php echo $opt_desc['dq_previewTextLength'] ?>
484                        </td>
485                </tr>
486                <tr>
487                        <th width="33%" valign="top" scope="row"><?php _e('Quote Description:') ?> </th>
488                        <td>
489                                <input name="quote_desc" type="text" id="quote_desc" value="<?php echo $quote_desc; ?>" size="50" /><br />
490                                <?php echo $opt_desc['dq_quote_desc'] ?>
491                        </td>
492                </tr>
493        </table>
494        <!--</fieldset>-->
495        <p class="submit">
496                <input type="submit" name="Submit" value="<?php _e('Update Options') ?> &raquo;" />
497        </p>
498</form>
499        <?php endif; ?>
500</div>
501        <?php
502}
503
504/*
505        Spits out all the quotes in a nice little table for the Manage->Quotes submenu.
506*/
507function quote_rows( $quotes)
508{
509        global $wpdb, $class, $user_level;
510
511        foreach($quotes as $quote)
512        {
513                $class = ('alternate' == $class) ? '' : 'alternate';
514?>
515  <tr class='<?php echo $class; ?>'>
516    <th scope="row"><?php echo $quote->quote_id; ?></th>
517    <td>
518      <?php print previewText($quote->quote_text); ?>
519    </td>
520    <td>
521        <?php print $quote->quote_src; ?>
522    </td>
523    <td><?php echo $quote->user_login; ?></td>
524    <td><?php echo mysql2date('Y-m-d g:i a', $quote->quote_added); ?></td>
525        <td><?php if (($user_level >= $quote->user_level) or ($user_login == $quote->user_login)) { echo "<a href='post.php?page=donkieQuote.php&action=edit&amp;quote=$quote->quote_id' class='edit'>" . __('Edit') . "</a>"; } ?></td>
526    <td><?php if (($user_level >= $quote->user_level) or ($user_login == $quote->user_login)) { echo "<a href='edit.php?page=donkieQuote.php&action=delete&amp;quote=$quote->quote_id' class='delete' onclick=\"return confirm('" . sprintf(__("You are about to delete this quote \'%s\'\\n  \'OK\' to delete, \'Cancel\' to stop."), $quote->quote_id) . "')\">" . __('Delete') . "</a>"; } ?></td>
527  </tr>
528
529<?php
530        }
531
532}
533
534/*
535        Generates a 'preview' of the quote for display in the Manage->Quotes table.
536*/
537function previewText($text)
538{
539        // Change to the number of characters you want to display
540        $chars = get_option('dq_previewTextLength');
541
542        $text = $text." ";
543        $text = substr($text,0,$chars);
544        $text = substr($text,0,strrpos($text,' '));
545        $text = $text."...";
546
547        return $text;
548}
549
550/*
551        Fetches a single quote, by ID.
552*/
553function fetchQuote( $quote_id )
554{
555        global $wpdb;
556        //make sure the quote exists!
557        $res = $wpdb->get_results('SELECT quote_text, quote_src, quote_src_url FROM ' . get_option('dq_table_nm') . ' WHERE quote_id = ' . $quote_id);
558
559        if($res)
560        {
561                //there should only be one quote here...
562                foreach($res as $quote)
563                {
564                        $dq_quote['text'] = $quote->quote_text;
565                        $dq_quote['src']  = $quote->quote_src;
566                        $dq_quote['src_url'] = $quote->quote_src_url;
567                }
568
569                /*print "<pre>";
570                print_r($dq_quote);
571                print "</pre>";
572                exit();*/
573
574                return $dq_quote;
575        }
576        else
577        {
578                return false;
579        }
580}
581
582// Synch up with the rest of Wordpress.
583add_action ( 'wp_head', 'dq_assign_quote', 10 );
584
585add_action('admin_menu', 'dq_add_pages');
586
587//build default options, if they don't exist.
588add_option('dq_table_nm', 'wp_quotes', 'Table where the quotes are stored.');
589
590add_option('dq_previewTextLength','45', 'How much of the quote should be previewed on the <a href="edit.php?page=donkieQuote.php">Manage Quotes</a> page.');
591
592add_option('dq_quote_desc', 'Quotes are interesting, insiteful, of funny phrases that you may find add value to display on your blog.', 'Describes what quotes are.');
593?>