calderawpmetaplateadminsettings

Class settings.

Defined (1)

The class is defined in the following location(s).

/vendor/calderawp/metaplate-admin/src/settings.php  
  1. class settings extends init { 
  2.  
  3.  
  4. /** 
  5. * Start up 
  6. */ 
  7. public function __construct() { 
  8.  
  9. // save config 
  10. add_action( 'wp_ajax_mtpt_save_config', array( $this, 'save_config') ); 
  11. // creat new 
  12. add_action( 'wp_ajax_mtpt_create_metaplate', array( $this, 'create_new_metaplate') ); 
  13. // delete 
  14. add_action( 'wp_ajax_mtpt_delete_metaplate', array( $this, 'delete_metaplate') ); 
  15.  
  16.  
  17.  
  18. /** 
  19. * saves a config 
  20. */ 
  21. private function update_settings($config) { 
  22.  
  23. if( isset( $config['metaplate-setup'] ) && !wp_verify_nonce( $config['metaplate-setup'], 'metaplate' ) ) { 
  24. wp_send_json_error( $config ); 
  25.  
  26. $metaplates = $this->get_registry(); 
  27. if( isset( $config['id'] ) && !empty( $metaplates[ $config['id'] ] ) ) { 
  28. $new_value = array(); 
  29. $old_value = data::get_metaplate( $config[ 'id' ] ); 
  30. if ( is_string( $old_value ) ) { 
  31. $old_value = (array) json_decode( $old_value ); 
  32. $fields = array( 'id', 'name', 'slug', 'content_type_specific', '_current_tab' ); 
  33. foreach( $fields as $field ) { 
  34. if ( isset( $config[ $field ] ) ) { 
  35. if ( 'content_type_specific' !== $field ) { 
  36. $new_value[ $field ] = strip_tags( $config[ $field ] ); 
  37. if ( isset( $old_value[ $field ] ) ) { 
  38. $new_value[ $field ] = $old_value[ $field ]; 
  39. else { 
  40. wp_send_json_error( $config ); 
  41. die(); 
  42. } else { 
  43. $new_value[ $field ] = (bool) $config[ $field ]; 
  44.  
  45. else { 
  46. if ( isset( $old_value[ $field ] ) ) { 
  47. $new_value[ $field ] = $old_value[ $field ]; 
  48.  
  49.  
  50.  
  51. // add search form to registery 
  52. if( !empty( $config['search_form'] ) ) { 
  53. $new_value['search_form'] = $config['search_form']; 
  54. $this->update_registry( $new_value, $config['id'] ); 
  55.  
  56.  
  57. if ( ! $config[ 'id'] && isset( $config[ 'slug'] ) ) { 
  58. $config[ 'id' ] = data::get_metaplate_id_by_slug( $config[ 'slug' ] ); 
  59. $update = update_option( $config['id'], $config ); 
  60. if ( $update ) { 
  61. wp_send_json_success( $config ); 
  62.  
  63. wp_send_json_error( $config ); 
  64. die(); 
  65.  
  66.  
  67.  
  68. /** 
  69. * saves a config 
  70. */ 
  71. public function save_config() { 
  72.  
  73. if( empty( $_POST['metaplate-setup'] ) || !wp_verify_nonce( $_POST['metaplate-setup'], 'metaplate' ) ) { 
  74. if( empty( $_POST['config'] ) ) { 
  75. return; 
  76. // define default 
  77. $config = array(); 
  78. if( !empty( $_POST['metaplate-setup'] ) && empty( $_POST['config'] ) ) { 
  79. $config = stripslashes_deep( $_POST ); 
  80.  
  81. $this->update_settings( $config ); 
  82.  
  83. wp_redirect( '?page=metaplate&updated=true' ); 
  84. exit; 
  85.  
  86. if( !empty( $_POST['config'] ) ) { 
  87. $config = json_decode( stripslashes_deep( $_POST['config'] ), true ); 
  88. $this->update_settings( $config ); 
  89. wp_send_json_success( $config ); 
  90.  
  91.  
  92. // nope 
  93. wp_send_json_error( $config ); 
  94.  
  95.  
  96. /** 
  97. * Deletes a block 
  98. */ 
  99. public function delete_metaplate() { 
  100.  
  101. $search_blocks = $this->get_registry(); 
  102. if( isset( $search_blocks[ $_POST['block'] ] ) ) { 
  103. delete_option( $search_blocks[$_POST['block']]['id'] ); 
  104.  
  105. unset( $search_blocks[ $_POST['block'] ] ); 
  106. update_option( '_metaplates_registry', $search_blocks ); 
  107.  
  108. wp_send_json_success( $_POST ); 
  109.  
  110. wp_send_json_error( $_POST ); 
  111.  
  112. /** 
  113. * create new metaplate 
  114. */ 
  115. public function create_new_metaplate() { 
  116.  
  117. $metaplates = $this->get_registry(); 
  118. if( empty( $metaplates ) ) { 
  119. $metaplates = array(); 
  120.  
  121. $metaplate_id = uniqid('MTPT').rand(100, 999); 
  122. if( !isset( $metaplates[ $metaplate_id ] ) ) { 
  123. $data = $this->validate_sanitize_new(); 
  124. if ( is_array( $data ) ) { 
  125. $new_metaplate = array( 
  126. 'id' => $metaplate_id,  
  127. '_current_tab' => '#metaplate-panel-general' 
  128. ); 
  129. $new_metaplate = array_merge( $new_metaplate, $data ); 
  130.  
  131. update_option( $metaplate_id, $new_metaplate ); 
  132. $this->update_registry( $new_metaplate, $metaplate_id ); 
  133.  
  134. // end 
  135. wp_send_json_success( $new_metaplate ); 
  136. die(); 
  137.  
  138.  
  139. wp_send_json_error(); 
  140.  
  141.  
  142. /** 
  143. * Validate and sanitize metaplate options coming from POST data. 
  144. * @return array|bool 
  145. */ 
  146. private function validate_sanitize_new() { 
  147. $fields = array( 'name', 'slug', 'content_type_specific' ); 
  148. foreach( $fields as $field ) { 
  149. if ( ! isset( $_POST[ $field ] ) ) { 
  150. return false; 
  151.  
  152.  
  153. if ( 'content_type_specific' !== $field ) { 
  154. $save[ $field ] = strip_tags( $_POST[ $field ] ); 
  155.  
  156.  
  157.  
  158. $content_type_specific = $_POST[ 'content_type_specific']; 
  159. if ( ! in_array( $content_type_specific, array( 'true', 'false' ) ) ) { 
  160. return false; 
  161.  
  162. if ( 'false' === $_POST[ 'content_type_specific'] ) { 
  163. $save[ 'content_type_specific' ] = false; 
  164. } else { 
  165. $save[ 'content_type_specific' ] = true; 
  166.  
  167. return $save; 
  168.  
  169.  
  170.  
  171. /** 
  172. * Get current metaplates 
  173. * @return array|bool 
  174. */ 
  175. private function get_registry() { 
  176. return data::get_registry(); 
  177.  
  178.  
  179. /** 
  180. * Update registry of metaplates 
  181. * Note: Does not save the metaplate itself. 
  182. * @param array $new_value The new item to add. 
  183. * @param string $id Id of new item to add. 
  184. * @return bool 
  185. */ 
  186. private function update_registry( $new_value, $id ) { 
  187. return data::update_registry( $new_value, $id ); 
  188.  
  189.  
  190.