· 5 years ago · Dec 05, 2020, 04:16 PM
1<?php
2/**
3 * Plugin Name: Schema Integrator
4 * Plugin URI: https://my.gmbgeeks.com
5 * Description: Integrates and shows GMB schema from gmbgeeks members area.
6 * Author: GMBGeeks Team
7 * Author URI: https://www.gmbgeeks.com
8 * Text Domain: schema-integrator
9 * Domain Path: /languages
10 * Version: 1.0.1
11 *
12 * @package Schema_Integrator
13 */
14
15// Your code starts here.
16if ( ! defined( 'WPINC' ) ) {
17 die;
18}
19
20function schema_integrator_activation_hook(){
21 $result = get_option('si_key');
22
23 if(!$result) {
24 $rand_num = md5(mt_rand(10000000,99999999));
25
26 add_option( 'si_key', $rand_num);
27 }
28
29}
30
31register_activation_hook( __FILE__, 'schema_integrator_activation_hook' );
32
33
34function schema_integrator_deactivation_hook(){
35
36}
37
38register_deactivation_hook( __FILE__, 'schema_integrator_deactivation_hook' );
39
40
41
42function schema_integrator_settings() {
43 add_option( 'schema_integrator_option_name', 'schmema_integrator_value');
44 register_setting( 'schema_integrator_option_group', 'schema_integrator_option_name', 'myplugin_callback' );
45}
46
47 add_action( 'admin_init', 'schema_integrator_settings' );
48
49
50function schema_integrator_options_page() {
51 $si_option_page = add_options_page('SI-Settings', 'Schema-Integrator', 'manage_options', 'schema_integrator', 'schema_integrator_options');
52 add_action( 'load-' . $si_option_page, 'load_si_admin_styles' );
53
54}
55
56add_action('admin_menu', 'schema_integrator_options_page');
57
58function load_si_admin_styles() {
59
60 wp_register_style( 'custom_wp_admin_bs_css', 'https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css' );
61 wp_enqueue_style( 'custom_wp_admin_bs_css' );
62}
63
64function schema_integrator_options()
65{
66 $key = get_option('si_key');
67 $json = get_option('si_ld_json');
68 ?>
69 <div class="row">
70 <div class="col-md-6">
71 <h2>Schema Integrator Settings</h2>
72 <label for="key_val">Copy the value</label>
73 <input type="text" class="form-control" id="key_val" value="<?php echo $key; ?>">
74 <button type="button" class="btn btn-primary mt-2 cb" data-clipboard-target="#key_val">COPY !</button>
75 </div>
76 <div class="col-md-6">
77 <div class="form-group">
78 <form action="" method="post">
79 <label for="schemaJson">Schema JSON</label>
80 <textarea class="form-control" id="schemaJson" rows="8" readonly><?php echo $json; ?></textarea>
81 <button type="sumbit" class="btn btn-primary mt-1" name="clearSchema">Clear Schema</button>
82 </form>
83 </div>
84 </div>
85 </div>
86
87 <?php }
88
89
90function load_admin_scripts() {
91
92 wp_register_script( 'clipBoardCopy',plugin_dir_url( __FILE__ ) .'/clipBoardCopy.js' );
93 wp_enqueue_script( 'clipBoardCopy' );
94
95 wp_register_script( 'custom_wp_admin_script', 'https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.4/clipboard.min.js' );
96 wp_enqueue_script( 'custom_wp_admin_script' );
97}
98
99add_action( 'admin_enqueue_scripts', 'load_admin_scripts' );
100
101
102function schema_handler() {
103 header('Content-Type: application/json');
104
105 // Takes raw data from the request
106 $json = file_get_contents('php://input');
107 $api_key = get_option('si_key');
108 $request_api_key = $_SERVER['HTTP_X_API_KEY'];
109
110 //check the api key is valid or not
111 if($api_key !== $request_api_key){
112 http_response_code(403);
113 echo json_encode(array(
114 'status'=> 403,
115 'message'=> 'Invalid api key',
116 ));
117 die();
118 }
119 //all the page and post ids set for schema
120 $post_ids = $_SERVER['HTTP_POST_IDS'];
121 $page_ids = $_SERVER['HTTP_PAGE_IDS'];
122
123 $scope = null;
124
125 if($_SERVER['HTTP_SCOPE']){
126
127 if($_SERVER['HTTP_SCOPE'] =='home_page'){
128 update_option( 'si_home_ld_json', $json);
129 }elseif($_SERVER['HTTP_SCOPE'] =='full_site'){
130 $pageID = get_option('page_on_front');
131 update_option( 'si_ld_json', $json);
132 update_option( 'si_home_ld_json', "");
133 }
134
135 }
136
137
138 if(count($post_ids)){
139 parse_str($post_ids, $outputPost);
140
141 foreach($outputPost as $post){
142 update_post_meta($post, 'added_post_for_schema', $json );
143 }
144 }
145
146 if(count($page_ids)){
147 parse_str($page_ids, $outputpage);
148 foreach($outputpage as $page){
149 update_post_meta($page, 'added_post_for_schema', $json );
150 }
151 }
152
153
154 // add schema for specific page and post
155
156 $schmema_data = array(
157 'status' => 200,
158 'message' => 'Success',
159 );
160
161 echo $json;
162 die();
163}
164
165add_action('wp_ajax_nopriv_schema-handler', 'schema_handler');
166add_action('wp_ajax_schema-handler', 'schema_handler');
167
168add_action('wp_head', 'add_header_script');
169
170function add_header_script(){
171
172 if( is_singular() && $data = get_post_meta( get_the_ID(), 'added_post_for_schema', true ) ) {
173 return $data;
174 }
175
176 if( is_home() && $data = get_option('si_home_ld_json')) {
177 return $data;
178 }
179
180 return get_option('si_ld_json');
181
182};
183?>
184 <script type="application/ld+json">
185 <?php
186 echo add_header_script();
187 ?>
188 </script>
189<?php
190
191if (isset($_POST['clearSchema'])) {
192 update_option( 'si_ld_json', "");
193 header("Refresh:0");
194}