PageRenderTime 59ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/simple-history/loggers/SimpleOptionsLogger.php

https://bitbucket.org/GraphicFusion/crafti-landing
PHP | 506 lines | 273 code | 143 blank | 90 comment | 54 complexity | 774f6a36049c34e84d50b05a500b7c78 MD5 | raw file
Possible License(s): LGPL-3.0, GPL-2.0, Apache-2.0, GPL-3.0
  1. <?php
  2. defined( 'ABSPATH' ) or die();
  3. /**
  4. * Logs changes to wordpress options
  5. */
  6. class SimpleOptionsLogger extends SimpleLogger
  7. {
  8. public $slug = __CLASS__;
  9. /**
  10. * Get array with information about this logger
  11. *
  12. * @return array
  13. */
  14. function getInfo() {
  15. $arr_info = array(
  16. "name" => "Options Logger",
  17. "description" => "Logs updates to WordPress settings",
  18. "capability" => "manage_options",
  19. "messages" => array(
  20. //'option_updated' => __('Updated option "{option}" on settings page "{option_page}"', "simple-history"),
  21. 'option_updated' => __('Updated option "{option}"', "simple-history"),
  22. /*
  23. Updated option "default_comment_status" on settings page "discussion"
  24. Edited option "default_comment_status" on settings page "discussion"
  25. Modified option "default_comment_status" on settings page "discussion"
  26. Edited settings page "discussion" and the "default_comment_status" options
  27. */
  28. ),
  29. "labels" => array(
  30. "search" => array(
  31. "label" => _x("Options", "Options logger: search", "simple-history"),
  32. "options" => array(
  33. _x("Changed options", "Options logger: search", "simple-history") => array(
  34. "option_updated"
  35. ),
  36. )
  37. ) // end search array
  38. ) // end labels
  39. );
  40. return $arr_info;
  41. }
  42. function loaded() {
  43. add_action( 'updated_option', array($this, "on_updated_option"), 10, 3 );
  44. }
  45. function on_updated_option( $option, $old_value, $new_value ) {
  46. if ( empty( $_SERVER["REQUEST_URI"] ) ) {
  47. return;
  48. }
  49. $arr_option_pages = array(
  50. 0 => "options.php",
  51. 1 => "options-permalink.php"
  52. );
  53. // We only want to log options being added via pages in $arr_option_pages
  54. if ( ! in_array( basename( $_SERVER["REQUEST_URI"] ), $arr_option_pages ) || basename( dirname( $_SERVER["REQUEST_URI"] ) ) !== "wp-admin" ) {
  55. return;
  56. }
  57. // Also only if "option_page" is set to one of these "built in" ones
  58. // We don't wanna start loging things from other plugins, like EDD
  59. $option_page = isset( $_REQUEST["option_page"] ) ? $_REQUEST["option_page"] : ""; // general | discussion | ...
  60. $arr_valid_option_pages = array(
  61. 'general',
  62. 'discussion',
  63. 'media',
  64. 'reading',
  65. 'writing',
  66. );
  67. $is_valid_options_page = $option_page && in_array( $option_page, $arr_valid_option_pages );
  68. // Permalink settings page does not post any "option_page", so use http referer instead
  69. if ( strpos( $_SERVER["REQUEST_URI"], "options-permalink.php" ) !== false ) {
  70. $is_valid_options_page = true;
  71. $options_page = "permalink";
  72. }
  73. if ( ! $is_valid_options_page ) {
  74. return;
  75. }
  76. // Check if option name is ok
  77. // For example if you change front page displays setting the "rewrite_rules" options gets updated too
  78. $arr_invalid_option_names = array(
  79. "rewrite_rules"
  80. );
  81. if ( in_array( $option, $arr_invalid_option_names ) ) {
  82. return;
  83. }
  84. $context = array(
  85. 'option' => $option,
  86. 'old_value' => $old_value,
  87. 'new_value' => $new_value,
  88. 'option_page' => $option_page,
  89. #'referer' => wp_get_referer(),
  90. #'REQUEST_URI' => $_SERVER['REQUEST_URI'],
  91. #'$_REQUEST' => print_r($_REQUEST, true),
  92. );
  93. // Store a bit more about some options
  94. // Like "page_on_front" we also store post title
  95. // Check for a method for current option in this class and calls it automagically
  96. $methodname = "add_context_for_option_{$option}";
  97. if ( method_exists( $this, $methodname ) ) {
  98. $context = $this->$methodname( $context, $old_value, $new_value, $option, $option_page );
  99. }
  100. $this->infoMessage( 'option_updated', $context );
  101. }
  102. /**
  103. * Give some options better plain text output
  104. *
  105. * Not doing anything at the moment, because it was really difficaly to give them meaningful text values
  106. */
  107. public function getLogRowPlainTextOutput( $row ) {
  108. $message = $row->message;
  109. $context = $row->context;
  110. $message_key = $context["_message_key"];
  111. $return_message = "";
  112. // Only link to attachment if it is still available
  113. if ( "option_updated" == $message_key ) {
  114. /*
  115. $option = isset( $context["option"] ) ? $context["option"] : null;
  116. $option_page = isset( $context["option_page"] ) ? $context["option_page"] : null;
  117. $new_value = isset( $context["new_value"] ) ? $context["new_value"] : null;
  118. $old_value = isset( $context["old_value"] ) ? $context["old_value"] : null;
  119. # $return_message = "";
  120. $arr_options_to_translate = array(
  121. "$option_page/blog_public" => array(
  122. "text" => "Updated setting Search Engine Visibility"
  123. ),
  124. "$option_page/rss_use_excerpt" => array(
  125. "text" => "Updated setting For each article in a feed, show"
  126. ),
  127. "$option_page/posts_per_rss" => array(
  128. "text" => "Updated setting for Syndication feeds show the most recent"
  129. ),
  130. "$option_page/posts_per_page" => array(
  131. "text" => "Updated setting for Blog pages show at most"
  132. )
  133. );
  134. if ( isset( $arr_options_to_translate[ "{$option_page}/{$option}" ] ) ) {
  135. $return_message = $arr_options_to_translate[ "{$option_page}/{$option}" ]["text"];
  136. }
  137. */
  138. }
  139. if ( empty( $return_message ) ) {
  140. // No specific text to output, fallback to default
  141. $return_message = parent::getLogRowPlainTextOutput( $row );
  142. }
  143. return $return_message;
  144. }
  145. /**
  146. * Get detailed output
  147. */
  148. function getLogRowDetailsOutput($row) {
  149. $context = $row->context;
  150. $message_key = $context["_message_key"];
  151. $output = "";
  152. $option = isset( $context["option"] ) ? $context["option"] : null;
  153. $option_page = isset( $context["option_page"] ) ? $context["option_page"] : null;
  154. $new_value = isset( $context["new_value"] ) ? $context["new_value"] : null;
  155. $old_value = isset( $context["old_value"] ) ? $context["old_value"] : null;
  156. $tmpl_row = '
  157. <tr>
  158. <td>%1$s</td>
  159. <td>%2$s</td>
  160. </tr>
  161. ';
  162. if ( "option_updated" == $message_key ) {
  163. //$message = 'Old value was {old_value} and new value is {new_value}';
  164. $output .= "<table class='SimpleHistoryLogitem__keyValueTable'>";
  165. // Output old and new values
  166. if ( $context["new_value"] || $context["old_value"] ) {
  167. $option_custom_output = "";
  168. $methodname = "get_details_output_for_option_{$option}";
  169. if ( method_exists( $this, $methodname ) ) {
  170. $option_custom_output = $this->$methodname( $context, $old_value, $new_value, $option, $option_page, $tmpl_row );
  171. }
  172. if ( empty( $option_custom_output ) ) {
  173. // all other options or fallback if custom output did not find all it's stuff
  174. $more = __( '&hellip;' );
  175. $trim_length = 250;
  176. $trimmed_new_value = substr( $new_value, 0, $trim_length );
  177. $trimmed_old_value = substr( $old_value, 0, $trim_length );
  178. if ( strlen( $new_value ) > $trim_length ) {
  179. $trimmed_new_value .= $more;
  180. }
  181. if ( strlen( $old_value ) > $trim_length ) {
  182. $trimmed_old_value .= $more;
  183. }
  184. $output .= sprintf(
  185. $tmpl_row,
  186. __("New value", "simple-history"),
  187. esc_html( $trimmed_new_value )
  188. );
  189. $output .= sprintf(
  190. $tmpl_row,
  191. __("Old value", "simple-history"),
  192. esc_html( $trimmed_old_value )
  193. );
  194. } else {
  195. $output .= $option_custom_output;
  196. } // if option output
  197. } // if new or old val
  198. // If key option_page this was saved from regular settings pages
  199. if ( ! empty( $option_page ) ) {
  200. $output .= sprintf(
  201. '
  202. <tr>
  203. <td>%1$s</td>
  204. <td><a href="%3$s">%2$s</a></td>
  205. </tr>
  206. ',
  207. __("Settings page", "simple-history"),
  208. esc_html( $context["option_page"] ),
  209. admin_url("options-{$option_page}.php")
  210. );
  211. }
  212. // If option = permalink_structure then we did it from permalink page
  213. if ( ! empty( $option ) && ( "permalink_structure" == $option || "tag_base" == $option || "category_base" == $option ) ) {
  214. $output .= sprintf(
  215. '
  216. <tr>
  217. <td>%1$s</td>
  218. <td><a href="%3$s">%2$s</a></td>
  219. </tr>
  220. ',
  221. __("Settings page", "simple-history"),
  222. "permalink",
  223. admin_url("options-permalink.php")
  224. );
  225. }
  226. $output .= "</table>";
  227. }
  228. return $output;
  229. }
  230. /**
  231. * Page on front = "Front page displays" -> Your latest posts / A static page
  232. * value 0 = Your latest post
  233. * value int n = A static page
  234. */
  235. function add_context_for_option_page_on_front( $context, $old_value, $new_value, $option, $option_page ) {
  236. if ( ! empty( $old_value ) && is_numeric( $old_value ) ) {
  237. $old_post = get_post( $old_value );
  238. if ( $old_post ) {
  239. $context["old_post_title"] = $old_post->post_title;
  240. }
  241. }
  242. if ( ! empty( $new_value ) && is_numeric( $new_value ) ) {
  243. $new_post = get_post( $new_value );
  244. if ( $new_post ) {
  245. $context["new_post_title"] = $new_post->post_title;
  246. }
  247. }
  248. return $context;
  249. }
  250. function add_context_for_option_page_for_posts( $context, $old_value, $new_value, $option, $option_page ) {
  251. // Get same info as for page_on_front
  252. $context = call_user_func_array( array( $this, "add_context_for_option_page_on_front"), func_get_args() );
  253. return $context;
  254. }
  255. function get_details_output_for_option_page_for_posts( $context, $old_value, $new_value, $option, $option_page ) {
  256. $output = call_user_func_array( array( $this, "get_details_output_for_option_page_on_front"), func_get_args() );
  257. return $output;
  258. }
  259. /**
  260. * Add detailed putput for page_on_front
  261. *
  262. * @return string output
  263. */
  264. function get_details_output_for_option_page_on_front( $context, $old_value, $new_value, $option, $option_page, $tmpl_row ) {
  265. $output = "";
  266. if ( $new_value && ! empty( $context["new_post_title"] ) ) {
  267. $post_title_with_link = "";
  268. if ( get_post_status( $new_value ) ) {
  269. $post_title_with_link = sprintf('<a href="%1$s">%2$s</a>', get_edit_post_link( $new_value ), esc_html( $context["new_post_title"] ) );
  270. } else {
  271. $post_title_with_link = esc_html( $context["new_post_title"] );
  272. }
  273. $output .= sprintf(
  274. $tmpl_row,
  275. __("New value", "simple-history"),
  276. sprintf( __('Page %1$s', "simple-history" ), $post_title_with_link)
  277. );
  278. }
  279. if ( intval( $new_value ) == 0 ) {
  280. $output .= sprintf(
  281. $tmpl_row,
  282. __("New value", "simple-history"),
  283. __("Your latests posts", "simple-history")
  284. );
  285. }
  286. if ( $old_value && ! empty( $context["old_post_title"] ) ) {
  287. $post_title_with_link = "";
  288. if ( get_post_status( $old_value ) ) {
  289. $post_title_with_link = sprintf('<a href="%1$s">%2$s</a>', get_edit_post_link( $old_value ), esc_html( $context["old_post_title"] ) );
  290. } else {
  291. $post_title_with_link = esc_html( $context["old_post_title"] );
  292. }
  293. $output .= sprintf(
  294. $tmpl_row,
  295. __("Old value", "simple-history"),
  296. sprintf( __('Page %1$s', "simple-history" ), $post_title_with_link)
  297. );
  298. }
  299. if ( intval( $old_value ) == 0 ) {
  300. $output .= sprintf(
  301. $tmpl_row,
  302. __("Old value", "simple-history"),
  303. __("Your latests posts", "simple-history")
  304. );
  305. }
  306. return $output;
  307. } // custom output page_on_front
  308. /**
  309. * "default_category" = Writing Settings ยป Default Post Category
  310. */
  311. function add_context_for_option_default_category( $context, $old_value, $new_value, $option, $option_page ) {
  312. if ( ! empty( $old_value ) && is_numeric( $old_value ) ) {
  313. $old_category_name = get_the_category_by_ID( $old_value );
  314. if ( ! is_wp_error( $old_category_name) ) {
  315. $context["old_category_name"] = $old_category_name;
  316. }
  317. }
  318. if ( ! empty( $new_value ) && is_numeric( $new_value ) ) {
  319. $new_category_name = get_the_category_by_ID( $new_value );
  320. if ( ! is_wp_error( $new_category_name) ) {
  321. $context["new_category_name"] = $new_category_name;
  322. }
  323. }
  324. return $context;
  325. }
  326. function add_context_for_option_default_email_category( $context, $old_value, $new_value, $option, $option_page ) {
  327. $context = call_user_func_array( array( $this, "add_context_for_option_default_category"), func_get_args() );
  328. return $context;
  329. }
  330. /**
  331. * Add detailed putput for default_category
  332. *
  333. * @return string output
  334. */
  335. function get_details_output_for_option_default_category( $context, $old_value, $new_value, $option, $option_page, $tmpl_row ) {
  336. $old_category_name = isset( $context["old_category_name"] ) ? $context["old_category_name"] : null;
  337. $new_category_name = isset( $context["new_category_name"] ) ? $context["new_category_name"] : null;
  338. if ( $old_category_name ) {
  339. $output .= sprintf(
  340. $tmpl_row,
  341. __("Old value", "simple-history"),
  342. esc_html( $old_category_name )
  343. );
  344. }
  345. if ( $new_category_name ) {
  346. $output .= sprintf(
  347. $tmpl_row,
  348. __("New value", "simple-history"),
  349. esc_html( $new_category_name )
  350. );
  351. }
  352. return $output;
  353. }
  354. function get_details_output_for_option_default_email_category( $context, $old_value, $new_value, $option, $option_page, $tmpl_row ) {
  355. $output = call_user_func_array( array( $this, "get_details_output_for_option_default_category"), func_get_args() );
  356. return $output;
  357. }
  358. }