/wp-content/plugins/all-in-one-seo-pack/app/Common/Api/Tools.php

https://gitlab.com/ebrjose/comcebu · PHP · 258 lines · 153 code · 33 blank · 72 comment · 12 complexity · a0edde472ebf820a907a1403f3205765 MD5 · raw file

  1. <?php
  2. namespace AIOSEO\Plugin\Common\Api;
  3. // Exit if accessed directly.
  4. if ( ! defined( 'ABSPATH' ) ) {
  5. exit;
  6. }
  7. use AIOSEO\Plugin\Common\Models;
  8. use AIOSEO\Plugin\Common\Tools as CommonTools;
  9. /**
  10. * Route class for the API.
  11. *
  12. * @since 4.0.0
  13. */
  14. class Tools {
  15. /**
  16. * Import and delete the static robots.txt.
  17. *
  18. * @since 4.0.0
  19. *
  20. * @param \WP_REST_Request $request The REST Request
  21. * @return \WP_REST_Response The response.
  22. */
  23. public static function importRobotsTxt( $request ) {
  24. $body = $request->get_json_params();
  25. $network = ! empty( $body['network'] ) ? (bool) $body['network'] : false;
  26. if ( ! aioseo()->robotsTxt->importPhysicalRobotsTxt( $network ) ) {
  27. return new \WP_REST_Response( [
  28. 'success' => false,
  29. 'message' => __( 'There was an error importing the physical robots.txt file.', 'all-in-one-seo-pack' )
  30. ], 400 );
  31. }
  32. aioseo()->options->tools->robots->enable = true;
  33. if ( ! aioseo()->robotsTxt->deletePhysicalRobotsTxt() ) {
  34. return new \WP_REST_Response( [
  35. 'success' => false,
  36. 'message' => __( 'There was an error deleting the physical robots.txt file.', 'all-in-one-seo-pack' )
  37. ], 400 );
  38. }
  39. Models\Notification::deleteNotificationByName( 'robots-physical-file' );
  40. return new \WP_REST_Response( [
  41. 'success' => true,
  42. 'notifications' => Models\Notification::getNotifications()
  43. ], 200 );
  44. }
  45. /**
  46. * Delete the static robots.txt.
  47. *
  48. * @since 4.0.0
  49. *
  50. * @param \WP_REST_Request $request The REST Request
  51. * @return \WP_REST_Response The response.
  52. */
  53. public static function deleteRobotsTxt() {
  54. if ( ! aioseo()->robotsTxt->deletePhysicalRobotsTxt() ) {
  55. return new \WP_REST_Response( [
  56. 'success' => false,
  57. 'message' => __( 'There was an error deleting the physical robots.txt file.', 'all-in-one-seo-pack' )
  58. ], 400 );
  59. }
  60. Models\Notification::deleteNotificationByName( 'robots-physical-file' );
  61. return new \WP_REST_Response( [
  62. 'success' => true,
  63. 'notifications' => Models\Notification::getNotifications()
  64. ], 200 );
  65. }
  66. /**
  67. * Email debug info.
  68. *
  69. * @since 4.0.0
  70. *
  71. * @param \WP_REST_Request $request The REST Request
  72. * @return \WP_REST_Response The response.
  73. */
  74. public static function emailDebugInfo( $request ) {
  75. $body = $request->get_json_params();
  76. $email = ! empty( $body['email'] ) ? $body['email'] : null;
  77. if ( ! filter_var( $email, FILTER_VALIDATE_EMAIL ) ) {
  78. return new \WP_REST_Response( [
  79. 'success' => false,
  80. 'message' => 'invalid-email-address'
  81. ], 400 );
  82. }
  83. require_once ABSPATH . 'wp-admin/includes/update.php';
  84. // Translators: 1 - The plugin name ("All in One SEO"), 2 - The Site URL.
  85. $html = sprintf( __( '%1$s Debug Info from %2$s', 'all-in-one-seo-pack' ), AIOSEO_PLUGIN_NAME, aioseo()->helpers->getSiteDomain() ) . "\r\n------------------\r\n\r\n";
  86. $info = CommonTools\SystemStatus::getSystemStatusInfo();
  87. foreach ( $info as $group ) {
  88. if ( empty( $group['results'] ) ) {
  89. continue;
  90. }
  91. $html .= "\r\n\r\n{$group['label']}\r\n";
  92. foreach ( $group['results'] as $data ) {
  93. $html .= "{$data['header']}: {$data['value']}\r\n";
  94. }
  95. }
  96. if ( ! wp_mail(
  97. $email,
  98. // Translators: 1 - The plugin name ("All in One SEO).
  99. sprintf( __( '%1$s Debug Info', 'all-in-one-seo-pack' ), AIOSEO_PLUGIN_NAME ),
  100. $html
  101. ) ) {
  102. return new \WP_REST_Response( [
  103. 'success' => false,
  104. 'message' => __( 'Unable to send debug email, please check your email send settings and try again.', 'all-in-one-seo-pack' )
  105. ], 400 );
  106. }
  107. return new \WP_REST_Response( [
  108. 'success' => true
  109. ], 200 );
  110. }
  111. /**
  112. * Create a settings backup.
  113. *
  114. * @since 4.0.0
  115. *
  116. * @param \WP_REST_Request $request The REST Request
  117. * @return \WP_REST_Response The response.
  118. */
  119. public static function createBackup() {
  120. aioseo()->backup->create();
  121. return new \WP_REST_Response( [
  122. 'success' => true,
  123. 'backups' => array_reverse( aioseo()->backup->all() )
  124. ], 200 );
  125. }
  126. /**
  127. * Restore a settings backup.
  128. *
  129. * @since 4.0.0
  130. *
  131. * @param \WP_REST_Request $request The REST Request
  132. * @return \WP_REST_Response The response.
  133. */
  134. public static function restoreBackup( $request ) {
  135. $body = $request->get_json_params();
  136. $backup = ! empty( $body['backup'] ) ? (int) $body['backup'] : null;
  137. if ( empty( $backup ) ) {
  138. return new \WP_REST_Response( [
  139. 'success' => false,
  140. 'backups' => array_reverse( aioseo()->backup->all() )
  141. ], 400 );
  142. }
  143. aioseo()->backup->restore( $backup );
  144. return new \WP_REST_Response( [
  145. 'success' => true,
  146. 'backups' => array_reverse( aioseo()->backup->all() ),
  147. 'options' => aioseo()->options->all(),
  148. 'internalOptions' => aioseo()->internalOptions->all()
  149. ], 200 );
  150. }
  151. /**
  152. * Delete a settings backup.
  153. *
  154. * @since 4.0.0
  155. *
  156. * @param \WP_REST_Request $request The REST Request
  157. * @return \WP_REST_Response The response.
  158. */
  159. public static function deleteBackup( $request ) {
  160. $body = $request->get_json_params();
  161. $backup = ! empty( $body['backup'] ) ? (int) $body['backup'] : null;
  162. if ( empty( $backup ) ) {
  163. return new \WP_REST_Response( [
  164. 'success' => false,
  165. 'backups' => array_reverse( aioseo()->backup->all() )
  166. ], 400 );
  167. }
  168. aioseo()->backup->delete( $backup );
  169. return new \WP_REST_Response( [
  170. 'success' => true,
  171. 'backups' => array_reverse( aioseo()->backup->all() )
  172. ], 200 );
  173. }
  174. /**
  175. * Save the .htaccess file.
  176. *
  177. * @since 4.0.0
  178. *
  179. * @param \WP_REST_Request $request The REST Request
  180. * @return \WP_REST_Response The response.
  181. */
  182. public static function saveHtaccess( $request ) {
  183. $body = $request->get_json_params();
  184. $htaccess = ! empty( $body['htaccess'] ) ? sanitize_textarea_field( $body['htaccess'] ) : '';
  185. if ( empty( $htaccess ) ) {
  186. return new \WP_REST_Response( [
  187. 'success' => false,
  188. 'message' => __( '.htaccess file is empty.', 'all-in-one-seo-pack' )
  189. ], 400 );
  190. }
  191. $htaccess = aioseo()->helpers->decodeHtmlEntities( $htaccess );
  192. if ( ! aioseo()->htaccess->saveContents( $htaccess ) ) {
  193. return new \WP_REST_Response( [
  194. 'success' => false,
  195. 'message' => __( 'An error occurred while trying to write to the .htaccess file. Please try again later.', 'all-in-one-seo-pack' )
  196. ], 400 );
  197. }
  198. return new \WP_REST_Response( [
  199. 'success' => true
  200. ], 200 );
  201. }
  202. /**
  203. * Clear the passed in log.
  204. *
  205. * @since 4.0.0
  206. *
  207. * @param \WP_REST_Request $request The REST Request
  208. * @return \WP_REST_Response The response.
  209. */
  210. public static function clearLog( $request ) {
  211. $body = $request->get_json_params();
  212. $log = ! empty( $body['log'] ) ? $body['log'] : null;
  213. $logSize = 0;
  214. switch ( $log ) {
  215. case 'badBotBlockerLog':
  216. aioseo()->badBotBlocker->clearLog();
  217. $logSize = aioseo()->badBotBlocker->getLogSize();
  218. break;
  219. }
  220. return new \WP_REST_Response( [
  221. 'success' => true,
  222. 'logSize' => aioseo()->helpers->convertFileSize( $logSize )
  223. ], 200 );
  224. }
  225. }