Ansico-CV-Blocks/ansico-cv-blocks.php

84 lines
5.3 KiB
PHP
Raw Normal View History

2026-04-13 03:48:00 +00:00
<?php
/**
* Plugin Name: Ansico CV Blocks
* Description: Adds Gutenberg CV blocks, Contact Form, TOC, Profile, and Top Navigation Block.
* Version: 1.0.2
* Author: Andreas Andersen (Ansico)
* Author URI: https://ansico.dk/Ansico
* Plugin URI: https://ansico.dk/Ansico/CV-Blocks
* License: GPL3 or later
* License URI: https://www.gnu.org/licenses/gpl-3.0.html
*/
if (!defined('ABSPATH')) exit;
function cv_v1_register_blocks() {
wp_register_script('cv-v1-blocks', plugins_url('block.js', __FILE__), array('wp-blocks','wp-element','wp-block-editor','wp-components', 'wp-data', 'wp-plugins', 'wp-edit-post'), filemtime(plugin_dir_path(__FILE__) . 'block.js'));
wp_register_style('cv-v1-style', plugins_url('style.css', __FILE__), array(), filemtime(plugin_dir_path(__FILE__) . 'style.css'));
wp_register_style('cv-v1-editor', plugins_url('editor.css', __FILE__), array('wp-edit-blocks'), filemtime(plugin_dir_path(__FILE__) . 'editor.css'));
wp_register_script('cv-v1-frontend', plugins_url('frontend.js', __FILE__), array(), filemtime(plugin_dir_path(__FILE__) . 'frontend.js'), true);
register_block_type('cv/company', ['editor_script' => 'cv-v1-blocks', 'style' => 'cv-v1-style', 'editor_style' => 'cv-v1-editor']);
register_block_type('cv/position', ['editor_script' => 'cv-v1-blocks', 'style' => 'cv-v1-style', 'editor_style' => 'cv-v1-editor']);
register_block_type('cv/education', ['editor_script' => 'cv-v1-blocks', 'style' => 'cv-v1-style', 'editor_style' => 'cv-v1-editor']);
register_block_type('cv/profileheader', ['editor_script' => 'cv-v1-blocks', 'style' => 'cv-v1-style', 'editor_style' => 'cv-v1-editor']);
register_block_type('cv/contentblock', ['editor_script' => 'cv-v1-blocks', 'style' => 'cv-v1-style', 'editor_style' => 'cv-v1-editor']);
register_block_type('cv/container', ['editor_script' => 'cv-v1-blocks', 'style' => 'cv-v1-style', 'editor_style' => 'cv-v1-editor']);
register_block_type('cv/contactform', ['editor_script' => 'cv-v1-blocks', 'style' => 'cv-v1-style', 'editor_style' => 'cv-v1-editor', 'view_script' => 'cv-v1-frontend']);
register_block_type('cv/toc', ['editor_script' => 'cv-v1-blocks', 'style' => 'cv-v1-style', 'editor_style' => 'cv-v1-editor', 'view_script' => 'cv-v1-frontend']);
register_block_type('cv/profile', ['editor_script' => 'cv-v1-blocks', 'style' => 'cv-v1-style', 'editor_style' => 'cv-v1-editor']);
register_block_type('cv/topnav', ['editor_script' => 'cv-v1-blocks', 'style' => 'cv-v1-style', 'editor_style' => 'cv-v1-editor']);
}
add_action('init', 'cv_v1_register_blocks');
add_action('init', function() {
register_post_meta('page', 'ansico_cv_hide_title', array('show_in_rest' => true, 'single' => true, 'type' => 'boolean', 'default' => false));
register_post_meta('page', 'ansico_cv_frame_design', array('show_in_rest' => true, 'single' => true, 'type' => 'boolean', 'default' => false));
});
add_filter('body_class', function($classes) {
if (is_page()) {
$hide_title = get_post_meta(get_the_ID(), 'ansico_cv_hide_title', true);
$frame_design = get_post_meta(get_the_ID(), 'ansico_cv_frame_design', true);
if ($hide_title) $classes[] = 'ansico-cv-hide-title';
if ($frame_design) $classes[] = 'ansico-cv-frame-design';
}
return $classes;
});
add_filter('the_content', function($content) {
if (is_page() && in_the_loop() && is_main_query()) {
$hide_title = get_post_meta(get_the_ID(), 'ansico_cv_hide_title', true);
$frame_design = get_post_meta(get_the_ID(), 'ansico_cv_frame_design', true);
if ($frame_design) {
$title_html = '';
if (!$hide_title) {
$title_html = '<h1 class="cv-page-framed-title">' . get_the_title() . '</h1>';
}
$content = '<div class="cv-page-frame-container">' . $title_html . '<div class="cv-page-frame-inner">' . $content . '</div></div>';
}
}
return $content;
});
add_action('rest_api_init', function () {
register_rest_route('cv-blocks/v1', '/send-message', array('methods' => 'POST', 'callback' => 'cv_blocks_send_email_callback', 'permission_callback' => '__return_true'));
});
function cv_blocks_send_email_callback($request) {
$params = $request->get_params();
$receiver = sanitize_email($params['receiver']);
$name = sanitize_text_field($params['name']);
$email = sanitize_email($params['email']);
$subject = sanitize_text_field($params['subject']);
$message = sanitize_textarea_field($params['message']);
$site_name = get_bloginfo('name');
if (empty($receiver) || empty($name) || empty($email) || empty($message)) return new WP_Error('missing_data', 'Missing required fields', array('status' => 400));
$headers = array('From: ' . $name . ' <' . $email . '>', 'Reply-To: ' . $name . ' <' . $email . '>', 'Content-Type: text/plain; charset=UTF-8');
$email_subject = $subject ? "New Contact Message: " . $subject : "New Contact Message from " . $name;
$email_body = "You have received a new message from your CV Contact Form on website " . $site_name . ".\n\nName: " . $name . "\nEmail: " . $email . "\nSubject: " . $subject . "\n\nMessage:\n" . $message . "\n";
$sent = wp_mail($receiver, $email_subject, $email_body, $headers);
if ($sent) return rest_ensure_response(array('success' => true));
else return new WP_Error('mail_failed', 'Could not send email', array('status' => 500));
}