PageRenderTime 57ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/wordpress/plugins/post-plugin-library/common_functions.php

https://github.com/prabhu/desistartups
PHP | 511 lines | 406 code | 45 blank | 60 comment | 134 complexity | 926df4e3797d8b6d45657bfa815c8fec MD5 | raw file
  1. <?php
  2. /*
  3. Library for the Recent Posts, Random Posts, Recent Comments, and Similar Posts plugins
  4. -- provides the routines which the plugins share
  5. */
  6. define('CF_LIBRARY', true);
  7. function ppl_parse_args($args) {
  8. // $args is of the form 'key1=val1&key2=val2'
  9. // The code copes with null values, e.g., 'key1=&key2=val2'
  10. // and arguments with embedded '=', e.g. 'output_template=<li class="stuff">{...}</li>'.
  11. if($args){
  12. // the default separator is '&' but you may wish to include the character in a title, say,
  13. // so you can specify an alternative separator by making the first character of $args
  14. // '&' and the second character your new separator...
  15. if (substr($args, 0, 1) === '&') {
  16. $s = substr($args, 1, 1);
  17. $args = substr($args, 2);
  18. } else {
  19. $s = '&';
  20. }
  21. // separate the arguments into key=value pairs
  22. $arguments = explode($s, $args);
  23. foreach($arguments as $arg){
  24. if($arg){
  25. // find the position of the first '='
  26. $i = strpos($arg, '=');
  27. // if not a valid format ('key=value) we ignore it
  28. if ($i){
  29. $key = substr($arg, 0, $i);
  30. $val = substr($arg, $i+1);
  31. $result[$key]=$val;
  32. }
  33. }
  34. }
  35. }
  36. return $result;
  37. }
  38. function ppl_set_options($option_key, $arg, $default_output_template) {
  39. $options = get_option($option_key);
  40. // deal with compound options
  41. if (isset($arg['custom-key'])) {$arg['custom']['key'] = $arg['custom-key']; unset($arg['custom-key']);}
  42. if (isset($arg['custom-op'])) {$arg['custom']['op'] = $arg['custom-op']; unset($arg['custom-op']);}
  43. if (isset($arg['custom-value'])) {$arg['custom']['value'] = $arg['custom-value']; unset($arg['custom-value']);}
  44. if (isset($arg['age-direction'])) {$arg['age']['direction'] = $arg['age-direction']; unset($arg['age-direction']);}
  45. if (isset($arg['age-length'])) {$arg['age']['length'] = $arg['age-length']; unset($arg['age-length']);}
  46. if (isset($arg['age-duration'])) {$arg['age']['duration'] = $arg['age-duration']; unset($arg['age-duration']);}
  47. // then fill in the defaults
  48. if (!isset($arg['limit'])) $arg['limit'] = stripslashes($options['limit']);
  49. if (!isset($arg['skip'])) $arg['skip'] = stripslashes($options['skip']);
  50. if (!isset($arg['trim_before'])) $arg['trim_before'] = stripslashes($options['trim_before']);
  51. if (!isset($arg['omit_current_post'])) $arg['omit_current_post'] = $options['omit_current_post'];
  52. if (!isset($arg['show_private'])) $arg['show_private'] = $options['show_private'];
  53. if (!isset($arg['show_pages'])) $arg['show_pages'] = $options['show_pages'];
  54. if (!isset($arg['none_text'])) $arg['none_text'] = stripslashes($options['none_text']);
  55. if (!isset($arg['no_text'])) $arg['no_text'] = $options['no_text'];
  56. if (!isset($arg['tag_str'])) $arg['tag_str'] = stripslashes($options['tag_str']);
  57. if (!isset($arg['excluded_cats'])) $arg['excluded_cats'] = stripslashes($options['excluded_cats']);
  58. if (!isset($arg['included_cats'])) $arg['included_cats'] = stripslashes($options['included_cats']);
  59. if (!isset($arg['excluded_authors'])) $arg['excluded_authors'] = stripslashes($options['excluded_authors']);
  60. if (!isset($arg['included_authors'])) $arg['included_authors'] = stripslashes($options['included_authors']);
  61. if (!isset($arg['excluded_posts'])) $arg['excluded_posts'] = stripslashes($options['excluded_posts']);
  62. if (!isset($arg['included_posts'])) $arg['included_posts'] = stripslashes($options['included_posts']);
  63. if (!isset($arg['stripcodes'])) $arg['stripcodes'] = $options['stripcodes'];
  64. if (!isset($arg['prefix'])) $arg['prefix'] = stripslashes($options['prefix']);
  65. if (!isset($arg['suffix'])) $arg['suffix'] = stripslashes($options['suffix']);
  66. if (!isset($arg['output_template'])) $arg['output_template'] = stripslashes($options['output_template']);
  67. // an empty output_template makes no sense so we fall back to the default
  68. if ($arg['output_template'] == '') $arg['output_template'] = $default_output_template;
  69. if (!isset($arg['match_cat'])) $arg['match_cat'] = $options['match_cat'];
  70. if (!isset($arg['match_tags'])) $arg['match_tags'] = $options['match_tags'];
  71. if (!isset($arg['age'])) $arg['age'] = $options['age'];
  72. if (!isset($arg['custom'])) $arg['custom'] = $options['custom'];
  73. // just for recent_comments
  74. if (!isset($arg['group_by'])) $arg['group_by'] = $options['group_by'];
  75. if (!isset($arg['group_template'])) $arg['group_template'] = stripslashes($options['group_template']);
  76. if (!isset($arg['show_type'])) $arg['show_type'] = $options['show_type'];
  77. if (!isset($arg['no_author_comments'])) $arg['no_author_comments'] = $options['no_author_comments'];
  78. if (!isset($arg['no_user_comments'])) $arg['no_user_comments'] = $options['no_user_comments'];
  79. // just for similar_posts[feed]
  80. if (!isset($arg['combine'])) $arg['combine'] = $options['crossmatch'];
  81. if (!isset($arg['weight_content'])) $arg['weight_content'] = $options['weight_content'];
  82. if (!isset($arg['weight_title'])) $arg['weight_title'] = $options['weight_title'];
  83. if (!isset($arg['weight_tags'])) $arg['weight_tags'] = $options['weight_tags'];
  84. if (!isset($arg['num_terms'])) $arg['num_terms'] = stripslashes($options['num_terms']);
  85. // the last options cannot be set via arguments
  86. $arg['stripcodes'] = $options['stripcodes'];
  87. $arg['utf8'] = $options['utf8'];
  88. $arg['use_stemmer'] = $options['use_stemmer'];
  89. $arg['batch'] = $options['batch'];
  90. //$arg['use_freq'] = $options['use_freq'];
  91. $arg['content_filter'] = $options['content_filter'];
  92. return $arg;
  93. }
  94. function ppl_prepare_template($template) {
  95. // Now we process the output_template to find the embedded tags which are to be replaced
  96. // with values taken from the database.
  97. // A tag is of the form, {tag:ext}, where the tag part will be evaluated and replaced
  98. // and the optional ext part provides extra data pertinent to that tag
  99. preg_match_all('/{((?:[^{}]|{[^{}]*})*)}/', $template, $matches);
  100. foreach ($matches[1] as $match) {
  101. list($tag, $ext) = explode(':', $match, 2);
  102. $action = output_tag_action($tag);
  103. if (function_exists($action)) {
  104. // store the action that instantiates the tag
  105. $translations['acts'][] = $action;
  106. // add the tag in a form ready to use in translation later
  107. $translations['fulltags'][] = '{'.$match.'}';
  108. // the extra data if any
  109. $translations['exts'][] = $ext;
  110. }
  111. }
  112. return $translations;
  113. }
  114. function xppl_prepare_template($template) {
  115. // Now we process the output_template to find the embedded tags which are to be replaced
  116. // with values taken from the database.
  117. // A tag is of the form, {tag:ext}, where the tag part will be evaluated and replaced
  118. // and the optional ext part provides extra data pertinent to that tag
  119. preg_match_all('/{(.+?)}/', $template, $matches);
  120. foreach ($matches[0] as $match) {
  121. list($tag, $ext) = explode(':', trim($match, '{}'), 2);
  122. $action = output_tag_action($tag);
  123. if (function_exists($action)) {
  124. // store the action that instantiates the tag
  125. $translations['acts'][] = $action;
  126. // add the tag in a form ready to use in translation later
  127. $translations['fulltags'][] = $match;
  128. // the extra data if any
  129. $translations['exts'][] = $ext;
  130. }
  131. }
  132. return $translations;
  133. }
  134. function ppl_expand_template($result, $template, $translations, $option_key) {
  135. global $wpdb, $wp_version;
  136. $replacements = array();
  137. $numtags = count($translations['fulltags']);
  138. for ($i = 0; $i < $numtags; $i++) {
  139. $fulltag = $translations['fulltags'][$i];
  140. $act = $translations['acts'][$i];
  141. $ext = $translations['exts'][$i];
  142. $replacements[$fulltag] = $act($option_key, $result, $ext);
  143. }
  144. // Replace every valid tag with its value
  145. $tmp = strtr($template, $replacements)."\n";
  146. return $tmp;
  147. }
  148. /*
  149. Functions to fill in the WHERE part of the workhorse SQL
  150. */
  151. function where_match_tags($match_tags) {
  152. global $wpdb, $wp_version, $post;
  153. $args = array('fields' => 'ids');
  154. $tag_ids = wp_get_object_terms($post->ID, 'post_tag', $args);
  155. if ( is_array($tag_ids) && count($tag_ids) > 0 ) {
  156. if ($match_tags === 'any') {
  157. $ids = get_objects_in_term($tag_ids, 'post_tag');
  158. } else {
  159. $ids = array();
  160. foreach ($tag_ids as $tag_id){
  161. if (count($ids) > 0) {
  162. $ids = array_intersect($ids, get_objects_in_term($tag_id, 'post_tag'));
  163. } else {
  164. $ids = get_objects_in_term($tag_id, 'post_tag');
  165. }
  166. }
  167. }
  168. if ( is_array($ids) && count($ids) > 0 ) {
  169. $ids = array_unique($ids);
  170. $out_posts = "'" . implode("', '", $ids) . "'";
  171. $sql = "$wpdb->posts.ID IN ($out_posts)";
  172. }
  173. } else {
  174. $sql = "1 = 2";
  175. }
  176. return $sql;
  177. }
  178. function where_show_pages($show_pages) {
  179. global $wp_version;
  180. if ($wp_version < 2.1) {
  181. if ($show_pages === 'true') $sql = "post_status IN ('publish', 'static')";
  182. else if ($show_pages === 'false') $sql = "post_status = 'publish'";
  183. else if ($show_pages === 'but') $sql = "post_status = 'static'";
  184. } else {
  185. if ($show_pages === 'true') $sql = "post_status='publish' AND post_type IN ('page', 'post')";
  186. else if ($show_pages === 'false') $sql = "post_status='publish' AND post_type='post'";
  187. else if ($show_pages === 'but') $sql = "post_status='publish' AND post_type='page'";
  188. }
  189. return $sql;
  190. }
  191. // a replacement, for WP < 2.3, ONLY category children
  192. if (!function_exists('get_term_children')) {
  193. function get_term_children($term, $taxonomy) {
  194. if ($taxonomies !== 'category') return array();
  195. return get_categories('child_of='.$term);
  196. }
  197. }
  198. // a replacement, for WP < 2.3, ONLY to get posts with given category IDs
  199. if (!function_exists('get_objects_in_term')) {
  200. function get_objects_in_term($terms, $taxonomies) {
  201. global $wpdb;
  202. if ($taxonomies !== 'category') return array();
  203. $terms = "'" . implode("', '", $terms) . "'";
  204. $object_ids = $wpdb->get_col("SELECT post_id FROM $wpdb->post2cat WHERE category_id IN ($terms)");
  205. if (!$object_ids) return array();
  206. return $object_ids;
  207. }
  208. }
  209. function where_match_category() {
  210. global $wpdb, $wp_version;
  211. $cat_ids = '';
  212. foreach(get_the_category() as $cat) {
  213. if ($cat->cat_ID) $cat_ids .= $cat->cat_ID . ',';
  214. }
  215. $cat_ids = rtrim($cat_ids, ',');
  216. $catarray = explode(',', $cat_ids);
  217. foreach ( $catarray as $cat ) {
  218. $catarray = array_merge($catarray, get_term_children($cat, 'category'));
  219. }
  220. $catarray = array_unique($catarray);
  221. $ids = get_objects_in_term($catarray, 'category');
  222. $ids = array_unique($ids);
  223. if ( is_array($ids) && count($ids) > 0 ) {
  224. $out_posts = "'" . implode("', '", $ids) . "'";
  225. $sql = "$wpdb->posts.ID IN ($out_posts)";
  226. } else {
  227. $sql = "1 = 2";
  228. }
  229. return $sql;
  230. }
  231. function where_included_cats($included_cats) {
  232. global $wpdb, $wp_version;
  233. $catarray = explode(',', $included_cats);
  234. foreach ( $catarray as $cat ) {
  235. $catarray = array_merge($catarray, get_term_children($cat, 'category'));
  236. }
  237. $catarray = array_unique($catarray);
  238. $ids = get_objects_in_term($catarray, 'category');
  239. if ( is_array($ids) && count($ids) > 0 ) {
  240. $ids = array_unique($ids);
  241. $in_posts = "'" . implode("', '", $ids) . "'";
  242. $sql = "$wpdb->posts.ID IN ($in_posts)";
  243. }
  244. return $sql;
  245. }
  246. function where_excluded_cats($excluded_cats) {
  247. global $wpdb, $wp_version;
  248. $catarray = explode(',', $excluded_cats);
  249. foreach ( $catarray as $cat ) {
  250. $catarray = array_merge($catarray, get_term_children($cat, 'category'));
  251. }
  252. $catarray = array_unique($catarray);
  253. $ids = get_objects_in_term($catarray, 'category');
  254. if ( is_array($ids) && count($ids) > 0 ) {
  255. $out_posts = "'" . implode("', '", $ids) . "'";
  256. $sql = "$wpdb->posts.ID NOT IN ($out_posts)";
  257. }
  258. return $sql;
  259. }
  260. function where_excluded_authors($excluded_authors){
  261. return "post_author NOT IN ( $excluded_authors )";
  262. }
  263. function where_included_authors($included_authors){
  264. return "post_author IN ( $included_authors )";
  265. }
  266. function where_excluded_posts($excluded_posts) {
  267. return "ID NOT IN ( $excluded_posts )";
  268. }
  269. function where_included_posts($included_posts) {
  270. return "ID IN ( $included_posts )";
  271. }
  272. function where_tag_str($tag_str) {
  273. global $wpdb;
  274. if ( strpos($tag_str, ',') !== false ) {
  275. $intags = explode(',', $tag_str);
  276. foreach ( (array) $intags as $tag ) {
  277. $tags[] = sanitize_term_field('name', $tag, 0, 'post_tag', 'db');
  278. }
  279. $tag_type = 'any';
  280. } else if ( strpos($tag_str, '+') !== false ) {
  281. $intags = explode('+', $tag_str);
  282. foreach ( (array) $intags as $tag ) {
  283. $tags[] = sanitize_term_field('name', $tag, 0, 'post_tag', 'db');
  284. }
  285. $tag_type = 'all';
  286. } else {
  287. $tags[] = sanitize_term_field('name', $tag_str, 0, 'post_tag', 'db');
  288. $tag_type = 'any';
  289. }
  290. $ids = array();
  291. if ($tag_type == 'any') {
  292. foreach ($tags as $tag){
  293. if (is_term($tag, 'post_tag')) {
  294. $t = get_term_by('name', $tag, 'post_tag');
  295. $ids = array_merge($ids, get_objects_in_term($t->term_id, 'post_tag'));
  296. }
  297. }
  298. } else {
  299. foreach ($tags as $tag){
  300. if (is_term($tag, 'post_tag')) {
  301. $t = get_term_by('name', $tag, 'post_tag');
  302. if (count($ids) > 0) {
  303. $ids = array_intersect($ids, get_objects_in_term($t->term_id, 'post_tag'));
  304. } else {
  305. $ids = get_objects_in_term($t->term_id, 'post_tag');
  306. }
  307. }
  308. }
  309. }
  310. if ( is_array($ids) && count($ids) > 0 ) {
  311. $ids = array_unique($ids);
  312. $out_posts = "'" . implode("', '", $ids) . "'";
  313. $sql .= "$wpdb->posts.ID IN ($out_posts)";
  314. } else $sql .= "1 = 2";
  315. return $sql;
  316. }
  317. function where_omit_post() {
  318. global $post, $ID;
  319. if (isset($post) && isset($post->ID)) {
  320. $postid = $post->ID;
  321. } else if (isset($ID)) {
  322. $postid = $ID;
  323. } else {
  324. $postid = -1;
  325. }
  326. return "ID != $postid";
  327. }
  328. function where_hide_pass() {
  329. return "post_password =''";
  330. }
  331. function where_hide_future() {
  332. global $wp_version;
  333. if ($wp_version < 2.1) {
  334. $time_difference = get_option('gmt_offset');
  335. $now = gmdate("Y-m-d H:i:s",(time()+($time_difference*3600)));
  336. $sql = "post_date <= '$now'";
  337. } else {
  338. $sql = "post_status != 'future'";
  339. }
  340. return $sql;
  341. }
  342. function where_fulltext_match($weight_title, $titleterms, $weight_content, $contentterms, $weight_tags, $tagterms) {
  343. $wsql = array();
  344. if ($weight_title) $wsql[] = "MATCH (`title`) AGAINST ( \"$titleterms\" )";
  345. if ($weight_content) $wsql[] = "MATCH (`content`) AGAINST ( \"$contentterms\" )";
  346. if ($weight_tags) $wsql[] = "MATCH (`tags`) AGAINST ( \"$tagterms\" )";
  347. return '(' . implode(' OR ', $wsql) . ') ' ;
  348. }
  349. function xwhere_fulltext_match($weight_title, $titleterms, $weight_content, $contentterms, $weight_tags, $tagterms) {
  350. $wsql = array();
  351. if ($weight_title) $wsql[] = "MATCH (`title`) AGAINST ( \"$titleterms\" )";
  352. $contentterms = implode(' ', array_keys($contentterms));
  353. if ($weight_content) $wsql[] = "MATCH (`content`) AGAINST ( \"$contentterms\" )";
  354. if ($weight_tags) $wsql[] = "MATCH (`tags`) AGAINST ( \"$tagterms\" )";
  355. return '(' . implode(' OR ', $wsql) . ') ' ;
  356. }
  357. function where_author_comments() {
  358. $author_email = get_the_author_email();
  359. return "'$author_email' != comment_author_email";
  360. }
  361. function where_user_comments() {
  362. return "user_id = 0";
  363. }
  364. function score_fulltext_match($table_name, $weight_title, $titleterms, $weight_content, $contentterms, $weight_tags, $tagterms) {
  365. global $wpdb;
  366. $wsql = array();
  367. if ($weight_title) $wsql[] = "($weight_title * (MATCH (`title`) AGAINST ( \"$titleterms\" )))";
  368. if ($weight_content) $wsql[] = "($weight_content * (MATCH (`content`) AGAINST ( \"$contentterms\" )))";
  369. if ($weight_tags) $wsql[] = "($weight_tags * (MATCH (`tags`) AGAINST ( \"$tagterms\" )))";
  370. return '(' . implode(' + ', $wsql) . " ) as score FROM `$table_name` LEFT JOIN `$wpdb->posts` ON `pID` = `ID` ";
  371. }
  372. function xscore_fulltext_match($table_name, $weight_title, $titleterms, $weight_content, $contentterms, $weight_tags, $tagterms) {
  373. global $wpdb;
  374. $wsql = array();
  375. if ($weight_title) $wsql[] = "($weight_title * (MATCH (`title`) AGAINST ( \"$titleterms\" )))";
  376. if ($weight_content) {
  377. foreach($contentterms as $word => $score) {
  378. $weight = $weight_content * sqrt($score);
  379. $wsql[] = "($weight * (MATCH (`content`) AGAINST ( \"$word\" )))";
  380. }
  381. }
  382. if ($weight_tags) $wsql[] = "($weight_tags * (MATCH (`tags`) AGAINST ( \"$tagterms\" )))";
  383. return '(' . implode(' + ', $wsql) . " ) as score FROM `$table_name` LEFT JOIN `$wpdb->posts` ON `pID` = `ID` ";
  384. }
  385. function where_comment_type($comment_type) {
  386. if ($comment_type === 'comments') $sql = "comment_type = ''";
  387. elseif ($comment_type === 'trackbacks') $sql = "comment_type != ''";
  388. return $sql;
  389. }
  390. function where_check_age($direction, $length, $duration) {
  391. global $wp_version;
  392. if ('none' === $direction) return '';
  393. $age = "DATE_SUB(CURDATE(), INTERVAL $length $duration)";
  394. // we only filter out posts based on age, not pages
  395. if ('before' === $direction) {
  396. if ($wp_version < 2.1) {
  397. return "(post_date <= $age OR post_status='static')";
  398. } else {
  399. return "(post_date <= $age OR post_type='page')";
  400. }
  401. } else {
  402. if ($wp_version < 2.1) {
  403. return "(post_date >= $age OR post_status='static')";
  404. } else {
  405. return "(post_date >= $age OR post_type='page')";
  406. }
  407. }
  408. }
  409. //"(meta_key = '_edit_last' && meta_value = '2')";
  410. function where_check_custom($key, $op, $value) {
  411. if ($op === 'EXISTS') {
  412. return "meta_key = '$key'";
  413. } else {
  414. return "(meta_key = '$key' && meta_value $op '$value')";
  415. }
  416. }
  417. /*
  418. End of SQL functions
  419. */
  420. function ppl_microtime() {
  421. list($usec, $sec) = explode(" ", microtime());
  422. return ((float)$usec + (float)$sec);
  423. }
  424. /*
  425. Now some routines to handle content filtering
  426. */
  427. // the '|'-separated list of valid content filter tags
  428. global $ppl_filter_tags;
  429. // each plugin calls this on startup to have content scanned for its own tag
  430. function ppl_register_content_filter($tag) {
  431. global $ppl_filter_tags;
  432. if (!$ppl_filter_tags) {
  433. $ppl_filter_tags = $tag;
  434. } else {
  435. $tags = explode('|', $ppl_filter_tags);
  436. $tags[] = $tag;
  437. $tags = array_unique($tags);
  438. $ppl_filter_tags = implode('|', $tags);
  439. }
  440. }
  441. function ppl_content_filter($content) {
  442. global $ppl_filter_tags;
  443. // replaces every instance of "<!--RecentPosts-->", for example, with the output of the plugin
  444. $content = preg_replace("/(<!\s*--\s*)($ppl_filter_tags)(\s*--\s*>)/e", "\\2::execute()", $content);
  445. return $content;
  446. }
  447. function ppl_content_filter_init() {
  448. global $ppl_filter_tags;
  449. if (!$ppl_filter_tags) return;
  450. add_filter( 'the_content', 'ppl_content_filter', 5 );
  451. add_filter( 'the_content_rss', 'ppl_content_filter', 5 );
  452. add_filter( 'the_excerpt', 'ppl_content_filter', 5 );
  453. add_filter( 'the_excerpt_rss', 'ppl_content_filter', 5 );
  454. add_filter( 'widget_text', 'ppl_content_filter', 5 );
  455. }
  456. // watch out that the registration fucntion are called earlier
  457. add_action ('init', 'ppl_content_filter_init');
  458. ?>