-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
341 lines (274 loc) · 10.8 KB
/
Copy pathfunctions.php
File metadata and controls
341 lines (274 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
<?php
//Theme support
function ashad_theme_support() {
//Add dynamic title tag support
add_theme_support('title-tag');
$logo_defaults = array(
'height' => 40,
'width' => 120,
'flex-height' => true,
'flex-width' => true,
'header-text' => array( 'site-title', 'site-description' ),
'unlink-homepage-logo' => true,
);
add_theme_support('custom-logo', $logo_defaults);
// add_theme_support( 'custom-header' );
add_theme_support('post-thumbnails');
add_image_size( 'hero-thumb', 760, 400, true );
add_image_size( 'archive-thumb', 380, 200, true );
add_image_size( 'language-thumb', 200, 200, true );
add_image_size( 's512', 512, 512, true );
@ini_set( 'upload_max_size' , '256M' );
@ini_set( 'post_max_size', '256M');
@ini_set( 'max_execution_time', '300' );
}
add_action('after_setup_theme', 'ashad_theme_support');
//Menu
function ashad_menus() {
$locations = array(
'primary' => "Main Menu",
'footer' => "Footer Menu"
);
register_nav_menus($locations);
}
add_action('init', 'ashad_menus');
//Stylesheets
function ashad_register_styles(){
$version = wp_get_theme()->get('Version');
// $version = rand(111,9999); //For development (Reload css)
// wp_enqueue_style('ashad-style', get_template_directory_uri() . "/style.css", array(), $version, 'all');
wp_enqueue_style('ashad-style', get_template_directory_uri() . "/build/style-index.css", array(), $version, 'all');
wp_enqueue_style('ashad-style2', get_template_directory_uri(array('ashad-style')) . "/assets/css/style2.css", array(), $version/*rand(111,9999)*/, 'all');
wp_enqueue_style('ashad-font', '//fonts.googleapis.com/css?family=Titillium+Web:300,400,700', array(), '1.0', 'all');
}
add_action('wp_enqueue_scripts', 'ashad_register_styles');
//JavaScripts
function ashad_register_scripts(){
$version = wp_get_theme()->get('Version');
$params = array(
'root_url' => get_site_url(),
'assets_url' => get_template_directory_uri() . '/assets',
'fetchURL' => admin_url('admin-ajax.php')
);
// wp_enqueue_script('ashad-main', get_template_directory_uri() . "/assets/js/scripts.min.js", array(), '1', true);
wp_enqueue_script('ashad-main', get_template_directory_uri() . "/build/index.js", array(), $version, true);
wp_localize_script( 'ashad-main', 'ashad', $params);
}
add_action('wp_enqueue_scripts', 'ashad_register_scripts');
//Widgets
require_once get_stylesheet_directory() . '/inc/widgets.php';
//Customizer
require_once get_stylesheet_directory() . '/inc/ashad-customizer.php';
new Ashad_Customizer();
//Custom Post Type
require_once get_stylesheet_directory() . '/inc/code_snippet.php';
//User Custom Fileds
require_once get_stylesheet_directory() . '/inc/custom-user-fileds.php';
//Custom API for Search
require_once get_stylesheet_directory() . '/inc/search-route.php';
//AJAX Search
require_once get_stylesheet_directory() . '/inc/ajax-search.php';
//Dynamic Open Graph Image
require_once get_stylesheet_directory() . '/inc/opengraph-image.php';
//Custom Contact Page
require_once get_stylesheet_directory() . '/inc/contact-page.php';
//Custom Email
require_once get_stylesheet_directory() . '/inc/mail-templates.php';
//Redirect subscriber account from admin panel to website home
function ashadRedirectSubscriber() {
$currentUser = wp_get_current_user();
if(count($currentUser->roles) == 1 AND $currentUser->roles[0] == 'subscriber') {
wp_redirect(site_url('/'));
exit();
}
}
add_action('admin_init', 'ashadRedirectSubscriber');
function ashadFeed_request($qv) {
if (isset($qv['feed']) && !isset($qv['post_type']))
$qv['post_type'] = array('code_snippets', 'post');
return $qv;
}
add_filter('request', 'ashadFeed_request');
//Hide admin top bar from subscriber account in website home
function ashadHideSubscriberAdminBar() {
$currentUser = wp_get_current_user();
if(count($currentUser->roles) == 1 AND $currentUser->roles[0] == 'subscriber') {
show_admin_bar(false);
}
}
add_action('wp_loaded', 'ashadHideSubscriberAdminBar');
//Get default thumbnail url
if (!function_exists('get_default_ashad_thumbnail')) {
function get_default_ashad_thumbnail() {
echo get_template_directory_uri() . '/assets/img/default_thumbnail.jpg';
}
}
if (!function_exists('get_default_ashad_thumbnail_url')) {
function get_default_ashad_thumbnail_url() {
get_template_directory_uri() . '/assets/img/default_thumbnail.jpg';
}
}
if (!function_exists('get_default_ashad_language_thumbnail')) {
function get_default_ashad_language_thumbnail() {
return get_template_directory_uri() . '/assets/img/language_thumbnail.png';
}
}
//Reading Time calculation
if (!function_exists('get_reading_time')) :
function get_reading_time($post_id) {
$content = apply_filters('the_content', get_post_field('post_content', $post_id));
$read_words = esc_attr(get_theme_mod('global_show_min_read_number','100'));
$decode_content = html_entity_decode($content);
$filter_shortcode = do_shortcode($decode_content);
$strip_tags = wp_strip_all_tags($filter_shortcode, true);
$count = str_word_count($strip_tags);
$word_per_min = (absint($count) / $read_words);
$word_per_min = ceil($word_per_min);
if ( absint($word_per_min) > 0) {
if ('post' == get_post_type($post_id)):
echo $word_per_min;
endif;
}
if ( absint($word_per_min) == Null) {
echo '0';
}
}
endif;
//Allow upload mime type
add_filter( 'upload_mimes', 'ashad_allow_upload_mime_types', 1, 1 );
function ashad_allow_upload_mime_types( $mime_types ) {
$mime_types['ino'] = 'text/x-arduino';//Arduino ino file
$mime_types['pcb'] = 'application/octet-stream';//CopperCAM pcb file
// $mime_types['bin'] = 'application/octet-stream';//ESP8266 or any firmware file
return $mime_types;
}
//Dynamic SVG Image
add_action( 'init', function() {
add_rewrite_rule( 'customsvg/([a-z0-9-]+)[/]?$', 'index.php?customsvg=$matches[1]', 'top' );
} );
add_filter( 'query_vars', function( $query_vars ) {
$query_vars[] = 'customsvg';
return $query_vars;
} );
add_action( 'template_include', function( $template ) {
if ( get_query_var( 'customsvg' ) == false || get_query_var( 'customsvg' ) == '' ) {
return $template;
}
return get_template_directory() . '/svg.php';
} );
// UPLOAD ENABLE/DISABLE FEATURE
// Create a settings page
function my_upload_settings_menu() {
add_options_page(
'Upload Settings', // Page title
'Upload Settings', // Menu title
'manage_options', // Capability
'upload-settings', // Menu slug
'my_upload_settings_page' // Callback function
);
}
add_action('admin_menu', 'my_upload_settings_menu');
// Display the settings page
function my_upload_settings_page() {
?>
<div class="wrap">
<h1>Upload Settings</h1>
<form method="post" action="options.php">
<?php
settings_fields('my_upload_settings_group');
do_settings_sections('my_upload_settings_group');
$upload_enabled = get_option('upload_enabled', '1'); // Default to enabled
?>
<label for="upload_enabled">
<input type="checkbox" id="upload_enabled" name="upload_enabled" value="1" <?php checked($upload_enabled, '1'); ?> />
Enable File Uploads
</label>
<p class="description">Check this box to allow file uploads.</p>
<?php submit_button(); ?>
</form>
</div>
<?php
}
// Register the setting with sanitization
function my_upload_settings_init() {
register_setting('my_upload_settings_group', 'upload_enabled', [
'type' => 'string',
'sanitize_callback' => function ($value) {
return $value === '1' ? '1' : '0';
},
'default' => '1',
]);
}
add_action('admin_init', 'my_upload_settings_init');
// Set upload limit to zero if disabled
function my_limit_upload_size($file) {
if (get_option('upload_enabled', '1') !== '1') {
$file['error'] = 'File uploads are currently disabled.';
}
return $file;
}
add_filter('wp_handle_upload_prefilter', 'my_limit_upload_size');
// Show admin notice when uploads are disabled
function my_upload_admin_notice() {
if (get_option('upload_enabled', '1') !== '1') {
echo '<div class="notice notice-warning"><p><strong>Warning:</strong> File uploads are currently disabled in settings.</p></div>';
}
}
add_action('admin_notices', 'my_upload_admin_notice');
/**
* Register GitHub Profile API endpoint
*/
function register_github_profile_endpoint() {
register_rest_route('elabins/v1', '/github-profile', array(
'methods' => 'GET',
'callback' => 'get_github_profile_data',
'permission_callback' => '__return_true'
));
}
add_action('rest_api_init', 'register_github_profile_endpoint');
/**
* Fetch GitHub profile data
*/
function get_github_profile_data() {
$username = 'e-labInnovations';
$api_url = "https://profile-summary-for-github.com/api/user/{$username}";
// Set up the request arguments
$args = array(
'timeout' => 30,
'headers' => array(
'User-Agent' => 'WordPress/' . get_bloginfo('version')
)
);
// Make the request
$response = wp_remote_get($api_url, $args);
// Check for errors
if (is_wp_error($response)) {
return new WP_Error('api_error', $response->get_error_message(), array('status' => 500));
}
// Get the response body
$body = wp_remote_retrieve_body($response);
$data = json_decode($body, true);
// Check if the data is valid
if (json_last_error() !== JSON_ERROR_NONE) {
return new WP_Error('json_error', 'Invalid JSON response', array('status' => 500));
}
// Cache the response for 1 hour
set_transient('github_profile_data', $data, HOUR_IN_SECONDS);
return $data;
}
/**
* Add CORS headers for the GitHub profile endpoint
*/
function add_cors_headers() {
header('Access-Control-Allow-Origin: ' . get_site_url());
header('Access-Control-Allow-Methods: GET');
header('Access-Control-Allow-Credentials: true');
}
add_action('rest_api_init', function() {
remove_filter('rest_pre_serve_request', 'rest_send_cors_headers');
add_filter('rest_pre_serve_request', function($value) {
add_cors_headers();
return $value;
});
}, 15);
?>