/htdocs/wp-content/plugins/redirection/models/importer.php

https://gitlab.com/VTTE/sitios-vtte · PHP · 425 lines · 333 code · 89 blank · 3 comment · 33 complexity · f30face2c6c234a1b2653673ef295104 MD5 · raw file

  1. <?php
  2. class Red_Plugin_Importer {
  3. public static function get_plugins() {
  4. $results = array();
  5. $importers = array(
  6. 'wp-simple-redirect',
  7. 'seo-redirection',
  8. 'safe-redirect-manager',
  9. 'wordpress-old-slugs',
  10. 'rank-math',
  11. 'quick-redirects',
  12. );
  13. foreach ( $importers as $importer ) {
  14. $importer = self::get_importer( $importer );
  15. $results[] = $importer->get_data();
  16. }
  17. return array_values( array_filter( $results ) );
  18. }
  19. public static function get_importer( $id ) {
  20. if ( $id === 'wp-simple-redirect' ) {
  21. return new Red_Simple301_Importer();
  22. }
  23. if ( $id === 'seo-redirection' ) {
  24. return new Red_SeoRedirection_Importer();
  25. }
  26. if ( $id === 'safe-redirect-manager' ) {
  27. return new Red_SafeRedirectManager_Importer();
  28. }
  29. if ( $id === 'wordpress-old-slugs' ) {
  30. return new Red_WordPressOldSlug_Importer();
  31. }
  32. if ( $id === 'rank-math' ) {
  33. return new Red_RankMath_Importer();
  34. }
  35. if ( $id === 'quick-redirects' ) {
  36. return new Red_QuickRedirect_Importer();
  37. }
  38. return false;
  39. }
  40. public static function import( $plugin, $group_id ) {
  41. $importer = self::get_importer( $plugin );
  42. if ( $importer ) {
  43. return $importer->import_plugin( $group_id );
  44. }
  45. return 0;
  46. }
  47. }
  48. class Red_QuickRedirect_Importer extends Red_Plugin_Importer {
  49. public function import_plugin( $group_id ) {
  50. $redirects = get_option( 'quickppr_redirects' );
  51. $count = 0;
  52. foreach ( $redirects as $source => $target ) {
  53. $item = $this->create_for_item( $group_id, $source, $target );
  54. if ( $item ) {
  55. $count++;
  56. }
  57. }
  58. return $count;
  59. }
  60. private function create_for_item( $group_id, $source, $target ) {
  61. $item = array(
  62. 'url' => $source,
  63. 'action_data' => array( 'url' => $target ),
  64. 'regex' => false,
  65. 'group_id' => $group_id,
  66. 'match_type' => 'url',
  67. 'action_type' => 'url',
  68. 'action_code' => 301,
  69. );
  70. return Red_Item::create( $item );
  71. }
  72. public function get_data() {
  73. $data = get_option( 'quickppr_redirects' );
  74. if ( $data ) {
  75. return array(
  76. 'id' => 'quick-redirects',
  77. 'name' => 'Quick Page/Post Redirects',
  78. 'total' => count( $data ),
  79. );
  80. }
  81. return false;
  82. }
  83. }
  84. class Red_RankMath_Importer extends Red_Plugin_Importer {
  85. public function import_plugin( $group_id ) {
  86. global $wpdb;
  87. $count = 0;
  88. $redirects = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}rank_math_redirections" );
  89. foreach ( $redirects as $redirect ) {
  90. $created = $this->create_for_item( $group_id, $redirect );
  91. $count += $created;
  92. }
  93. return $count;
  94. }
  95. private function create_for_item( $group_id, $redirect ) {
  96. // phpcs:ignore
  97. $sources = unserialize( $redirect->sources );
  98. $items = [];
  99. foreach ( $sources as $source ) {
  100. $url = $source['pattern'];
  101. if ( substr( $url, 0, 1 ) !== '/' ) {
  102. $url = '/' . $url;
  103. }
  104. $data = array(
  105. 'url' => $url,
  106. 'action_data' => array( 'url' => str_replace( '\\\\', '\\', $redirect->url_to ) ),
  107. 'regex' => $source['comparison'] === 'regex' ? true : false,
  108. 'group_id' => $group_id,
  109. 'match_type' => 'url',
  110. 'action_type' => 'url',
  111. 'action_code' => $redirect->header_code,
  112. );
  113. $items[] = Red_Item::create( $data );
  114. }
  115. return count( $items );
  116. }
  117. public function get_data() {
  118. global $wpdb;
  119. if ( defined( 'REDIRECTION_TESTS' ) && REDIRECTION_TESTS ) {
  120. return 0;
  121. }
  122. if ( ! function_exists( 'is_plugin_active' ) ) {
  123. require_once ABSPATH . 'wp-admin/includes/plugin.php';
  124. }
  125. $total = 0;
  126. if ( is_plugin_active( 'seo-by-rank-math/rank-math.php' ) ) {
  127. $total = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}rank_math_redirections" );
  128. }
  129. if ( $total ) {
  130. return array(
  131. 'id' => 'rank-math',
  132. 'name' => 'RankMath',
  133. 'total' => intval( $total, 10 ),
  134. );
  135. }
  136. return 0;
  137. }
  138. }
  139. class Red_Simple301_Importer extends Red_Plugin_Importer {
  140. public function import_plugin( $group_id ) {
  141. $redirects = get_option( '301_redirects' );
  142. $count = 0;
  143. foreach ( $redirects as $source => $target ) {
  144. $item = $this->create_for_item( $group_id, $source, $target );
  145. if ( $item ) {
  146. $count++;
  147. }
  148. }
  149. return $count;
  150. }
  151. private function create_for_item( $group_id, $source, $target ) {
  152. $item = array(
  153. 'url' => str_replace( '*', '(.*?)', $source ),
  154. 'action_data' => array( 'url' => str_replace( '*', '$1', trim( $target ) ) ),
  155. 'regex' => strpos( $source, '*' ) === false ? false : true,
  156. 'group_id' => $group_id,
  157. 'match_type' => 'url',
  158. 'action_type' => 'url',
  159. 'action_code' => 301,
  160. );
  161. return Red_Item::create( $item );
  162. }
  163. public function get_data() {
  164. $data = get_option( '301_redirects' );
  165. if ( $data ) {
  166. return array(
  167. 'id' => 'wp-simple-redirect',
  168. 'name' => 'Simple 301 Redirects',
  169. 'total' => count( $data ),
  170. );
  171. }
  172. return false;
  173. }
  174. }
  175. class Red_WordPressOldSlug_Importer extends Red_Plugin_Importer {
  176. public function import_plugin( $group_id ) {
  177. global $wpdb;
  178. $count = 0;
  179. $redirects = $wpdb->get_results(
  180. "SELECT {$wpdb->prefix}postmeta.* FROM {$wpdb->prefix}postmeta INNER JOIN {$wpdb->prefix}posts ON {$wpdb->prefix}posts.ID={$wpdb->prefix}postmeta.post_id " .
  181. "WHERE {$wpdb->prefix}postmeta.meta_key = '_wp_old_slug' AND {$wpdb->prefix}postmeta.meta_value != '' AND {$wpdb->prefix}posts.post_status='publish' AND {$wpdb->prefix}posts.post_type IN ('page', 'post')"
  182. );
  183. foreach ( $redirects as $redirect ) {
  184. $item = $this->create_for_item( $group_id, $redirect );
  185. if ( $item ) {
  186. $count++;
  187. }
  188. }
  189. return $count;
  190. }
  191. private function create_for_item( $group_id, $redirect ) {
  192. $new = get_permalink( $redirect->post_id );
  193. if ( is_wp_error( $new ) ) {
  194. return false;
  195. }
  196. $new_path = wp_parse_url( $new, PHP_URL_PATH );
  197. $old = rtrim( dirname( $new_path ), '/' ) . '/' . rtrim( $redirect->meta_value, '/' ) . '/';
  198. $old = str_replace( '\\', '', $old );
  199. $old = str_replace( '//', '/', $old );
  200. $data = array(
  201. 'url' => $old,
  202. 'action_data' => array( 'url' => $new ),
  203. 'regex' => false,
  204. 'group_id' => $group_id,
  205. 'match_type' => 'url',
  206. 'action_type' => 'url',
  207. 'action_code' => 301,
  208. );
  209. return Red_Item::create( $data );
  210. }
  211. public function get_data() {
  212. global $wpdb;
  213. $total = $wpdb->get_var(
  214. "SELECT COUNT(*) FROM {$wpdb->prefix}postmeta INNER JOIN {$wpdb->prefix}posts ON {$wpdb->prefix}posts.ID={$wpdb->prefix}postmeta.post_id WHERE {$wpdb->prefix}postmeta.meta_key = '_wp_old_slug' AND {$wpdb->prefix}postmeta.meta_value != '' AND {$wpdb->prefix}posts.post_status='publish' AND {$wpdb->prefix}posts.post_type IN ('page', 'post')"
  215. );
  216. if ( $total ) {
  217. return array(
  218. 'id' => 'wordpress-old-slugs',
  219. 'name' => __( 'Default WordPress "old slugs"', 'redirection' ),
  220. 'total' => intval( $total, 10 ),
  221. );
  222. }
  223. return false;
  224. }
  225. }
  226. class Red_SeoRedirection_Importer extends Red_Plugin_Importer {
  227. public function import_plugin( $group_id ) {
  228. global $wpdb;
  229. if ( defined( 'REDIRECTION_TESTS' ) && REDIRECTION_TESTS ) {
  230. return 0;
  231. }
  232. $count = 0;
  233. $redirects = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}WP_SEO_Redirection" );
  234. foreach ( $redirects as $redirect ) {
  235. $item = $this->create_for_item( $group_id, $redirect );
  236. if ( $item ) {
  237. $count++;
  238. }
  239. }
  240. return $count;
  241. }
  242. private function create_for_item( $group_id, $seo ) {
  243. if ( intval( $seo->enabled, 10 ) === 0 ) {
  244. return false;
  245. }
  246. $data = array(
  247. 'url' => $seo->regex ? $seo->regex : $seo->redirect_from,
  248. 'action_data' => array( 'url' => $seo->redirect_to ),
  249. 'regex' => $seo->regex ? true : false,
  250. 'group_id' => $group_id,
  251. 'match_type' => 'url',
  252. 'action_type' => 'url',
  253. 'action_code' => intval( $seo->redirect_type, 10 ),
  254. );
  255. return Red_Item::create( $data );
  256. }
  257. public function get_data() {
  258. global $wpdb;
  259. $plugins = get_option( 'active_plugins', array() );
  260. $found = false;
  261. foreach ( $plugins as $plugin ) {
  262. if ( strpos( $plugin, 'seo-redirection.php' ) !== false ) {
  263. $found = true;
  264. break;
  265. }
  266. }
  267. if ( $found ) {
  268. $total = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}WP_SEO_Redirection" );
  269. return array(
  270. 'id' => 'seo-redirection',
  271. 'name' => 'SEO Redirection',
  272. 'total' => $total,
  273. );
  274. }
  275. return false;
  276. }
  277. }
  278. class Red_SafeRedirectManager_Importer extends Red_Plugin_Importer {
  279. public function import_plugin( $group_id ) {
  280. global $wpdb;
  281. $count = 0;
  282. $redirects = $wpdb->get_results(
  283. "SELECT {$wpdb->prefix}postmeta.* FROM {$wpdb->prefix}postmeta INNER JOIN {$wpdb->prefix}posts ON {$wpdb->prefix}posts.ID={$wpdb->prefix}postmeta.post_id WHERE {$wpdb->prefix}postmeta.meta_key LIKE '_redirect_rule_%' AND {$wpdb->prefix}posts.post_status='publish'"
  284. );
  285. // Group them by post ID
  286. $by_post = array();
  287. foreach ( $redirects as $redirect ) {
  288. if ( ! isset( $by_post[ $redirect->post_id ] ) ) {
  289. $by_post[ $redirect->post_id ] = array();
  290. }
  291. $by_post[ $redirect->post_id ][ str_replace( '_redirect_rule_', '', $redirect->meta_key ) ] = $redirect->meta_value;
  292. }
  293. // Now go through the redirects
  294. foreach ( $by_post as $post ) {
  295. $item = $this->create_for_item( $group_id, $post );
  296. if ( $item ) {
  297. $count++;
  298. }
  299. }
  300. return $count;
  301. }
  302. private function create_for_item( $group_id, $post ) {
  303. $regex = false;
  304. $source = $post['from'];
  305. if ( strpos( $post['from'], '*' ) !== false ) {
  306. $regex = true;
  307. $source = str_replace( '*', '.*', $source );
  308. } elseif ( isset( $post['from_regex'] ) && $post['from_regex'] === '1' ) {
  309. $regex = true;
  310. }
  311. $data = array(
  312. 'url' => $source,
  313. 'action_data' => array( 'url' => $post['to'] ),
  314. 'regex' => $regex,
  315. 'group_id' => $group_id,
  316. 'match_type' => 'url',
  317. 'action_type' => 'url',
  318. 'action_code' => intval( $post['status_code'], 10 ),
  319. );
  320. return Red_Item::create( $data );
  321. }
  322. public function get_data() {
  323. global $wpdb;
  324. $total = $wpdb->get_var(
  325. "SELECT COUNT(*) FROM {$wpdb->prefix}postmeta INNER JOIN {$wpdb->prefix}posts ON {$wpdb->prefix}posts.ID={$wpdb->prefix}postmeta.post_id WHERE {$wpdb->prefix}postmeta.meta_key = '_redirect_rule_from' AND {$wpdb->prefix}posts.post_status='publish'"
  326. );
  327. if ( $total ) {
  328. return array(
  329. 'id' => 'safe-redirect-manager',
  330. 'name' => 'Safe Redirect Manager',
  331. 'total' => intval( $total, 10 ),
  332. );
  333. }
  334. return false;
  335. }
  336. }