Untitled
unknown
plain_text
2 years ago
8.0 kB
6
Indexable
<?php
define('IMAGES_URL', get_bloginfo('stylesheet_directory') . '/dist/images');
define('IMAGES_PATH', get_stylesheet_directory() . '/dist/images');
/* JS */
function load_scripts() {
wp_enqueue_script('jquery');
wp_enqueue_script('main_js', get_stylesheet_directory_uri() . '/dist/scripts/main.js', array('jquery'), true);
if (get_post_type() == 'project') {
wp_enqueue_script('prism', get_stylesheet_directory_uri() . '/dist/scripts/prism.js', array('jquery'), true);
}
}
add_action('wp_enqueue_scripts', 'load_scripts');
/* CSS */
function load_styles() {
wp_enqueue_style('main_css', get_stylesheet_directory_uri() . '/dist/styles/main.css');
if (get_post_type() === 'project') {
wp_enqueue_style('prism', get_stylesheet_directory_uri() . '/dist/styles/prism.css');
}
}
add_action( 'wp_enqueue_scripts', 'load_styles' );
/* MENUS */
function register_menus() {
register_nav_menu('main_menu', 'Main Menu');
}
add_action('after_setup_theme', 'register_menus');
/* FONTS */
function wpb_add_google_fonts() {
wp_enqueue_style( 'wpb-google-fonts', 'http://fonts.googleapis.com/css?family=Open+Sans:300,600', false );
}
add_action( 'wp_enqueue_scripts', 'wpb_add_google_fonts' );
/* CPT: PROJECT */
function my_custom_post_project() {
$labels = array(
'name' => _x('Projects', 'post type general name'),
'singular_name' => _x('Project', 'post type singular name'),
'add_new' => __('Add New Project'),
'add_new_item' => __('Add New Project'),
'edit_item' => __('Edit Project'),
'new_item' => __('New Project'),
'all_items' => __('All Projects'),
'view_item' => __('View Project'),
'search_items' => __('Search Projects'),
'not_found' => __('No projects found'),
'not_found_in_trash' => __('No projects found in the Trash'),
'parent_item_colon' => '',
'menu_name' => 'Projects'
);
$args = array(
'labels' => $labels,
'show_in_rest' => true,
'description' => 'Holds our projects and project specific data',
'public' => true,
'menu_position' => 5,
'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'comments', 'custom-fields'),
'has_archive' => true,
);
register_post_type('project', $args);
}
add_action('init', 'my_custom_post_project');
function my_updated_messages_project($messages) {
global $post, $post_ID;
$messages['project'] = array(
0 => '',
1 => sprintf( __('Project updated. <a href="%s">View project</a>'), esc_url( get_permalink($post_ID) ) ),
2 => __('Custom field updated.'),
3 => __('Custom field deleted.'),
4 => __('Project updated.'),
5 => isset($_GET['revision']) ? sprintf( __('Project restored to revision from %s'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
6 => sprintf( __('Project published. <a href="%s">View project</a>'), esc_url( get_permalink($post_ID) ) ),
7 => __('Project saved.'),
8 => sprintf( __('Project submitted. <a target="_blank" href="%s">Preview project</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID)))),
9 => sprintf( __('Project scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview project</a>'), date_i18n( __( 'M j, Y @ G:i' ), strtotime($post->post_date)), esc_url(get_permalink($post_ID))),
10 => sprintf( __('Project draft updated. <a target="_blank" href="%s">Preview project</a>'), esc_url( add_query_arg('preview', 'true', get_permalink($post_ID) ))),
);
return $messages;
}
add_filter('post_updated_messages', 'my_updated_messages_project');
/* HIDE ADMIN BAR */
add_filter('show_admin_bar', '__return_false');
/* CONTACT FORM ERROR MESSAGES */
function generate_response($type, $message) {
$t = $type === 'success' ? 'success' : 'error';
return "<div class='$t'>$message</div><br/>";
}
/* REST API ENDPOINTS FOR PROJECTS */
function last_projects() {
$args = array(
'post_type' => 'project',
'posts_per_page'=> 3
);
$posts = get_posts($args);
foreach ($posts as $key => $post) {
$posts[$key]->acf = get_fields($post->ID);
}
return $posts;
}
function all_projects() {
$args = array(
'post_type' => 'project',
'posts_per_page'=> -1
);
$posts = get_posts($args);
foreach ($posts as $key => $post) {
$posts[$key]->acf = get_fields($post->ID);
}
return $posts;
}
function single_project($data) {
$post_ID = $data['id'];
return get_post($post_ID);
}
add_action('rest_api_init', function () {
register_rest_route( 'last_projects/v1', 'posts', array(
'methods' => 'GET',
'callback' => 'last_projects'
));
register_rest_route( 'projects/v1', 'posts', array(
'methods' => 'GET',
'callback' => 'all_projects'
));
register_rest_route( 'project/v1', 'post/(?P<id>\d+)', array(
'methods' => 'GET',
'callback' => 'single_project',
'args' => [
'id'
]
));
});
/* REST API ENDPOINT FOR MAIN MENU */
function main_menu() {
return wp_get_nav_menu_items('main_menu');
}
add_action( 'rest_api_init', function () {
register_rest_route( 'menus/v1', 'main_menu', array(
'methods' => 'GET',
'callback' => 'main_menu'
));
});
/* REST API ENDPOINT FOR SENDING EMAIL */
function sendContactMail(WP_REST_Request $request) {
$siteName = wp_strip_all_tags(trim(get_option('blogname')));
$contactName = $request['contact_name'];
$contactEmail = $request['contact_email'];
$contactMessage = $request['contact_message'];
$subject = "[$siteName] (testing) New message from $contactName";
$subject = "New message sent from site $siteName - $contactName <$contactEmail>";
$body = "<p><b>Name:</b> $contactName</p>";
$body .= "<p><b>Email:</b> $contactEmail</p>";
$body .= "<p style='white-space: pre;'><b>Message:</b><br>$contactMessage</p>";
$to = get_option('admin_email');
$headers = array(
'Content-Type: text/html; charset=UTF-8',
"Reply-To: $contactName <$contactEmail>",
);
$result_mail_function = false;
if ($result_mail_function) {
return new WP_REST_Response(array('message' => 'Form sent!'));
} else {
return new WP_Error('502', "Server issue, please use your usual email client instead.");
}
}
/* CONTACT PAGE FORM SUBMISSION ENDPOINT - REST API */
add_action('rest_api_init', function() {
register_rest_route('contact/v1', 'send', array(
'methods' => 'POST',
'callback' => 'sendContactMail',
'permission_callback' => '__return_true',
'args' => array(
'contact_name' => array(
'required' => true,
'validate_callback' => function ($value) {
return preg_match('/[a-z0-9]{2,}/i', $value) ? true :
new WP_Error('invalid_contact_name', 'Your custom error.');
},
'sanitize_callback' => 'sanitize_text_field',
),
'contact_email' => array(
'required' => true,
'validate_callback' => 'is_email',
'sanitize_callback' => 'sanitize_email',
),
'contact_message' => array(
'required' => true,
'sanitize_callback' => 'sanitize_textarea_field',
),
),
));
});
/* ADDS CPT PROJECT'S 'VIDEO' CUSTOM FIELD TO REST API */
add_action('rest_api_init', 'register_featured_video');
function register_featured_video() {
register_rest_field(
'project',
'video',
array(
'get_callback' => 'get_featured_video',
'update_callback' => null,
'schema' => null,
)
);
}
function get_featured_video($object) {
$post_id = $object['id'];
return get_post_meta($post_id);
}
?>Editor is loading...