PageRenderTime 51ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/types/library/toolset/types/includes/custom-types-form.php

https://gitlab.com/Fraternal-Group/fraternal
PHP | 287 lines | 195 code | 33 blank | 59 comment | 36 complexity | cc2a7bcfe45861d9ee3a517817419f08 MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. * Custom types form
  5. *
  6. *
  7. */
  8. /**
  9. * Adds JS validation script.
  10. */
  11. function wpcf_admin_types_form_js_validation()
  12. {
  13. wpcf_form_render_js_validation();
  14. }
  15. /**
  16. * Submit function
  17. *
  18. * @global object $wpdb
  19. *
  20. */
  21. function wpcf_admin_custom_types_form_submit($form)
  22. {
  23. global $wpcf;
  24. if ( !isset( $_POST['ct'] ) ) {
  25. return false;
  26. }
  27. $data = $_POST['ct'];
  28. $update = false;
  29. // Sanitize data
  30. if ( isset( $data['wpcf-post-type'] ) ) {
  31. $update = true;
  32. $data['wpcf-post-type'] = sanitize_title( $data['wpcf-post-type'] );
  33. } else {
  34. $data['wpcf-post-type'] = null;
  35. }
  36. if ( isset( $data['slug'] ) ) {
  37. $data['slug'] = sanitize_title( $data['slug'] );
  38. } else {
  39. $data['slug'] = null;
  40. }
  41. if ( isset( $data['rewrite']['slug'] ) ) {
  42. $data['rewrite']['slug'] = remove_accents( $data['rewrite']['slug'] );
  43. $data['rewrite']['slug'] = strtolower( $data['rewrite']['slug'] );
  44. $data['rewrite']['slug'] = trim( $data['rewrite']['slug'] );
  45. }
  46. $data['_builtin'] = false;
  47. // Set post type name
  48. $post_type = null;
  49. if ( !empty( $data['slug'] ) ) {
  50. $post_type = $data['slug'];
  51. } elseif ( !empty( $data['wpcf-post-type'] ) ) {
  52. $post_type = $data['wpcf-post-type'];
  53. } elseif ( !empty( $data['labels']['singular_name'] ) ) {
  54. $post_type = sanitize_title( $data['labels']['singular_name'] );
  55. }
  56. if ( empty( $post_type ) ) {
  57. wpcf_admin_message( __( 'Please set post type name', 'wpcf' ), 'error' );
  58. return false;
  59. }
  60. $data['slug'] = $post_type;
  61. $custom_types = get_option( WPCF_OPTION_NAME_CUSTOM_TYPES, array() );
  62. $protected_data_check = array();
  63. if ( wpcf_is_builtin_post_types($data['slug']) ) {
  64. $data['_builtin'] = true;
  65. } else {
  66. // Check reserved name
  67. $reserved = wpcf_is_reserved_name( $post_type, 'post_type' );
  68. if ( is_wp_error( $reserved ) ) {
  69. wpcf_admin_message( $reserved->get_error_message(), 'error' );
  70. return false;
  71. }
  72. // Check overwriting
  73. if ( ( !array_key_exists( 'wpcf-post-type', $data ) || $data['wpcf-post-type'] != $post_type ) && array_key_exists( $post_type, $custom_types ) ) {
  74. wpcf_admin_message( __( 'Post Type already exists', 'wpcf' ), 'error' );
  75. return false;
  76. }
  77. /*
  78. * Since Types 1.2
  79. * We do not allow plural and singular names to be same.
  80. */
  81. if ( $wpcf->post_types->check_singular_plural_match( $data ) ) {
  82. wpcf_admin_message( $wpcf->post_types->message( 'warning_singular_plural_match' ), 'error' );
  83. return false;
  84. }
  85. // Check if renaming then rename all post entries and delete old type
  86. if ( !empty( $data['wpcf-post-type'] )
  87. && $data['wpcf-post-type'] != $post_type ) {
  88. global $wpdb;
  89. $wpdb->update( $wpdb->posts, array('post_type' => $post_type),
  90. array('post_type' => $data['wpcf-post-type']), array('%s'),
  91. array('%s')
  92. );
  93. /**
  94. * update post meta "_wp_types_group_post_types"
  95. */
  96. $sql = $wpdb->prepare(
  97. sprintf(
  98. 'select meta_id, meta_value from %s where meta_key = %%s',
  99. $wpdb->postmeta
  100. ),
  101. '_wp_types_group_post_types'
  102. );
  103. $all_meta = $wpdb->get_results($sql, OBJECT_K);
  104. $re = sprintf( '/,%s,/', $data['wpcf-post-type'] );
  105. foreach( $all_meta as $meta ) {
  106. if ( !preg_match( $re, $meta->meta_value ) ) {
  107. continue;
  108. }
  109. $wpdb->update(
  110. $wpdb->postmeta,
  111. array(
  112. 'meta_value' => preg_replace( $re, ','.$post_type.',', $meta->meta_value ),
  113. ),
  114. array(
  115. 'meta_id' => $meta->meta_id,
  116. ),
  117. array( '%s' ),
  118. array( '%d' )
  119. );
  120. }
  121. /**
  122. * update _wpcf_belongs_{$data['wpcf-post-type']}_id
  123. */
  124. $wpdb->update(
  125. $wpdb->postmeta,
  126. array(
  127. 'meta_key' => sprintf( '_wpcf_belongs_%s_id', $post_type ),
  128. ),
  129. array(
  130. 'meta_key' => sprintf( '_wpcf_belongs_%s_id', $data['wpcf-post-type'] ),
  131. ),
  132. array( '%s' ),
  133. array( '%s' )
  134. );
  135. /**
  136. * update options "wpv_options"
  137. */
  138. $wpv_options = get_option( 'wpv_options', true );
  139. if ( is_array( $wpv_options ) ) {
  140. $re = sprintf( '/(views_template_(archive_)?for_)%s/', $data['wpcf-post-type'] );
  141. foreach( $wpv_options as $key => $value ) {
  142. if ( !preg_match( $re, $key ) ) {
  143. continue;
  144. }
  145. unset($wpv_options[$key]);
  146. $key = preg_replace( $re, "$1".$post_type, $key );
  147. $wpv_options[$key] = $value;
  148. }
  149. update_option( 'wpv_options', $wpv_options );
  150. }
  151. /**
  152. * update option "wpcf-custom-taxonomies"
  153. */
  154. $wpcf_custom_taxonomies = get_option( WPCF_OPTION_NAME_CUSTOM_TAXONOMIES, true );
  155. if ( is_array( $wpcf_custom_taxonomies ) ) {
  156. $update_wpcf_custom_taxonomies = false;
  157. foreach( $wpcf_custom_taxonomies as $key => $value ) {
  158. if ( array_key_exists( 'supports', $value ) && array_key_exists( $data['wpcf-post-type'], $value['supports'] ) ) {
  159. unset( $wpcf_custom_taxonomies[$key]['supports'][$data['wpcf-post-type']] );
  160. $update_wpcf_custom_taxonomies = true;
  161. }
  162. }
  163. if ( $update_wpcf_custom_taxonomies ) {
  164. update_option( WPCF_OPTION_NAME_CUSTOM_TAXONOMIES, $wpcf_custom_taxonomies );
  165. }
  166. }
  167. // Sync action
  168. do_action( 'wpcf_post_type_renamed', $post_type, $data['wpcf-post-type'] );
  169. // Set protected data
  170. $protected_data_check = $custom_types[$data['wpcf-post-type']];
  171. // Delete old type
  172. unset( $custom_types[$data['wpcf-post-type']] );
  173. $data['wpcf-post-type'] = $post_type;
  174. } else {
  175. // Set protected data
  176. $protected_data_check = !empty( $custom_types[$post_type] ) ? $custom_types[$post_type] : array();
  177. }
  178. // Check if active
  179. if ( isset( $custom_types[$post_type]['disabled'] ) ) {
  180. $data['disabled'] = $custom_types[$post_type]['disabled'];
  181. }
  182. }
  183. // Sync taxes with custom taxes
  184. if ( !empty( $data['taxonomies'] ) ) {
  185. $taxes = get_option( WPCF_OPTION_NAME_CUSTOM_TAXONOMIES, array() );
  186. foreach ( $taxes as $id => $tax ) {
  187. if ( array_key_exists( $id, $data['taxonomies'] ) ) {
  188. $taxes[$id]['supports'][$data['slug']] = 1;
  189. } else {
  190. unset( $taxes[$id]['supports'][$data['slug']] );
  191. }
  192. }
  193. update_option( WPCF_OPTION_NAME_CUSTOM_TAXONOMIES, $taxes );
  194. }
  195. // Preserve protected data
  196. foreach ( $protected_data_check as $key => $value ) {
  197. if ( strpos( $key, '_' ) !== 0 ) {
  198. unset( $protected_data_check[$key] );
  199. }
  200. }
  201. /**
  202. * set last edit time
  203. */
  204. $data[TOOLSET_EDIT_LAST] = time();
  205. /**
  206. * set last edit author
  207. */
  208. $data[WPCF_AUTHOR] = get_current_user_id();
  209. /**
  210. * add builid in
  211. */
  212. if ( $data['_builtin'] && !isset( $protected_data_check[$data['slug']])) {
  213. $protected_data_check[$data['slug']] = array();
  214. }
  215. // Merging protected data
  216. $custom_types[$post_type] = array_merge( $protected_data_check, $data );
  217. update_option( WPCF_OPTION_NAME_CUSTOM_TYPES, $custom_types );
  218. // WPML register strings
  219. if ( !$data['_builtin'] ) {
  220. wpcf_custom_types_register_translation( $post_type, $data );
  221. }
  222. /**
  223. * success message
  224. */
  225. wpcf_admin_message_store(
  226. apply_filters(
  227. 'types_message_custom_post_type_saved',
  228. __( 'Post Type saved', 'wpcf' ),
  229. $data,
  230. $update
  231. ),
  232. 'custom'
  233. );
  234. if ( !$data['_builtin'] ) {
  235. // Flush rewrite rules
  236. flush_rewrite_rules();
  237. do_action( 'wpcf_custom_types_save', $data );
  238. }
  239. // Redirect
  240. wp_safe_redirect(
  241. esc_url_raw(
  242. add_query_arg(
  243. array(
  244. 'page' => 'wpcf-edit-type',
  245. 'wpcf-post-type' => $post_type,
  246. 'wpcf-rewrite' => 1,
  247. 'wpcf-message' => 'view',
  248. ),
  249. admin_url( 'admin.php' )
  250. )
  251. )
  252. );
  253. die();
  254. }