Advertisement
Guest User

wp_movie_ratings.php version 1.5

a guest
Aug 8th, 2010
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 37.12 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: WP Movie Ratings
  4. Version: 1.5
  5. Plugin URI: http://paulgoscicki.com/projects/wp-movie-ratings/
  6. Author: Paul Goscicki
  7. Author URI: http://paulgoscicki.com/
  8. Description: Wordpress movie rating plugin, which lets you easily rate movies
  9. you've seen recently and display a short list of those movies on your blog.
  10. Internet Movie Database (imdb.com) is used to automatically fetch movie
  11. titles. One-click movie rating is possible using the included bookmarklet
  12. while browsing the imdb.com pages.
  13.  
  14. This fix can be applied to correct WP Movie Rating to display text, like month, in local language, e.g. Swedish or date format. Just scroll down, somewhere abou 363, and you will find it in the code, this one contains swedish language and date format. To translate just change e.g. Januari to your local language.
  15.  
  16. Thanks to http://filmdagbok.heavysheep.com/ who fixed this...
  17.  
  18. */
  19.  
  20. /*
  21. Copyright (c) 2006-2008 by Paul Goscicki, http://paulgoscicki.com/
  22.  
  23. Available under the GNU General Public License (GPL) version 2 or later.
  24. http://www.gnu.org/licenses/gpl.html
  25.  
  26. This program is free software; you can redistribute it and/or
  27. modify it under the terms of the GNU General Public License
  28. as published by the Free Software Foundation; either version 2
  29. of the License, or (at your option) any later version.
  30.  
  31. This program is distributed in the hope that it will be useful,
  32. but WITHOUT ANY WARRANTY; without even the implied warranty of
  33. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  34. GNU General Public License for more details.
  35.  
  36. You should have received a copy of the GNU General Public License
  37. along with this program; if not, write to the Free Software
  38. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  39. */
  40.  
  41. # Anti-hack
  42. if (!defined('ABSPATH')) die("No cheating!");
  43.  
  44. include_once(dirname(__FILE__) . "/wp_http_request.class.php");
  45. include_once(dirname(__FILE__) . "/movie.class.php");
  46.  
  47. # Plugin installation function
  48. function wp_movie_ratings_install() {
  49. global $wpdb, $user_level;
  50.  
  51. # usually: wp_movie_ratings
  52. $table_name = $wpdb->prefix . "movie_ratings";
  53.  
  54. # INSTALLAION -> Create the movie ratings table (first install)
  55. if ($wpdb->get_var("show tables like '$table_name'") != $table_name) {
  56. $sql = "CREATE TABLE " . $table_name . " (
  57. id int(11) NOT NULL auto_increment,
  58. title varchar(255) NOT NULL default '',
  59. imdb_url_short varchar(10) NOT NULL default '',
  60. rating tinyint(2) unsigned NOT NULL default '0',
  61. review text,
  62. replacement_url varchar(255) default '',
  63. watched_on datetime NOT NULL default '0000-00-00 00:00:00',
  64. PRIMARY KEY (id)
  65. );";
  66.  
  67. require_once(ABSPATH . 'wp-admin/upgrade-functions.php');
  68. dbDelta($sql);
  69.  
  70. # UPGRADE -> Add column not present in versions 1.0 - 1.3
  71. } else {
  72. $found = false;
  73. $new_column = "replacement_url";
  74. $table_fields = $wpdb->get_results("DESCRIBE $table_name;");
  75.  
  76. foreach($table_fields as $table_field) {
  77. if ($table_field->Field == $new_column) $found = true;
  78. }
  79.  
  80. if (!$found) $wpdb->query("ALTER TABLE $table_name ADD COLUMN $new_column varchar(255) default '';");
  81. }
  82.  
  83.  
  84. # DROP INDEX UNIQUE KEY (imdb_url_short) created in versions prior to 1.4 of this plugin so we have no problems while adding titles without imdb link
  85. $indexes = $wpdb->get_results("SHOW INDEX FROM $table_name;");
  86. foreach($indexes as $index) {
  87. if ($index->Column_name == "imdb_url_short") {
  88. $wpdb->query("ALTER TABLE $table_name DROP INDEX imdb_url_short");
  89. }
  90. }
  91.  
  92.  
  93. # plugin options
  94. add_option('wp_movie_ratings_count', 6, 'Number of displayed movie ratings (default)', 'no');
  95. add_option('wp_movie_ratings_text_ratings', 'no', 'Display movie ratings as text or as images (stars)', 'no');
  96. add_option('wp_movie_ratings_include_review', 'yes', 'Include review when displaying movie ratings?', 'no');
  97. add_option('wp_movie_ratings_expand_review', 'no', 'Initially show expanded reviews when in page mode?', 'no');
  98. add_option('wp_movie_ratings_order_by', 'title', 'Default movies order when in page mode', 'no');
  99. add_option('wp_movie_ratings_order_direction', 'ASC', 'Default movies order direction when in page mode', 'no');
  100. add_option('wp_movie_ratings_char_limit', 44, 'Display that many characters when the movie title is too long to fit', 'no');
  101. add_option('wp_movie_ratings_sidebar_mode', 'no', 'Display rating below movie title as to not use too much space', 'no');
  102. add_option('wp_movie_ratings_five_stars_ratings', 'no', 'Display ratings using 5 stars instead of 10', 'no');
  103. add_option('wp_movie_ratings_highlight', 'yes', 'Highlight top rated movies?', 'no');
  104. add_option('wp_movie_ratings_dialog_title', 'Movies I\'ve watched recently:', 'Dialog title for movie ratings box', 'no');
  105. add_option('wp_movie_ratings_page_url', '', 'Movie ratings page url', 'no');
  106. add_option('wp_movie_ratings_ping_pingerati', 'yes', 'Ping pingerati.net with movie reviews', 'no');
  107. add_option('wp_movie_ratings_pagination_limit', 100, 'Display that many movies per page when using pagination in page mode', 'no');
  108. }
  109.  
  110.  
  111. # Get web server plugin path -> "relative" or "absolute"
  112. function wp_movie_ratings_get_plugin_path($type) {
  113. $siteurl = get_option("siteurl");
  114. if ($siteurl[strlen($siteurl)-1] != "/") $siteurl .= "/";
  115. $path = $siteurl . "wp-content/plugins/" . dirname(plugin_basename(__FILE__)) . "/";
  116. if ($type == "absolute") return $path;
  117. else {
  118. $tmp_array = parse_url($path);
  119. return $tmp_array["path"];
  120. }
  121. }
  122.  
  123.  
  124. # PHP decode javascript's escape() encoded string
  125. function wp_movie_ratings_utf8_raw_url_decode($source) {
  126. $decodedStr = '';
  127. $pos = 0;
  128. $len = strlen($source);
  129. while ($pos < $len) {
  130. $charAt = substr($source, $pos, 1);
  131. if ($charAt == '%') {
  132. $pos++;
  133. $charAt = substr($source, $pos, 1);
  134. if ($charAt == 'u') {
  135. # we have a unicode character
  136. $pos++;
  137. $unicodeHexVal = substr($source, $pos, 4);
  138. $unicode = hexdec($unicodeHexVal);
  139. $entity = "&#" . $unicode . ';';
  140. $decodedStr .= utf8_encode($entity);
  141. $pos += 4;
  142. } else {
  143. # we have an escaped ascii character
  144. $hexVal = substr($source, $pos, 2);
  145. $decodedStr .= chr(hexdec($hexVal));
  146. $pos += 2;
  147. }
  148. } else {
  149. $decodedStr .= $charAt;
  150. $pos++;
  151. }
  152. }
  153. return $decodedStr;
  154. }
  155.  
  156.  
  157. # Custom encoding/escaping
  158. function wp_movie_ratings_real_escape_string($v, $options = array()) {
  159. if (isset($options["strip_html"]) && $options["strip_html"]) {
  160. $v = strip_tags($v);
  161. }
  162.  
  163. if (isset($options["encode_html"]) && $options["encode_html"]) {
  164. # we will NOT encode HTML entities if there are already some encoded HTML entities (so we will not double encode)
  165. if (!preg_match("/&[a-zA-Z0-9#]*?;/", $v)) {
  166. $v = str_replace('&', "&amp;", $v); # ampersand
  167. $v = str_replace('"', "&quot;", $v); # double quote
  168. $v = str_replace("'", "&#039;", $v); # single quote
  169. $v = str_replace("\\", "&#092;", $v); # backslash (one)
  170. $v = str_replace('<', '&lt;', $v);
  171. $v = str_replace('>', '&gt;', $v);
  172. }
  173. }
  174.  
  175. if (isset($options["output"]) && $options["output"] == "database") {
  176. # first remove default escaping
  177. #if (get_magic_quotes_gpc() || get_magic_quotes_runtime()) $v = stripslashes($v);
  178.  
  179. # FORCE stripslashes() anyway (suxx... can break things; but it mostly fixes them)
  180. $v = stripslashes($v);
  181.  
  182. # then apply better mysql escaping
  183. $v = mysql_real_escape_string($v);
  184. }
  185.  
  186. return $v;
  187. }
  188.  
  189.  
  190. # Advanced version of stripslashes()
  191. function wp_movie_ratings_real_unescape_string($v) {
  192. # work your way through different PHP configurations and strip automatic character escaping
  193. if (get_magic_quotes_gpc() || get_magic_quotes_runtime()) {
  194. if (ini_get("get_magic_quotes_sybase") == 1) $v = str_replace("''", "'", $v);
  195. #else $v = stripslashes($v);
  196. }
  197.  
  198. # FORCE stripslashes() anyway (suxx... can break things; but it mostly fixes them)
  199. $v = stripslashes($v);
  200.  
  201. return $v;
  202. }
  203.  
  204.  
  205. # Include CSS/JS in the HEAD of the html page
  206. function wp_movie_ratings_head_inclusion() {
  207. $plugin_path = wp_movie_ratings_get_plugin_path("absolute");
  208.  
  209. # CSS inclusion
  210. echo "<link rel=\"stylesheet\" type=\"text/css\" media=\"screen\" href=\"" . $plugin_path;
  211. echo (is_plugin_page() ? "admin_page" : basename(__FILE__, ".php")) . ".css" . "\" />\n";
  212.  
  213. # JS inclusion
  214. echo "<script type=\"text/javascript\" src=\"" . $plugin_path . "wp_movie_ratings.js\"></script>\n";
  215. }
  216.  
  217.  
  218. # Change [[wp_movie_ratings_page]] into movie ratings list (alternate tag: <!--wp_movie_ratings_page--> -- Markdown fix)
  219. function wp_movie_ratings_parse_page_tag($content = "") {
  220. # change alternate tag to normal tag
  221. $tmp = str_replace("<!--wp_movie_ratings_page-->", "[[wp_movie_ratings_page]]", $content);
  222.  
  223. # get rid of the unnecessary p/pre/div/h1/h2/h3/h4/h5 tags, which make the movie ratings page non XHTML compliant
  224. $tmp = preg_replace("/<(p|pre|div|h1|h2|h3|h4|h5)[\s]*(class=\".*?\")*>[\s]*(\[\[wp_movie_ratings_page\]\])[\s]*<\/(p|pre|div|h1|h2|h3|h4|h5)>/", "[[wp_movie_ratings_page]]", $tmp);
  225.  
  226. # parse the movie ratings tag
  227. $tmp = str_replace("[[wp_movie_ratings_page]]", wp_movie_ratings_get(null, array("page_mode" => "yes")), $tmp); // . wp_movie_ratings_get_statistics("brief")
  228.  
  229. return $tmp;
  230. }
  231.  
  232.  
  233. # Pass-through function
  234. function wp_movie_ratings_show($count = null, $options = array()) {
  235. echo wp_movie_ratings_get($count, $options);
  236. }
  237.  
  238.  
  239. # Get latest movie ratings (get HTML code)
  240. # Params:
  241. # $count - number of movies to show; if equals -1 it will read the number from the options saved in the database
  242. # $options - optional parameters as hash array (if not specified, they will be read from the database)
  243. # 'text_ratings' -> text ratings (like 5/10) or images of stars ('yes'/'no')
  244. # 'include_review' -> include review with each movie rating ('yes'/'no')
  245. # 'expand_review' -> initially display expanded reviews when in page mode
  246. # 'sidebar_mode' -> compact view for sidebar mode ('yes'/'no')
  247. # 'five_stars_ratings' -> display movie ratings using 5 stars instead of 10 ('yes'/'no')
  248. # 'highlight' -> will highlight the stars of top rated movies ('yes'/'no')
  249. # 'page_mode' -> display all movie ratings on a separate page (with additional options, etc.) ('yes'/'no')
  250. # 'page_url' -> link to movie reviews page (url)
  251. # 'char_limit' -> will cut any character in the title after this number (number)
  252. # 'only_not_rated' -> will select only not yet rated movies ('yes'/'no')
  253. # 'only_rated' -> will select only already rated movies ('yes'/'no')
  254. # 'order_by' -> default sorting (valid only for 'only_not_rated') ('title'/'rating'/'watched_on')
  255. # 'order_direction' -> default sorting direction (valid only for 'only_not_rated') ('ASC'/'DESC')
  256. function wp_movie_ratings_get($count = null, $options = array()) {
  257. # output
  258. $o = "";
  259.  
  260. # parse function parameters
  261. if ($count == null) $count = get_option("wp_movie_ratings_count");
  262. $text_ratings = (isset($options["text_ratings"]) ? $options["text_ratings"] : get_option("wp_movie_ratings_text_ratings"));
  263. $include_review = (isset($options["include_review"]) ? $options["include_review"] : get_option("wp_movie_ratings_include_review"));
  264. $expand_review = (isset($options["expand_review"]) ? $options["expand_review"] : get_option("wp_movie_ratings_expand_review"));
  265. $sidebar_mode = (isset($options["sidebar_mode"]) ? $options["sidebar_mode"] : get_option("wp_movie_ratings_sidebar_mode"));
  266. $five_stars_ratings = (isset($options["five_stars_ratings"]) ? $options["five_stars_ratings"] : get_option("wp_movie_ratings_five_stars_ratings"));
  267. $highlight = (isset($options["highlight"]) ? $options["highlight"] : get_option("wp_movie_ratings_highlight"));
  268. $page_mode = (isset($options["page_mode"]) ? $options["page_mode"] : "no");
  269. $page_url = get_option("wp_movie_ratings_page_url");
  270. $char_limit = (isset($options["char_limit"]) ? intval($options["char_limit"]) : get_option("wp_movie_ratings_char_limit"));
  271. $only_not_rated = (isset($options["only_not_rated"]) ? $options["only_not_rated"] : "no");
  272. $only_rated = (isset($options["only_rated"]) ? $options["only_rated"] : "no");
  273.  
  274. # parse query parameters for page mode (sorting options) (title/rating/watched_on && ASC/DESC)
  275. if ($page_mode == "yes") {
  276. if (isset($_GET["sort"])) $order_by = $_GET["sort"]; else $order_by = get_option("wp_movie_ratings_order_by");
  277. if (isset($_GET["descending"])) $order_direction = "DESC";
  278. else if (isset($_GET["ascending"])) $order_direction = "ASC";
  279. else $order_direction = get_option("wp_movie_ratings_order_direction");
  280.  
  281. # pagination logic
  282. $current_page = (isset($_GET["movies_page"]) ? $_GET["movies_page"] : 1);
  283. $limit = get_option("wp_movie_ratings_pagination_limit");
  284. $start = ($current_page - 1) * $limit;
  285. }
  286.  
  287. # for 'only not rated' and 'only rated' movies you can set 'order_by' and 'order_direction' paremeters
  288. if (($only_not_rated == "yes") || ($only_rated == "yes")) {
  289. $order_by = (isset($options["order_by"]) ? $options["order_by"] : "title");
  290. $order_direction = (isset($options["order_direction"]) ? $options["order_direction"] : "ASC");
  291. }
  292.  
  293. # fetch movies
  294. $m = new Movie();
  295. if ($only_not_rated == "yes") {
  296. $movies = $m->get_not_rated_movies($order_by, $order_direction);
  297. } elseif ($only_rated == "yes") {
  298. $movies = $m->get_rated_movies($order_by, $order_direction);
  299. } elseif ($page_mode == "yes") {
  300. $movies = $m->get_all_movies($order_by, $order_direction, $start, $limit);
  301. } else {
  302. $movies = $m->get_latest_movies(intval($count));
  303. }
  304.  
  305. # love advert
  306. $o .= "\n";
  307.  
  308. # html container
  309. $classes = ($page_mode == "yes" ? "page_mode " : "");
  310. $classes .= ($sidebar_mode == "yes" ? "sidebar_mode " : "");
  311. if (strlen($classes) > 0) $classes = substr($classes, 0, strlen($classes)-1); # drop last space
  312.  
  313. $o .= "<div id=\"wp_movie_ratings\"" . (strlen($classes) > 0 ? " class=\"$classes\"" : "") . ">\n";
  314.  
  315. # sorting options for page mode
  316. if ($page_mode == "yes") {
  317.  
  318. $link = $_SERVER["REQUEST_URI"];
  319.  
  320. # drop everything after '#' (including '#')
  321. if (strpos($link, "#")) $link = substr($link, 0, strpos($link, "#"));
  322.  
  323. # clear link from my stuff
  324. $link = preg_replace("/(&|\?)*sort=(title|rating|watched_on)&(ascending|descending)/", "", $link);
  325. $link = preg_replace("/(&|\?)*movies_page=[0-9]*/", "", $link);
  326.  
  327. # put ? or &amp; at the end of the link depending on the situation
  328. if (strpos($link, "?")) $link .= "&amp;";
  329. else $link .= "?";
  330.  
  331. # create appropriate sorting links
  332. $link_t = $link_r = $link_w = $link;
  333.  
  334. $link_t .= "sort=title&amp;" . ((($order_by == "title") && ($order_direction == "ASC")) ? "descending" : "ascending");
  335. $link_r .= "sort=rating&amp;" . ((($order_by == "rating") && ($order_direction == "DESC")) ? "ascending" : "descending");
  336. $link_w .= "sort=watched_on&amp;" . ((($order_by == "watched_on") && ($order_direction == "DESC")) ? "ascending" : "descending");
  337.  
  338. $o .= "<p id=\"sort_options\">Sortera listan efter: \n";
  339. $o .= "<a href=\"$link_t\">titel" . ($order_by == "title" ? " <span class=\"bullet\">&" . ($order_direction == "ASC" ? "u" : "d") . "arr;</span>" : "") . "</a> | \n";
  340. $o .= "<a href=\"$link_r\">betyg" . ($order_by == "rating" ? " <span class=\"bullet\">&" . ($order_direction == "ASC" ? "u" : "d") . "arr;</span>" : "") . "</a> | \n";
  341. $o .= "<a href=\"$link_w\">datum" . ($order_by == "watched_on" ? " <span class=\"bullet\">&" . ($order_direction == "ASC" ? "u" : "d") . "arr;</span>" : "") . "</a>\n";
  342. $o .= "</p>\n";
  343. }
  344.  
  345. # dialog title
  346. $dialog_title = stripslashes(get_option("wp_movie_ratings_dialog_title"));
  347. if (($page_mode != "yes") && (strlen($dialog_title) > 0)) $o .= "<h2 id=\"reviews_title\">$dialog_title</h2>\n";
  348.  
  349. $o .= "<ul id=\"reviews\"" . ($text_ratings == "yes" ? " class=\"text_ratings\"" : "") . ">\n";
  350.  
  351. if (count($movies) == 0) $o .= "<li>Inga filmer är ännu betygsatta, så sätt igång och betygsätt!</li>\n";
  352. else {
  353. $i = 0; # row alternator
  354. $separator = ""; # used when sorting by view date when in page mode
  355. $separator_last = "";
  356.  
  357. foreach($movies as $movie) {
  358.  
  359. # Separator logic
  360. if (($page_mode == "yes") && ($order_by == "watched_on")) {
  361. $separator = substr($movie->_watched_on, 0, 7);
  362. if (($i == 0) || ($separator != $separator_last)) {
  363. $pendato = date("F Y", mktime(1, 1, 0, substr($separator, 5, 2), 1, substr($separator, 0, 4)));
  364. $pendato = str_replace('January', 'Januari', $pendato);
  365. $pendato = str_replace('February', 'Februari', $pendato);
  366. $pendato = str_replace('March', 'Mars', $pendato);
  367. $pendato = str_replace('April', 'April', $pendato);
  368. $pendato = str_replace('May', 'Maj', $pendato);
  369. $pendato = str_replace('June', 'Juni', $pendato);
  370. $pendato = str_replace('July', 'Juli', $pendato);
  371. $pendato = str_replace('August', 'Augusti', $pendato);
  372. $pendato = str_replace('October', 'Oktober', $pendato);
  373. $pendato = str_replace('November', 'November', $pendato);
  374. $pendato = str_replace('December', 'December', $pendato);
  375. $o .= "<li class=\"separator\">";
  376. $o .= "<h3" . ($i == 0 ? " class=\"first\"" : "") . ">";
  377. $o .= $pendato;
  378. $o .= "</h3></li>\n";
  379. }
  380. $separator_last = $separator;
  381. }
  382.  
  383. # Movie display
  384. $o .= "<li" . ((++$i % 2) == 0 ? " class=\"odd\"" : "") . ">\n";
  385. $o .= $movie->show(wp_movie_ratings_get_plugin_path("absolute"), array("include_review" => $include_review, "text_ratings" => $text_ratings, "sidebar_mode" => $sidebar_mode, "five_stars_ratings" => $five_stars_ratings, "highlight" => $highlight, "page_mode" => $page_mode, "char_limit" => $char_limit));
  386. $o .= "</li>\n";
  387. }
  388. }
  389.  
  390. $o .= "</ul>\n";
  391.  
  392.  
  393. # Pagination
  394. if ($page_mode == "yes") {
  395. $total_movies = $m->get_watched_movies_count("total");
  396. $total_pages = ceil($total_movies / $limit);
  397.  
  398. # display only if $limit is less than $total, so the pagination makes sense
  399. if ($limit < $total_movies) {
  400.  
  401. $link = $_SERVER["REQUEST_URI"];
  402.  
  403. # drop everything after '#' (including '#')
  404. if (strpos($link, "#")) $link = substr($link, 0, strpos($link, "#"));
  405.  
  406. # cleanup of my sh*t
  407. $link = preg_replace("/(&|\?)*movies_page=[0-9]*/", "", $link);
  408.  
  409. # put ? or &amp; at the end of the link depending on the situation
  410. if (strpos($link, "?")) $link .= "&amp;";
  411. else $link .= "?";
  412.  
  413.  
  414. $o .= "<div id=\"pagination\"><p>";
  415.  
  416. # prev button
  417. if ($current_page > 1) $o .= "<a class=\"next_prev\" href=\"" . $link . "movies_page=" . ($current_page - 1) . "\">"; else $o .= "<em class=\"next_prev\">";
  418. $o .= " <span class=\"bullet\">&larr;</span> nyare betyg";
  419. if ($current_page > 1) $o .= "</a> "; else $o .= "</em> ";
  420. $o .= "\n";
  421.  
  422. # pages
  423. for ($i = 1; $i <= $total_pages; $i++) {
  424. if ($current_page != $i) $o .= "<a href=\"" . $link . "movies_page=" . $i . "\">"; else $o .= "<em id=\"current\">";
  425. $o .= $i;
  426. if ($current_page != $i) $o .= "</a> "; else $o .= "</em> ";
  427. $o .= "\n";
  428. }
  429.  
  430. # next button
  431. if ($current_page < $total_pages) $o .= "<a class=\"next_prev\" href=\"" . $link . "movies_page=" . ($current_page + 1) . "\">"; else $o .= "<em class=\"next_prev\">";
  432. $o .= "&auml;ldre betyg <span class=\"bullet\">&rarr;</span>";
  433. if ($current_page < $total_pages) $o .= "</a>"; else $o .= "</em>";
  434. $o .= "\n";
  435.  
  436.  
  437. $o .= "</p></div>\n";
  438. }
  439. }
  440.  
  441.  
  442. # Please do not remove the love ad. Thank you :)
  443. if ($page_mode == "yes")
  444. $o .= "\n";
  445. else if (!is_plugin_page() && (strlen($page_url) > 0))
  446. $o .= "<p id=\"page_url\"><a href=\"$page_url\">Filmbetygsarkiv &raquo;</a></p>\n";
  447.  
  448. $o .= "</div>\n";
  449.  
  450. return $o;
  451. }
  452.  
  453.  
  454. # Show statistics for watched movies (Pass-through function)
  455. function wp_movie_ratings_show_statistics($type = "brief") {
  456. echo wp_movie_ratings_get_statistics($type);
  457. }
  458.  
  459.  
  460. # Get statistics
  461. function wp_movie_ratings_get_statistics($type = "brief") {
  462. $o = "";
  463. $m = new Movie();
  464.  
  465. $total = $m->get_watched_movies_count("total");
  466. $total_avg = $m->get_watched_movies_count("total-average");
  467.  
  468. # division by zero bugfix
  469. # TODO: change this code to calculate days from the database, not by divisions
  470. $days = ($total_avg == 0 ? 1 : round($total/$total_avg));
  471.  
  472. $last_30_days_avg = $m->get_watched_movies_count("last-30-days") / 30;
  473. $last_7_days_avg = $m->get_watched_movies_count("last-7-days") / 7;
  474.  
  475. if ($type == "detailed") echo "<h2>Statistics</h2>\n";
  476.  
  477. $o .= "<p>Total number of rated movies: <strong>$total</strong> (";
  478. if ($type == "detailed") $o .= "average of <strong>$total_avg</strong> movies per day; ";
  479. $o .= "<strong>$days</strong> days of movie ratings).</p>\n";
  480.  
  481. if ($type == "detailed") $o .= "<p>Average of <strong>" . sprintf("%.4f", $last_30_days_avg) . "</strong> movies per day for the past <strong>30</strong> days (<strong>" . sprintf("%.4f", $last_7_days_avg) . "</strong> for the past <strong>7</strong> days).</p>\n";
  482.  
  483. $o .= "<p>Average movie rating: <strong>" . $m->get_average_movie_rating() . "</strong>.</p>\n";
  484.  
  485. if ($type == "detailed") {
  486. $o .= "<p>This month: <strong>" . $m->get_watched_movies_count("month") . "</strong> (last month: <strong>" . $m->get_watched_movies_count("last-month") . "</strong>).</p>\n";
  487. $o .= "<p>This year: <strong>" . $m->get_watched_movies_count("year") . "</strong> (last year: <strong>" . $m->get_watched_movies_count("last-year") . "</strong>).</p>\n";
  488. $o .= "<p>First movie rated on: <strong>" . $m->get_watched_movies_count("first-rated") . "</strong>.</p>\n";
  489. $o .= "<p>Last movie rated on: <strong>" . $m->get_watched_movies_count("last-rated") . "</strong>.</p>\n";
  490. }
  491.  
  492. return $o;
  493. }
  494.  
  495.  
  496. # Add 'Movies' page to Wordpress' Manage menu
  497. function wp_movie_ratings_add_management_page() {
  498. if (function_exists('add_management_page')) {
  499. add_management_page('Movies', 'Movies', 8, 'wp_movie_ratings_management', 'wp_movie_ratings_management_page');
  500. }
  501. }
  502.  
  503.  
  504. # Add 'Movies' page to Wordpress' Options menu
  505. function wp_movie_ratings_add_options_page() {
  506. if (function_exists('add_options_page')) {
  507. add_options_page('Movies', 'Movies', 8, 'wp_movie_ratings_options', 'wp_movie_ratings_options_page');
  508. }
  509. }
  510.  
  511.  
  512. # Manage Movies administration page
  513. function wp_movie_ratings_management_page() {
  514. # DATABASE -> ADD A NEW MOVIE
  515. # Get title of the movie and save its rating in the database
  516. if (isset($_POST["action"]) && (substr(strtolower($_POST["action"]), 0, 3) == "add")) {
  517. $url = (isset($_POST["url"]) ? wp_movie_ratings_utf8_raw_url_decode($_POST["url"]) : null);
  518. $rating = (isset($_POST["rating"]) ? $_POST["rating"] : null);
  519. $title = (isset($_POST["title"]) ? wp_movie_ratings_utf8_raw_url_decode($_POST["title"]) : "");
  520. $review = (isset($_POST["review"]) ? wp_movie_ratings_utf8_raw_url_decode($_POST["review"]) : "");
  521. $replacement_url = (isset($_POST["replacement_url"]) ? wp_movie_ratings_utf8_raw_url_decode($_POST["replacement_url"]) : "");
  522. $watched_on = (isset($_POST["watched_on"]) ? wp_movie_ratings_utf8_raw_url_decode($_POST["watched_on"]) : null);
  523.  
  524. $movie = new Movie($url, $rating, $review, $title, $replacement_url, $watched_on);
  525. $msg = $movie->parse_rating();
  526. if (empty($msg)) {
  527. if (!empty($url)) $msg = $movie->parse_imdb_url();
  528.  
  529. # fetch title from imdb
  530. if (empty($msg) && empty($title) && !empty($url)) $msg = $movie->get_title();
  531.  
  532. # save new movie raing in database
  533. if (empty($msg)) $msg = $movie->save();
  534. }
  535. echo wp_movie_ratings_utf8_raw_url_decode($msg);
  536. $m = new Movie(); # new 'empty' movie object
  537. }
  538.  
  539. # DATABASE -> DELETE MOVIE
  540. if (isset($_POST["action"]) && (substr(strtolower($_POST["action"]), 0, 6) == "delete")) {
  541. $m = new Movie();
  542. $movie = $m->get_movie_by_id($_POST["id"]);
  543. if ($movie != null) echo $movie->delete();
  544. else echo '<div id="message" class="error fade"><p><strong>Error: no movie to delete.</strong></p></div>';
  545.  
  546. $m = new Movie(); # new 'empty' movie object
  547. }
  548.  
  549. # DATABASE -> UPDATE MOVIE DATA
  550. if (isset($_POST["action"]) && (substr(strtolower($_POST["action"]), 0, 6) == "update")) {
  551. $movie = new Movie();
  552. $m = $movie->get_movie_by_id($_POST["id"]);
  553. if (isset($_POST["url"]) && isset($_POST["title"]) && isset($_POST["rating"]) && isset($_POST["review"]) && isset($_POST["replacement_url"]) && isset($_POST["watched_on"])) echo $m->update_from_post();
  554. }
  555.  
  556. # EDIT MOVIE
  557. if ((isset($_POST["action"]) && ($_POST["action"] == "edit")) || (isset($_GET["action"]) && ($_GET["action"] == "edit"))) {
  558. $movie = new Movie();
  559. $m = $movie->get_movie_by_id( (isset($_POST["id"]) ? $_POST["id"] : (isset($_GET["id"]) ? $_GET["id"] : 0) ));
  560. $dialog_title = "Edit";
  561. $action = "Update";
  562. } else { # ADD MOVIE
  563. $dialog_title = "Add a new";
  564. $action = "Add a new";
  565. $m = new Movie(null, 7); # new 'empty' movie object (7 is the default rating)
  566. }
  567.  
  568. $dialog_title .= " movie rating.";
  569. ?>
  570.  
  571. <div class="wrap">
  572. <h2><?php echo $dialog_title ?></h2>
  573.  
  574. <?php
  575. $m->show_add_edit_form($action);
  576. wp_movie_ratings_show(20, array("text_ratings" => "yes", "include_review" => "no", "sidebar_mode" => "no"));
  577. ?>
  578.  
  579. <br />
  580.  
  581. <?php
  582. wp_movie_ratings_show_statistics("detailed");
  583. ?>
  584.  
  585. <br />
  586.  
  587. <h2>Bookmarklet</h2>
  588.  
  589. <p>Add the following link to your Bookmarklets folder so you can rate your movies without visiting Wordpress administration page. You must be <strong>logged in</strong> to your Wordpress blog for it to work, though.</p>
  590. <p><a href="javascript:(function(){open('<?php echo wp_movie_ratings_get_plugin_path("absolute") ?>add_movie.html?url='+escape(location.href),'<?php echo basename(__FILE__, ".php") ?>','toolbar=no,width=432,height=335')})()" title="Add movie rating bookmarklet">Add movie rating bookmarklet</a></p>
  591.  
  592. </div>
  593.  
  594. <?php
  595. }
  596.  
  597.  
  598. # Get all plugin's options
  599. function wp_movie_ratings_get_plugin_options() {
  600. $options = array();
  601. $options["count"] = get_option("wp_movie_ratings_count");
  602. $options["text_ratings"] = get_option("wp_movie_ratings_text_ratings");
  603. $options["include_review"] = get_option("wp_movie_ratings_include_review");
  604. $options["expand_review"] = get_option("wp_movie_ratings_expand_review");
  605. $options["order_by"] = get_option("wp_movie_ratings_order_by");
  606. $options["order_direction"] = get_option("wp_movie_ratings_order_direction");
  607. $options["char_limit"] = get_option("wp_movie_ratings_char_limit");
  608. $options["sidebar_mode"] = get_option("wp_movie_ratings_sidebar_mode");
  609. $options["five_stars_ratings"] = get_option("wp_movie_ratings_five_stars_ratings");
  610. $options["highlight"] = get_option("wp_movie_ratings_highlight");
  611. $options["dialog_title"] = get_option("wp_movie_ratings_dialog_title");
  612. $options["page_url"] = get_option("wp_movie_ratings_page_url");
  613. $options["ping_pingerati"] = get_option("wp_movie_ratings_ping_pingerati");
  614. $options["pagination_limit"] = get_option("wp_movie_ratings_pagination_limit");
  615. return $options;
  616. }
  617.  
  618.  
  619. # WP Movie Ratings options page
  620. function wp_movie_ratings_options_page() {
  621. # Save options in the database
  622. if (isset($_POST["wp_movie_ratings_count"]) && isset($_POST["wp_movie_ratings_text_ratings"])) {
  623. update_option("wp_movie_ratings_count", $_POST["wp_movie_ratings_count"]);
  624. update_option("wp_movie_ratings_text_ratings", $_POST["wp_movie_ratings_text_ratings"]);
  625. update_option("wp_movie_ratings_include_review", $_POST["wp_movie_ratings_include_review"]);
  626. update_option("wp_movie_ratings_expand_review", $_POST["wp_movie_ratings_expand_review"]);
  627. update_option("wp_movie_ratings_order_by", $_POST["wp_movie_ratings_order_by"]);
  628. update_option("wp_movie_ratings_order_direction", $_POST["wp_movie_ratings_order_direction"]);
  629. update_option("wp_movie_ratings_char_limit", $_POST["wp_movie_ratings_char_limit"]);
  630. update_option("wp_movie_ratings_sidebar_mode", $_POST["wp_movie_ratings_sidebar_mode"]);
  631. update_option("wp_movie_ratings_five_stars_ratings", $_POST["wp_movie_ratings_five_stars_ratings"]);
  632. update_option("wp_movie_ratings_highlight", $_POST["wp_movie_ratings_highlight"]);
  633. update_option("wp_movie_ratings_dialog_title", stripslashes($_POST["wp_movie_ratings_dialog_title"]));
  634. update_option("wp_movie_ratings_page_url", stripslashes($_POST["wp_movie_ratings_page_url"]));
  635. update_option("wp_movie_ratings_ping_pingerati", $_POST["wp_movie_ratings_ping_pingerati"]);
  636. update_option("wp_movie_ratings_pagination_limit", $_POST["wp_movie_ratings_pagination_limit"]);
  637. echo "<div id=\"message\" class=\"updated fade\"><p>Options updated</p></div>\n";
  638. }
  639.  
  640. $plugin_options = wp_movie_ratings_get_plugin_options();
  641. ?>
  642.  
  643. <div class="wrap">
  644. <h2>WP Movie Ratings options</h2>
  645.  
  646. <form method="post" action="">
  647.  
  648. <fieldset class="options">
  649. <legend>General options</legend>
  650. <table class="optiontable">
  651.  
  652. <tr valign="top">
  653. <th scope="row"><label for="wp_movie_ratings_dialog_title">Title for movie ratings box:</label></th>
  654. <td><input type="text" name="wp_movie_ratings_dialog_title" id="wp_movie_ratings_dialog_title" class="text" size="50" value="<?php echo stripslashes($plugin_options["dialog_title"]) ?>"/><br />
  655. Leave empty if you don't want any title at all.
  656. </td>
  657. </tr>
  658.  
  659. <tr valign="top">
  660. <th scope="row"><label for="wp_movie_ratings_char_limit">Cut movie title at:</label></th>
  661. <td><input type="text" name="wp_movie_ratings_char_limit" id="wp_movie_ratings_char_limit" class="text" size="2" value="<?php echo $plugin_options["char_limit"] ?>"/> character.<br />
  662. Display that many characters when the movie title is too long to fit.
  663. </td>
  664. </tr>
  665.  
  666. <tr valign="top">
  667. <th scope="row"><label for="wp_movie_ratings_page_url">Movie ratings page url:</label></th>
  668. <td><input type="text" name="wp_movie_ratings_page_url" id="wp_movie_ratings_page_url" class="text" size="50" value="<?php echo stripslashes($plugin_options["page_url"]) ?>"/><br />
  669. If you enter the link (absolute) to the page listing all movie ratings it will create a link from movie ratings box to full archive.
  670. </td>
  671. </tr>
  672.  
  673. <tr valign="top">
  674. <th scope="row"><label for="wp_movie_ratings_ping_pingerati_yes">Ping pingerati?</label></th>
  675. <td>
  676. <input type="radio" value="yes" id="wp_movie_ratings_ping_pingerati_yes" name="wp_movie_ratings_ping_pingerati"<?php echo ($plugin_options["ping_pingerati"] == "yes" ? " checked=\"checked\"" : "") ?> />
  677. <label for="wp_movie_ratings_ping_pingerati_yes">yes</label>
  678. <input type="radio" value="no" id="wp_movie_ratings_ping_pingerati_no" name="wp_movie_ratings_ping_pingerati"<?php echo ($plugin_options["ping_pingerati"] == "no" ? " checked=\"checked\"" : "") ?> />
  679. <label for="wp_movie_ratings_ping_pingerati_no">no</label><br />
  680. Will ping <a href="http://pingerati.net/">pingerati.net</a> for every new movie review you make.
  681. </td>
  682. </tr>
  683.  
  684. </table>
  685. </fieldset>
  686.  
  687.  
  688. <fieldset class="options">
  689. <legend>Display options</legend>
  690. <table class="optiontable">
  691.  
  692. <tr valign="top">
  693. <th scope="row"><label for="wp_movie_ratings_count">Number of displayed movie ratings:</label></th>
  694. <td><input type="text" name="wp_movie_ratings_count" id="wp_movie_ratings_count" class="text" size="2" value="<?php echo $plugin_options["count"] ?>"/><br />
  695. Display that many latest movie ratings.
  696. </td>
  697. </tr>
  698.  
  699. <tr valign="top">
  700. <th scope="row"><label for="wp_movie_ratings_text_ratings_yes">Text movie ratings?</label></th>
  701. <td>
  702. <input type="radio" value="yes" id="wp_movie_ratings_text_ratings_yes" name="wp_movie_ratings_text_ratings"<?php echo ($plugin_options["text_ratings"] == "yes" ? " checked=\"checked\"" : "") ?> />
  703. <label for="wp_movie_ratings_text_ratings_yes">yes</label>
  704. <input type="radio" value="no" id="wp_movie_ratings_text_ratings_no" name="wp_movie_ratings_text_ratings"<?php echo ($plugin_options["text_ratings"] == "no" ? " checked=\"checked\"" : "") ?> />
  705. <label for="wp_movie_ratings_text_ratings_no">no</label><br />
  706. Display text ratings (ie: <strong>5/10</strong>) instead of images.
  707. </td>
  708. </tr>
  709.  
  710. <tr valign="top">
  711. <th scope="row"><label for="wp_movie_ratings_include_review_yes">Display reviews?</label></th>
  712. <td>
  713. <input type="radio" value="yes" id="wp_movie_ratings_include_review_yes" name="wp_movie_ratings_include_review"<?php echo ($plugin_options["include_review"] == "yes" ? " checked=\"checked\"" : "") ?> />
  714. <label for="wp_movie_ratings_include_review_yes">yes</label>
  715. <input type="radio" value="no" id="wp_movie_ratings_include_review_no" name="wp_movie_ratings_include_review"<?php echo ($plugin_options["include_review"] == "no" ? " checked=\"checked\"" : "") ?> />
  716. <label for="wp_movie_ratings_include_review_no">no</label>
  717. </td>
  718. </tr>
  719.  
  720. <tr valign="top">
  721. <th scope="row"><label for="wp_movie_ratings_sidebar_mode_yes">Sidebar mode:</label></th>
  722. <td>
  723. <input type="radio" value="yes" id="wp_movie_ratings_sidebar_mode_yes" name="wp_movie_ratings_sidebar_mode"<?php echo ($plugin_options["sidebar_mode"] == "yes" ? " checked=\"checked\"" : "") ?> />
  724. <label for="wp_movie_ratings_sidebar_mode_yes">yes</label>
  725. <input type="radio" value="no" id="wp_movie_ratings_sidebar_mode_no" name="wp_movie_ratings_sidebar_mode"<?php echo ($plugin_options["sidebar_mode"] == "no" ? " checked=\"checked\"" : "") ?> />
  726. <label for="wp_movie_ratings_sidebar_mode_no">no</label><br />
  727. Movie rating will be displayed in a new line.
  728. </td>
  729. </tr>
  730.  
  731. <tr valign="top">
  732. <th scope="row"><label for="wp_movie_ratings_five_stars_ratings_yes">5 stars ratings?</label></th>
  733. <td>
  734. <input type="radio" value="yes" id="wp_movie_ratings_five_stars_ratings_yes" name="wp_movie_ratings_five_stars_ratings"<?php echo ($plugin_options["five_stars_ratings"] == "yes" ? " checked=\"checked\"" : "") ?> />
  735. <label for="wp_movie_ratings_five_stars_ratings_yes">yes</label>
  736. <input type="radio" value="no" id="wp_movie_ratings_five_stars_ratings_no" name="wp_movie_ratings_five_stars_ratings"<?php echo ($plugin_options["five_stars_ratings"] == "no" ? " checked=\"checked\"" : "") ?> />
  737. <label for="wp_movie_ratings_five_stars_ratings_no">no</label><br />
  738. Display ratings using 5 stars instead of 10.
  739. </td>
  740. </tr>
  741.  
  742. <tr valign="top">
  743. <th scope="row"><label for="wp_movie_ratings_highlight_yes">Highlight top rated movies?</label></th>
  744. <td>
  745. <input type="radio" value="yes" id="wp_movie_ratings_highlight_yes" name="wp_movie_ratings_highlight"<?php echo ($plugin_options["highlight"] == "yes" ? " checked=\"checked\"" : "") ?> />
  746. <label for="wp_movie_ratings_highlight_yes">yes</label>
  747. <input type="radio" value="no" id="wp_movie_ratings_highlight_no" name="wp_movie_ratings_highlight"<?php echo ($plugin_options["highlight"] == "no" ? " checked=\"checked\"" : "") ?> />
  748. <label for="wp_movie_ratings_highlight_no">no</label><br />
  749. Will highlight movies rated 9 and 10 (4,5 and 5 for five stars mode).
  750. </td>
  751. </tr>
  752.  
  753. </table>
  754. </fieldset>
  755.  
  756.  
  757. <fieldset class="options">
  758. <legend>Page mode options</legend>
  759. <table class="optiontable">
  760.  
  761. <tr valign="top">
  762. <th scope="row"><label for="wp_movie_ratings_expand_review_yes">Expand reviews in page mode?</label></th>
  763. <td>
  764. <input type="radio" value="yes" id="wp_movie_ratings_expand_review_yes" name="wp_movie_ratings_expand_review"<?php echo ($plugin_options["expand_review"] == "yes" ? " checked=\"checked\"" : "") ?> />
  765. <label for="wp_movie_ratings_expand_review_yes">yes</label>
  766. <input type="radio" value="no" id="wp_movie_ratings_expand_review_no" name="wp_movie_ratings_expand_review"<?php echo ($plugin_options["expand_review"] == "no" ? " checked=\"checked\"" : "") ?> />
  767. <label for="wp_movie_ratings_expand_review_no">no</label><br />
  768. Initially show expanded reviews when in page mode.
  769. </td>
  770. </tr>
  771.  
  772. <tr valign="top">
  773. <th scope="row"><label for="wp_movie_ratings_order_by">Sort movies by:</label></th>
  774. <td>
  775. <select name="wp_movie_ratings_order_by" id="wp_movie_ratings_order_by">
  776. <option value="title"<?php echo ($plugin_options["order_by"] == "title" ? "selected=\"selected\"" : ""); ?>>title</option>
  777. <option value="rating"<?php echo ($plugin_options["order_by"] == "rating" ? "selected=\"selected\"" : ""); ?>>rating</option>
  778. <option value="watched_on"<?php echo ($plugin_options["order_by"] == "watched_on" ? "selected=\"selected\"" : ""); ?>>view date</option>
  779. </select>
  780. <select name="wp_movie_ratings_order_direction" id="wp_movie_ratings_order_direction">
  781. <option value="ASC"<?php echo ($plugin_options["order_direction"] == "ASC" ? "selected=\"selected\"" : ""); ?>>ascending</option>
  782. <option value="DESC"<?php echo ($plugin_options["order_direction"] == "DESC" ? "selected=\"selected\"" : ""); ?>>descending</option>
  783. </select>
  784. when in page mode.
  785. </td>
  786. </tr>
  787.  
  788. <tr valign="top">
  789. <th scope="row"><label for="wp_movie_ratings_pagination_limit">Max movies per page:</label></th>
  790. <td><input type="text" name="wp_movie_ratings_pagination_limit" id="wp_movie_ratings_pagination_limit" class="text" size="2" value="<?php echo $plugin_options["pagination_limit"] ?>"/><br />
  791. Display that many movies per page when in page mode (otherwise paginate the page).
  792. </td>
  793. </tr>
  794.  
  795. </table>
  796. </fieldset>
  797.  
  798. <p class="submit"><input type="submit" name="submit" value="Update Options &raquo;" /></p>
  799.  
  800. </form>
  801.  
  802. </div>
  803.  
  804. <?php
  805. }
  806.  
  807. # Hook for plugin installation
  808. add_action('activate_' . dirname(plugin_basename(__FILE__)) . '/' . basename(plugin_basename(__FILE__)), 'wp_movie_ratings_install');
  809.  
  810. # Add actions for admin panel
  811. add_action('admin_menu', 'wp_movie_ratings_add_management_page');
  812. add_action('admin_menu', 'wp_movie_ratings_add_options_page');
  813.  
  814. # CSS/JS inclusion in HEAD
  815. add_action('wp_head', 'wp_movie_ratings_head_inclusion');
  816. add_action('admin_head', 'wp_movie_ratings_head_inclusion');
  817.  
  818. # Filter [[wp_movie_ratings_page]] tag in page mode
  819. add_filter("the_content", "wp_movie_ratings_parse_page_tag");
  820.  
  821. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement