PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/iwp-client/addons/post_links/link.class.php

https://github.com/louieDA/bestilblomster
PHP | 182 lines | 137 code | 33 blank | 12 comment | 20 complexity | 7034fab563b950b1c41246f44a5a468e MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, GPL-3.0, BSD-3-Clause, AGPL-1.0
  1. <?php
  2. /*************************************************************
  3. *
  4. * link.class.php
  5. *
  6. * Manage/Add Links
  7. *
  8. *
  9. * Copyright (c) 2011 Prelovac Media
  10. * www.prelovac.com
  11. **************************************************************/
  12. if(basename($_SERVER['SCRIPT_FILENAME']) == "link.class.php"):
  13. exit;
  14. endif;
  15. class IWP_MMB_Link extends IWP_MMB_Core
  16. {
  17. function __construct()
  18. {
  19. parent::__construct();
  20. }
  21. function add_link($args)
  22. {
  23. extract($args);
  24. $params['link_url'] = esc_html($url);
  25. $params['link_url'] = esc_url($params['link_url']);
  26. $params['link_name'] = esc_html($name);
  27. $params['link_id'] = '';
  28. $params['link_description'] = $description;
  29. $params['link_target'] = $link_target;
  30. $params['link_category'] = array();
  31. //Add Link category
  32. if(is_array($link_category) && !empty($link_category)){
  33. $terms = get_terms('link_category',array('hide_empty' => 0));
  34. if($terms){
  35. foreach($terms as $term){
  36. if(in_array($term->name,$link_category)){
  37. $params['link_category'][] = $term->term_id;
  38. $link_category = $this->remove_element($link_category, $term->name);
  39. }
  40. }
  41. }
  42. if(!empty($link_category)){
  43. foreach($link_category as $linkkey => $linkval){
  44. if(!empty($linkval)){
  45. $link = wp_insert_term($linkval,'link_category');
  46. if(isset($link['term_id']) && !empty($link['term_id'])){
  47. $params['link_category'][] = $link['term_id'];
  48. }
  49. }
  50. }
  51. }
  52. }
  53. //Add Link Owner
  54. $user_obj = get_userdatabylogin($user);
  55. if($user_obj && $user_obj->ID){
  56. $params['link_owner'] = $user_obj->ID;
  57. }
  58. if(!function_exists('wp_insert_link'))
  59. include_once (ABSPATH . 'wp-admin/includes/bookmark.php');
  60. $is_success = wp_insert_link($params);
  61. return $is_success ? true : array('error' => 'Failed to add link.');
  62. }
  63. function remove_element($arr, $val){
  64. foreach ($arr as $key => $value){
  65. if ($value == $val){
  66. unset($arr[$key]);
  67. }
  68. }
  69. return $arr = array_values($arr);
  70. }
  71. function get_links($args){
  72. global $wpdb;
  73. $where='';
  74. extract($args);
  75. if(!empty($filter_links))
  76. {
  77. $where.=" AND (link_name LIKE '%".mysql_real_escape_string($filter_links)."%' OR link_url LIKE '%".mysql_real_escape_string($filter_links)."%')";
  78. }
  79. $linkcats = $this->getLinkCats();
  80. $sql_query = "$wpdb->links WHERE 1=1 ".$where;
  81. $links_total = $wpdb->get_results("SELECT count(*) as total_links FROM ".$sql_query);
  82. $total=$links_total[0]->total_links;
  83. $query_links = $wpdb->get_results("SELECT link_id, link_url, link_name, link_target, link_visible, link_rating, link_rel FROM ".$sql_query." ORDER BY link_name ASC LIMIT 500");
  84. $links = array();
  85. foreach ( $query_links as $link_info )
  86. {
  87. $link_cat = $linkcats[$link_info->link_id];
  88. $cats = array();
  89. foreach($link_cat as $catkey=>$catval)
  90. {
  91. $cats[] = $catval;
  92. }
  93. $links[$link_info->link_id] = array(
  94. "link_url" => $link_info->link_url,
  95. "link_name" => $link_info->link_name,
  96. "link_target" => $link_info->link_target,
  97. "link_visible" => $link_info->link_visible,
  98. "link_rating" => $link_info->link_rating,
  99. "link_rel" => $link_info->link_rel,
  100. "link_cats" => $cats
  101. );
  102. }
  103. return array('links' => $links, 'total' => $total);
  104. }
  105. function getLinkCats($taxonomy = 'link_category')
  106. {
  107. global $wpdb;
  108. $cats = $wpdb->get_results("SELECT l.link_id, $wpdb->terms.name
  109. FROM $wpdb->links AS l
  110. INNER JOIN $wpdb->term_relationships ON ( l.link_id = $wpdb->term_relationships.object_id )
  111. INNER JOIN $wpdb->term_taxonomy ON ( $wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id
  112. AND $wpdb->term_taxonomy.taxonomy = '".$taxonomy."' )
  113. INNER JOIN $wpdb->terms ON ( $wpdb->term_taxonomy.term_id = $wpdb->terms.term_id )");
  114. foreach ( $cats as $post_val )
  115. {
  116. $post_cats[$post_val->link_id][] = $post_val->name;
  117. }
  118. return $post_cats;
  119. }
  120. function delete_link($args){
  121. global $wpdb;
  122. if(!empty($args['link_id']))
  123. {
  124. $delete_query = "DELETE FROM $wpdb->links WHERE link_id = ".$args['link_id'];
  125. $wpdb->get_results($delete_query);
  126. return 'Link deleted.';
  127. }
  128. else
  129. {
  130. return 'No ID...';
  131. }
  132. }
  133. function delete_links($args){
  134. global $wpdb;
  135. extract($args);
  136. if($deleteaction=='delete'){
  137. $delete_query_intro = "DELETE FROM $wpdb->links WHERE link_id = ";
  138. }
  139. foreach($args as $key=>$val){
  140. if(!empty($val) && is_numeric($val))
  141. {
  142. $delete_query = $delete_query_intro.$val;
  143. $wpdb->query($delete_query);
  144. }
  145. }
  146. return "Link deleted";
  147. }
  148. }
  149. ?>