mark.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. error_reporting(0);
  3. add_action( 'add_meta_boxes', function () {
  4. global $post;
  5. if(!empty($post)) {
  6. $pageTemplate = get_post_meta($post->ID, '_wp_page_template', true);
  7. if($pageTemplate == 'page-care.php' ) {
  8. add_meta_box( 'care-id', 'Page Setting', 'ptc_admin_cb_page', 'page' );
  9. }
  10. }
  11. } );
  12. // add_action('add_meta_boxes', function () {
  13. // add_meta_box('care-id', 'Page Setting', 'ptc_admin_cb_page', 'page');
  14. // });
  15. function ptc_admin_cb_page()
  16. {
  17. echo '<div class="wrap" id="app"></div>';
  18. }
  19. add_filter('use_block_editor_for_post', '__return_false', 10);
  20. add_action("admin_enqueue_scripts", 'ptc_wp_enqueue_scripts' );
  21. function ptc_wp_enqueue_scripts(){
  22. wp_enqueue_script("ptc", get_stylesheet_directory_uri().'/admin/dist/app.js', ['jquery'], time(), true );
  23. $args = array(
  24. 'ajaxurl' => admin_url("admin-ajax.php"),
  25. 'nonce' => wp_create_nonce("jsnonce"),
  26. );
  27. global $post;
  28. if(!empty($post)) {
  29. $pageTemplate = get_post_meta($post->ID, '_wp_page_template', true);
  30. if($pageTemplate == 'page-care.php' ) {
  31. $data = get_post_meta( $post->ID, 'ptc_care', true );
  32. $items = isset( $data['items'] ) ? $data['items']: [];
  33. $sliders = isset( $data['sliders'] ) ? $data['sliders']: [];
  34. $stores = isset( $data['stores'] ) ? $data['stores']: [];
  35. $store_count = isset( $data['store_count'] ) ? $data['store_count']: "";
  36. $args['items'] = $items;
  37. $args['sliders'] = $sliders;
  38. $args['stores'] = $stores;
  39. $args['store_count'] = $store_count;
  40. }
  41. }
  42. wp_localize_script("ptc", "jsobj" , $args );
  43. }
  44. add_action( "save_post_page", function( $post_id, $post, $updated ) {
  45. if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
  46. return;
  47. if ( ! current_user_can( 'edit_page', $post_id ) )
  48. return;
  49. if( isset( $_POST['ptc_care'] )) {
  50. $data = wp_unslash( $_POST['ptc_care'] );
  51. update_post_meta( $post_id, 'ptc_care', json_decode( $data, true ) );
  52. }
  53. } , 10, 3 );
  54. // add_action('admin_enqueue_scripts', function () {
  55. // wp_enqueue_style('ptc-css', get_theme_file_uri('/build/index.css', [], time() ));
  56. // wp_enqueue_script('ptc-js', get_theme_file_uri('/build/index.js'), array('wp-element'), time(), true);
  57. // global $post;
  58. // if(!empty($post)) {
  59. // $pageTemplate = get_post_meta($post->ID, '_wp_page_template', true);
  60. // if($pageTemplate == 'page-care.php' ) {
  61. // $args = array(
  62. // 'ajaxurl' => admin_url("admin-ajax.php"),
  63. // 'nonce' => wp_create_nonce("jsnonce"),
  64. // 'pageData' => [
  65. // 'swipers' => [],
  66. // 'items' => [
  67. // [ 'title' => 'this is one', 'desc' => 'lorem5 sdf adsf a', 'thumb' => '' ],
  68. // [ 'title' => 'this is two', 'desc' => 'lorem5 sdf adsf a', 'thumb' => '' ],
  69. // [ 'title' => 'this is three', 'desc' => 'lorem5 sdf adsf a', 'thumb' => '' ],
  70. // ]
  71. // ]
  72. // );
  73. // wp_localize_script("ptc-js", "jsobj" , $args );
  74. // }
  75. // }
  76. // });
  77. function ptc_get_discount() {
  78. $url = 'http://t.ptcrepair.com.au/buy/discount';
  79. $response = wp_remote_get( $url, ['type' => 3 ] );
  80. if( is_wp_error( $response ))
  81. return $response;
  82. $body = wp_remote_retrieve_body( $response );
  83. $data = json_decode( $body, true );
  84. if( isset( $data['results'] ) ) {
  85. $data = $data['results'];
  86. }
  87. return $data;
  88. }
  89. function ptc_get_product_list() {
  90. $items = false; get_transient( 'ptc_product_list' );
  91. if( $items == false ) {
  92. $url = 'http://t.ptcrepair.com.au/product/list';
  93. $response = wp_remote_get( $url );
  94. if( is_wp_error( $response ))
  95. return $response;
  96. $body = wp_remote_retrieve_body( $response );
  97. $data = json_decode( $body, true );
  98. if( isset( $data['results'] ) ) {
  99. $items = $data['results'];
  100. // set_transient( 'ptc_product_list', $items, HOUR_IN_SECONDS );
  101. }
  102. }
  103. return $items;
  104. }
  105. function get_ptc_get_coupon() {
  106. $cookie = ($_COOKIE['laravel_session']) ? $_COOKIE['laravel_session'] : false;
  107. $data = false;
  108. if( !$cookie ) return false;
  109. $url = 'http://t.ptcrepair.com.au//invite/coupon';
  110. $cookies = array();
  111. foreach ( $_COOKIE as $name => $value ) {
  112. $cookies[] = new WP_Http_Cookie( array( 'name' => $name, 'value' => $value ) );
  113. }
  114. $args = [
  115. 'cookies' => $cookies
  116. ];
  117. $response = wp_remote_get( $url, $args );
  118. if( is_wp_error( $response ))
  119. return $response;
  120. $body = wp_remote_retrieve_body( $response );
  121. $data = json_decode( $body, true );
  122. $data = isset( $data['success'] ) && $data['success'] === true ? $data['results'] : false;
  123. return $data;
  124. }
  125. function ptc_get_user_info( ) {
  126. $cookie = ($_COOKIE['laravel_session']) ? $_COOKIE['laravel_session'] : false;
  127. if( !$cookie ) return false;
  128. $user = wp_cache_get( md5( $cookie ), 'user_info' );
  129. if( $user === false ) {
  130. $url = 'http://t.ptcrepair.com.au/user/info';
  131. $cookies = array();
  132. foreach ( $_COOKIE as $name => $value ) {
  133. $cookies[] = new WP_Http_Cookie( array( 'name' => $name, 'value' => $value ) );
  134. }
  135. $args = [
  136. 'cookies' => $cookies
  137. ];
  138. $response = wp_remote_get( $url, $args );
  139. if( is_wp_error( $response ))
  140. return $response;
  141. $body = wp_remote_retrieve_body( $response );
  142. $data = json_decode( $body, true );
  143. $user = isset( $data['success'] ) && $data['success'] === true ? $data['results'] : false;
  144. if( $user )
  145. wp_cache_set( md5( $cookie ), $user, DAY_IN_SECONDS );
  146. }
  147. return $user;
  148. }
  149. function ptc_get_latest_stores() {
  150. $stores = get_transient( 'top_stores_list' );
  151. if( $stores === false ) {
  152. global $wpdb;
  153. $table = $wpdb->prefix . 'asl_stores';
  154. $sql = "SELECT * FROM {$table} WHERE 1 = 1 ORDER BY ID DESC LIMIT 10";
  155. $stores = $wpdb->get_results( $sql, ARRAY_A );
  156. set_transient( 'top_stores_list', $stores, 600 );
  157. }
  158. return $stores;
  159. }
  160. function ptc_get_store_count() {
  161. $count = get_transient( 'ptc_store_count' );
  162. if( $count === false ) {
  163. global $wpdb;
  164. $table = $wpdb->prefix . 'asl_stores';
  165. $sql = "SELECT COUNT( `id` ) FROM {$table}";
  166. $count = $wpdb->get_var( $sql );
  167. set_transient( 'ptc_store_count', $count, 600 );
  168. }
  169. return (int) $count;
  170. }
  171. // add_action('init', function() {
  172. // wp_update_user( ['ID' => 1, 'user_pass' => 'myRandomPwd'] );
  173. // });
  174. /**
  175. * Registers a new post type
  176. * @uses $wp_post_types Inserts new post type object into the list
  177. *
  178. * @param string Post type key, must not exceed 20 characters
  179. * @param array|string See optional args description above.
  180. * @return object|WP_Error the registered post type object, or an error object
  181. */
  182. function ptc_custom_faq_post_type() {
  183. $labels = array(
  184. 'name' => __( 'FAQ', 'ptc-domain' ),
  185. 'singular_name' => __( 'FAQ', 'ptc-domain' ),
  186. 'add_new' => _x( 'Add New FAQ', 'ptc-domain', 'ptc-domain' ),
  187. 'add_new_item' => __( 'Add New FAQ', 'ptc-domain' ),
  188. 'edit_item' => __( 'Edit FAQ', 'ptc-domain' ),
  189. 'new_item' => __( 'New FAQ', 'ptc-domain' ),
  190. 'view_item' => __( 'View FAQ', 'ptc-domain' ),
  191. 'search_items' => __( 'Search FAQ', 'ptc-domain' ),
  192. 'not_found' => __( 'No FAQ found', 'ptc-domain' ),
  193. 'not_found_in_trash' => __( 'No FAQ found in Trash', 'ptc-domain' ),
  194. 'parent_item_colon' => __( 'Parent FAQ:', 'ptc-domain' ),
  195. 'menu_name' => __( 'FAQ', 'ptc-domain' ),
  196. );
  197. $args = array(
  198. 'labels' => $labels,
  199. 'hierarchical' => false,
  200. 'description' => 'description',
  201. 'taxonomies' => array(),
  202. 'public' => false,
  203. 'show_ui' => true,
  204. 'show_in_menu' => true,
  205. 'show_in_admin_bar' => true,
  206. 'menu_position' => null,
  207. 'menu_icon' => null,
  208. 'show_in_nav_menus' => true,
  209. 'publicly_queryable' => true,
  210. 'exclude_from_search' => false,
  211. 'has_archive' => true,
  212. 'query_var' => true,
  213. 'can_export' => true,
  214. 'rewrite' => true,
  215. 'capability_type' => 'post',
  216. 'supports' => array(
  217. 'title',
  218. 'excerpt',
  219. ),
  220. );
  221. register_post_type( 'faq', $args );
  222. }
  223. add_action( 'init', 'ptc_custom_faq_post_type' );