Untitled
public
Oct 23, 2023
Never
89
1 <?php 2 define('IMAGES_URL', get_bloginfo('stylesheet_directory') . '/dist/images'); 3 define('IMAGES_PATH', get_stylesheet_directory() . '/dist/images'); 4 5 /* JS */ 6 7 function load_scripts() { 8 wp_enqueue_script('jquery'); 9 wp_enqueue_script('main_js', get_stylesheet_directory_uri() . '/dist/scripts/main.js', array('jquery'), true); 10 if (get_post_type() == 'project') { 11 wp_enqueue_script('prism', get_stylesheet_directory_uri() . '/dist/scripts/prism.js', array('jquery'), true); 12 } 13 } 14 add_action('wp_enqueue_scripts', 'load_scripts'); 15 16 /* CSS */ 17 18 function load_styles() { 19 wp_enqueue_style('main_css', get_stylesheet_directory_uri() . '/dist/styles/main.css'); 20 if (get_post_type() === 'project') { 21 wp_enqueue_style('prism', get_stylesheet_directory_uri() . '/dist/styles/prism.css'); 22 } 23 } 24 add_action( 'wp_enqueue_scripts', 'load_styles' ); 25 26 /* MENUS */ 27 28 function register_menus() { 29 register_nav_menu('main_menu', 'Main Menu'); 30 } 31 add_action('after_setup_theme', 'register_menus'); 32 33 /* FONTS */ 34 35 function wpb_add_google_fonts() { 36 wp_enqueue_style( 'wpb-google-fonts', 'http://fonts.googleapis.com/css?family=Open+Sans:300,600', false ); 37 } 38 add_action( 'wp_enqueue_scripts', 'wpb_add_google_fonts' ); 39 40 /* CPT: PROJECT */ 41 42 function my_custom_post_project() { 43 $labels = array( 44 'name' => _x('Projects', 'post type general name'), 45 'singular_name' => _x('Project', 'post type singular name'), 46 'add_new' => __('Add New Project'), 47 'add_new_item' => __('Add New Project'), 48 'edit_item' => __('Edit Project'), 49 'new_item' => __('New Project'), 50 'all_items' => __('All Projects'), 51 'view_item' => __('View Project'), 52 'search_items' => __('Search Projects'), 53 'not_found' => __('No projects found'), 54 'not_found_in_trash' => __('No projects found in the Trash'), 55 'parent_item_colon' => '', 56 'menu_name' => 'Projects' 57 ); 58 $args = array( 59 'labels' => $labels, 60 'show_in_rest' => true, 61 'description' => 'Holds our projects and project specific data', 62 'public' => true, 63 'menu_position' => 5, 64 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'comments', 'custom-fields'), 65 'has_archive' => true, 66 ); 67 register_post_type('project', $args); 68 } 69 add_action('init', 'my_custom_post_project'); 70 71 function my_updated_messages_project($messages) { 72 global $post, $post_ID; 73 $messages['project'] = array( 74 0 => '', 75 1 => sprintf( __('Project updated. <a href="%s">View project</a>'), esc_url( get_permalink($post_ID) ) ), 76 2 => __('Custom field updated.'), 77 3 => __('Custom field deleted.'), 78 4 => __('Project updated.'), 79 5 => isset($_GET['revision']) ? sprintf( __('Project restored to revision from %s'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false, 80 6 => sprintf( __('Project published. <a href="%s">View project</a>'), esc_url( get_permalink($post_ID) ) ), 81 7 => __('Project saved.'), 82 8 => sprintf( __('Project submitted. <a target="_blank" href="%s">Preview project</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID)))), 83 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))), 84 10 => sprintf( __('Project draft updated. <a target="_blank" href="%s">Preview project</a>'), esc_url( add_query_arg('preview', 'true', get_permalink($post_ID) ))), 85 ); 86 return $messages; 87 } 88 add_filter('post_updated_messages', 'my_updated_messages_project'); 89 90 /* HIDE ADMIN BAR */ 91 92 add_filter('show_admin_bar', '__return_false'); 93 94 /* CONTACT FORM ERROR MESSAGES */ 95 96 function generate_response($type, $message) { 97 $t = $type === 'success' ? 'success' : 'error'; 98 return "<div class='$t'>$message</div><br/>"; 99 } 100 101 /* REST API ENDPOINTS FOR PROJECTS */ 102 103 function last_projects() { 104 $args = array( 105 'post_type' => 'project', 106 'posts_per_page'=> 3 107 ); 108 $posts = get_posts($args); 109 foreach ($posts as $key => $post) { 110 $posts[$key]->acf = get_fields($post->ID); 111 } 112 return $posts; 113 } 114 115 function all_projects() { 116 $args = array( 117 'post_type' => 'project', 118 'posts_per_page'=> -1 119 ); 120 $posts = get_posts($args); 121 foreach ($posts as $key => $post) { 122 $posts[$key]->acf = get_fields($post->ID); 123 } 124 return $posts; 125 } 126 127 function single_project($data) { 128 $post_ID = $data['id']; 129 return get_post($post_ID); 130 } 131 132 add_action('rest_api_init', function () { 133 register_rest_route( 'last_projects/v1', 'posts', array( 134 'methods' => 'GET', 135 'callback' => 'last_projects' 136 )); 137 138 register_rest_route( 'projects/v1', 'posts', array( 139 'methods' => 'GET', 140 'callback' => 'all_projects' 141 )); 142 143 register_rest_route( 'project/v1', 'post/(?P<id>\d+)', array( 144 'methods' => 'GET', 145 'callback' => 'single_project', 146 'args' => [ 147 'id' 148 ] 149 )); 150 }); 151 152 /* REST API ENDPOINT FOR MAIN MENU */ 153 154 function main_menu() { 155 return wp_get_nav_menu_items('main_menu'); 156 } 157 158 add_action( 'rest_api_init', function () { 159 register_rest_route( 'menus/v1', 'main_menu', array( 160 'methods' => 'GET', 161 'callback' => 'main_menu' 162 )); 163 }); 164 165 /* REST API ENDPOINT FOR SENDING EMAIL */ 166 167 function sendContactMail(WP_REST_Request $request) { 168 $siteName = wp_strip_all_tags(trim(get_option('blogname'))); 169 170 $contactName = $request['contact_name']; 171 $contactEmail = $request['contact_email']; 172 $contactMessage = $request['contact_message']; 173 174 $subject = "[$siteName] (testing) New message from $contactName"; 175 $subject = "New message sent from site $siteName - $contactName <$contactEmail>"; 176 177 $body = "<p><b>Name:</b> $contactName</p>"; 178 $body .= "<p><b>Email:</b> $contactEmail</p>"; 179 $body .= "<p style='white-space: pre;'><b>Message:</b><br>$contactMessage</p>"; 180 181 $to = get_option('admin_email'); 182 $headers = array( 183 'Content-Type: text/html; charset=UTF-8', 184 "Reply-To: $contactName <$contactEmail>", 185 ); 186 187 $result_mail_function = false; 188 189 if ($result_mail_function) { 190 return new WP_REST_Response(array('message' => 'Form sent!')); 191 } else { 192 return new WP_Error('502', "Server issue, please use your usual email client instead."); 193 } 194 } 195 196 /* CONTACT PAGE FORM SUBMISSION ENDPOINT - REST API */ 197 198 add_action('rest_api_init', function() { 199 register_rest_route('contact/v1', 'send', array( 200 'methods' => 'POST', 201 'callback' => 'sendContactMail', 202 'permission_callback' => '__return_true', 203 'args' => array( 204 'contact_name' => array( 205 'required' => true, 206 'validate_callback' => function ($value) { 207 return preg_match('/[a-z0-9]{2,}/i', $value) ? true : 208 new WP_Error('invalid_contact_name', 'Your custom error.'); 209 }, 210 'sanitize_callback' => 'sanitize_text_field', 211 ), 212 'contact_email' => array( 213 'required' => true, 214 'validate_callback' => 'is_email', 215 'sanitize_callback' => 'sanitize_email', 216 ), 217 'contact_message' => array( 218 'required' => true, 219 'sanitize_callback' => 'sanitize_textarea_field', 220 ), 221 ), 222 )); 223 }); 224 225 /* ADDS CPT PROJECT'S 'VIDEO' CUSTOM FIELD TO REST API */ 226 227 add_action('rest_api_init', 'register_featured_video'); 228 229 function register_featured_video() { 230 register_rest_field( 231 'project', 232 'video', 233 array( 234 'get_callback' => 'get_featured_video', 235 'update_callback' => null, 236 'schema' => null, 237 ) 238 ); 239 } 240 241 function get_featured_video($object) { 242 $post_id = $object['id']; 243 return get_post_meta($post_id); 244 } 245 ?>