/src/lib/transcribe/settings.php

https://github.com/mgsisk/webcomic · PHP · 324 lines · 212 code · 41 blank · 71 comment · 8 complexity · 9908ba03841b1df649ce6965837bcefe MD5 · raw file

  1. <?php
  2. /**
  3. * Transcribe settings functionality
  4. *
  5. * @package Webcomic
  6. */
  7. namespace Mgsisk\Webcomic\Transcribe;
  8. /**
  9. * Add settings hooks.
  10. *
  11. * @return void
  12. */
  13. function settings() {
  14. add_filter( 'webcomic_activate_transcribe', __NAMESPACE__ . '\hook_activate' );
  15. add_filter( 'webcomic_deactivate_transcribe', __NAMESPACE__ . '\hook_deactivate' );
  16. add_filter( 'webcomic_collection_allowed_options', __NAMESPACE__ . '\hook_add_allowed_options' );
  17. add_filter( 'admin_init', __NAMESPACE__ . '\hook_add_settings_section' );
  18. add_filter( 'admin_init', __NAMESPACE__ . '\hook_add_field_comics' );
  19. add_filter( 'admin_init', __NAMESPACE__ . '\hook_add_field_permissions' );
  20. add_filter( 'admin_init', __NAMESPACE__ . '\hook_add_field_notifications' );
  21. add_filter( 'admin_head', __NAMESPACE__ . '\hook_add_help_tab' );
  22. foreach ( webcomic( 'option.collections' ) as $collection ) {
  23. add_filter( "sanitize_option_{$collection}", __NAMESPACE__ . '\hook_sanitize_field_comics' );
  24. add_filter( "sanitize_option_{$collection}", __NAMESPACE__ . '\hook_sanitize_field_permissions' );
  25. add_filter( "sanitize_option_{$collection}", __NAMESPACE__ . '\hook_sanitize_field_notifications' );
  26. }
  27. }
  28. /**
  29. * Activate the transcribe component.
  30. *
  31. * @return void
  32. */
  33. function hook_activate() {
  34. set_transient( 'webcomic_flush_rewrite_rules', true, 1 );
  35. foreach ( webcomic( 'option.collections' ) as $collection ) {
  36. if ( null !== webcomic( "option.{$collection}.transcribe_comic" ) ) {
  37. continue;
  38. }
  39. update_option(
  40. $collection, [
  41. 'transcribe_comic' => false,
  42. 'transcribe_close' => 0,
  43. 'transcribe_require' => 'loggedin',
  44. 'transcribe_publish' => '',
  45. 'transcribe_alert_pub' => false,
  46. 'transcribe_alert_mod' => false,
  47. ]
  48. );
  49. }
  50. }
  51. /**
  52. * Deactivate the transcribe component.
  53. *
  54. * @return void
  55. * @suppress PhanAccessMethodInternal - get_terms() incorrectly triggers this.
  56. */
  57. function hook_deactivate() {
  58. set_transient( 'webcomic_flush_rewrite_rules', true, 1 );
  59. if ( ! webcomic( 'option.uninstall' ) ) {
  60. return;
  61. }
  62. foreach ( webcomic( 'option.collections' ) as $collection ) {
  63. add_filter(
  64. "sanitize_option_{$collection}", function( array $options ) use ( $collection ) {
  65. remove_filter( "sanitize_option_{$collection}", __NAMESPACE__ . '\hook_sanitize_field_comics' );
  66. remove_filter( "sanitize_option_{$collection}", __NAMESPACE__ . '\hook_sanitize_field_permissions' );
  67. remove_filter( "sanitize_option_{$collection}", __NAMESPACE__ . '\hook_sanitize_field_notifications' );
  68. unset(
  69. $options['transcribe_comic'],
  70. $options['transcribe_close'],
  71. $options['transcribe_require'],
  72. $options['transcribe_publish'],
  73. $options['transcribe_alert_pub'],
  74. $options['transcribe_alert_mod']
  75. );
  76. return $options;
  77. }, 99
  78. );
  79. update_option(
  80. $collection, [
  81. 'transcribe_comic' => false,
  82. 'transcribe_close' => 0,
  83. 'transcribe_require' => 'loggedin',
  84. 'transcribe_publish' => '',
  85. 'transcribe_alert_pub' => false,
  86. 'transcribe_alert_mod' => false,
  87. ]
  88. );
  89. }
  90. $table = webcomic( 'GLOBALS.wpdb' )->options;
  91. webcomic( 'GLOBALS.wpdb' )->query( "DELETE from {$table} where option_name like 'widget_mgsisk_webcomic_transcribe_%'" );
  92. $transcripts = get_posts(
  93. [
  94. 'fields' => 'ids',
  95. 'post_type' => 'webcomic_transcript',
  96. 'post_status' => 'any',
  97. 'posts_per_page' => -1,
  98. ]
  99. );
  100. foreach ( $transcripts as $transcript ) {
  101. wp_delete_post( $transcript, true );
  102. }
  103. $languages = get_terms(
  104. [
  105. 'fields' => 'ids',
  106. 'taxonomy' => 'webcomic_transcript_language',
  107. 'hide_empty' => false,
  108. ]
  109. );
  110. foreach ( $languages as $language ) {
  111. wp_delete_term( $language, 'webcomic_transcript_language' );
  112. }
  113. delete_metadata( 'post', 0, 'webcomic_transcribe', null, true );
  114. }
  115. /**
  116. * Add the transcribe allowed collection options.
  117. *
  118. * @param array $allowed The allowed options.
  119. * @return array
  120. */
  121. function hook_add_allowed_options( array $allowed ) : array {
  122. return array_merge( $allowed, [ 'transcribe_comic', 'transcribe_close', 'transcribe_close_check', 'transcribe_require', 'transcribe_require_check', 'transcribe_publish', 'transcribe_publish_check', 'transcribe_alert_pub', 'transcribe_alert_mod' ] );
  123. }
  124. /**
  125. * Add the settings section.
  126. *
  127. * @return void
  128. */
  129. function hook_add_settings_section() {
  130. foreach ( webcomic( 'option.collections' ) as $collection ) {
  131. add_settings_section(
  132. "{$collection}_transcribe",
  133. '<span class="dashicons dashicons-testimonial"></span> ' . esc_html__( 'Transcripts', 'webcomic' ),
  134. function() {
  135. echo '<div></div>';
  136. },
  137. "{$collection}_settings"
  138. );
  139. }
  140. }
  141. /**
  142. * Add the comics setting field.
  143. *
  144. * @return void
  145. */
  146. function hook_add_field_comics() {
  147. foreach ( webcomic( 'option.collections' ) as $collection ) {
  148. add_settings_field(
  149. "{$collection}_comics",
  150. __( 'Comics', 'webcomic' ),
  151. function( $args ) {
  152. require $args['file'];
  153. },
  154. "{$collection}_settings",
  155. "{$collection}_transcribe", [
  156. 'file' => __DIR__ . '/settings-inc-field-comics.php',
  157. 'option' => webcomic( "option.{$collection}.transcribe_comic" ),
  158. 'option_close' => webcomic( "option.{$collection}.transcribe_close" ),
  159. 'label_for' => "{$collection}[transcribe_comic]",
  160. 'label_close' => "{$collection}[transcribe_close]",
  161. 'label_close_check' => "{$collection}[transcribe_close_check]",
  162. ]
  163. );
  164. }
  165. }
  166. /**
  167. * Add the permissions setting field.
  168. *
  169. * @return void
  170. */
  171. function hook_add_field_permissions() {
  172. foreach ( webcomic( 'option.collections' ) as $collection ) {
  173. add_settings_field(
  174. "{$collection}_permissions",
  175. __( 'Permissions', 'webcomic' ),
  176. function( $args ) {
  177. require $args['file'];
  178. },
  179. "{$collection}_settings",
  180. "{$collection}_transcribe", [
  181. 'file' => __DIR__ . '/settings-inc-field-permissions.php',
  182. 'option' => webcomic( "option.{$collection}.transcribe_require" ),
  183. 'option_publish' => webcomic( "option.{$collection}.transcribe_publish" ),
  184. 'label_for' => "{$collection}[transcribe_require_check]",
  185. 'label_option' => "{$collection}[transcribe_require]",
  186. 'label_publish' => "{$collection}[transcribe_publish]",
  187. 'label_publish_check' => "{$collection}[transcribe_publish_check]",
  188. ]
  189. );
  190. }
  191. }
  192. /**
  193. * Add the notifications setting field.
  194. *
  195. * @return void
  196. */
  197. function hook_add_field_notifications() {
  198. foreach ( webcomic( 'option.collections' ) as $collection ) {
  199. add_settings_field(
  200. "{$collection}_donation",
  201. __( 'Notifications', 'webcomic' ),
  202. function( $args ) {
  203. require $args['file'];
  204. },
  205. "{$collection}_settings",
  206. "{$collection}_transcribe", [
  207. 'file' => __DIR__ . '/settings-inc-field-notifications.php',
  208. 'option' => webcomic( "option.{$collection}.transcribe_alert_pub" ),
  209. 'option_mod' => webcomic( "option.{$collection}.transcribe_alert_mod" ),
  210. 'label_for' => "{$collection}[transcribe_alert_pub]",
  211. 'label_mod' => "{$collection}[transcribe_alert_mod]",
  212. ]
  213. );
  214. }
  215. }
  216. /**
  217. * Add the help tab.
  218. *
  219. * @return void
  220. */
  221. function hook_add_help_tab() {
  222. $screen = get_current_screen();
  223. if ( ! preg_match( '/^webcomic\d+_page_webcomic\d+_options$/', $screen->id ) ) {
  224. return;
  225. }
  226. $screen->add_help_tab(
  227. [
  228. 'id' => 'transcribe',
  229. 'title' => __( 'Transcripts', 'webcomic' ),
  230. 'callback' => function() {
  231. require __DIR__ . '/settings-inc-help.php';
  232. },
  233. ]
  234. );
  235. }
  236. /* ===== Collection Hooks =================================================== */
  237. /**
  238. * Sanitize the comics field.
  239. *
  240. * @param array $options The submitted options.
  241. * @return array
  242. */
  243. function hook_sanitize_field_comics( array $options ) : array {
  244. $options['transcribe_comic'] = (bool) $options['transcribe_comic'];
  245. $close = 0;
  246. if ( isset( $options['transcribe_close_check'] ) ) {
  247. $close = abs( (int) $options['transcribe_close'] );
  248. }
  249. $options['transcribe_close'] = $close;
  250. unset( $options['transcribe_close_check'] );
  251. return $options;
  252. }
  253. /**
  254. * Sanitize the permissions field.
  255. *
  256. * @param array $options The submitted options.
  257. * @return array
  258. */
  259. function hook_sanitize_field_permissions( array $options ) : array {
  260. $require = '';
  261. $publish = '';
  262. if ( isset( $options['transcribe_require_check'] ) && in_array( $options['transcribe_require'], [ '', 'name_email', 'loggedin' ], true ) ) {
  263. $require = $options['transcribe_require'];
  264. }
  265. if ( isset( $options['transcribe_publish_check'] ) && in_array( $options['transcribe_publish'], [ '', 'name_email', 'loggedin' ], true ) ) {
  266. $publish = $options['transcribe_publish'];
  267. }
  268. unset( $options['transcribe_require_check'], $options['transcribe_publish_check'] );
  269. $options['transcribe_require'] = $require;
  270. $options['transcribe_publish'] = $publish;
  271. return $options;
  272. }
  273. /**
  274. * Sanitize the notifications field.
  275. *
  276. * @param array $options The submitted options.
  277. * @return array
  278. */
  279. function hook_sanitize_field_notifications( array $options ) : array {
  280. $options['transcribe_alert_pub'] = (bool) $options['transcribe_alert_pub'];
  281. $options['transcribe_alert_mod'] = (bool) $options['transcribe_alert_mod'];
  282. return $options;
  283. }