متوجه شدم. برای ساخت افزونه وردپرسی ترسیم پرسپکتیو، کد اصلی افزونه را به صورت کامل مینویسم:
<?php
/*
Plugin Name: Professional Perspective Drawer
Description: Advanced Perspective Drawing Tool for WordPress
Version: 1.0
Author: Your Name
*/
class PerspectiveDrawerPlugin {
public function __construct() {
add_action('admin_menu', [$this, 'add_plugin_page']);
add_action('admin_enqueue_scripts', [$this, 'enqueue_scripts']);
add_action('wp_ajax_save_perspective_drawing', [$this, 'save_drawing']);
}
public function add_plugin_page() {
add_menu_page(
'Perspective Drawer',
'Perspective Drawer',
'manage_options',
'perspective-drawer',
[$this, 'render_plugin_page'],
'dashicons-layout',
6
);
}
public function enqueue_scripts($hook) {
if ($hook != 'toplevel_page_perspective-drawer') return;
wp_enqueue_script('fabric-js', 'https://cdnjs.cloudflare.com/ajax/libs/fabric.js/5.3.1/fabric.min.js', [], '5.3.1', true);
wp_enqueue_script('perspective-drawer-script', plugin_dir_url(__FILE__) . 'js/perspective-drawer.js', ['fabric-js', 'jquery'], '1.0', true);
wp_enqueue_style('perspective-drawer-style', plugin_dir_url(__FILE__) . 'css/perspective-drawer.css');
wp_localize_script('perspective-drawer-script', 'perspectiveDrawerAjax', [
'ajax_url' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('perspective_drawer_nonce')
]);
}
public function render_plugin_page() {
?>
<div class="wrap perspective-drawer-container">
<h1>Professional Perspective Drawing Tool</h1>
<div class="perspective-controls">
<select id="perspective-type">
<option value="one-point">One Point Perspective</option>
<option value="two-point">Two Point Perspective</option>
<option value="three-point">Three Point Perspective</option>
</select>
<div class="shape-controls">
<button id="draw-cube">Draw Cube</button>
<button id="draw-sphere">Draw Sphere</button>
<button id="draw-cylinder">Draw Cylinder</button>
</div>
</div>
<div class="canvas-container">
<canvas id="perspective-canvas" width="800" height="600"></canvas>
</div>
<div class="drawing-tools">
<input type="color" id="color-picker" value="#000000">
<input type="range" id="line-width" min="1" max="10" value="2">
<button id="save-drawing">Save Drawing</button>
<button id="clear-canvas">Clear Canvas</button>
</div>
</div>
<?php
}
public function save_drawing() {
check_ajax_referer('perspective_drawer_nonce', 'security');
$drawing_data = isset($_POST['drawing_data']) ? sanitize_text_field($_POST['drawing_data']) : '';
$upload_dir = wp_upload_dir();
$filename = 'perspective_drawing_' . time() . '.png';
$filepath = $upload_dir['path'] . '/' . $filename;
// Base64 decoding and saving
$image_data = str_replace('data:image/png;base64,', '', $drawing_data);
$image_data = str_replace(' ', '+', $image_data);
$decoded_image = base64_decode($image_data);
file_put_contents($filepath, $decoded_image);
$attachment = [
'guid' => $upload_dir['url'] . '/' . $filename,
'post_mime_type' => 'image/png',
'post_title' => 'Perspective Drawing',
'post_content' => '',
'post_status' => 'inherit'
];
$attach_id = wp_insert_attachment($attachment, $filepath);
wp_send_json_success([
'message' => 'Drawing saved successfully',
'attachment_id' => $attach_id
]);
}
}
new PerspectiveDrawerPlugin();
برای تکمیل، شما نیاز به ایجاد دو فایل دیگر دارید:
فایل JavaScript (perspective-drawer.js):
jQuery(document).ready(function($) {
var canvas = new fabric.Canvas('perspective-canvas', {
isDrawingMode: true
});
// Perspective Type Selection
$('#perspective-type').on('change', function() {
var type = $(this).val();
// Implement perspective grid logic based on type
switch(type) {
case 'one-point':
drawOnePointPerspectiveGrid();
break;
case 'two-point':
drawTwoPointPerspectiveGrid();
break;
case 'three-point':
drawThreePointPerspectiveGrid();
break;
}
});
// Drawing Tools
$('#color-picker').on('change', function() {
canvas.freeDrawingBrush.color = this.value;
});
$('#line-width').on('input', function() {
canvas.freeDrawingBrush.width = parseInt(this.value);
});
// Shape Drawing Functions
$('#draw-cube').on('click', function() {
drawCubeInPerspective();
});
$('#draw-sphere').on('click', function() {
drawSphereInPerspective();
});
$('#save-drawing').on('click', function() {
var dataURL = canvas.toDataURL();
$.ajax({
url: perspectiveDrawerAjax.ajax_url,
type: 'POST',
data: {
action: 'save_perspective_drawing',
security: perspectiveDrawerAjax.nonce,
drawing_data: dataURL
},
success: function(response) {
alert('Drawing saved successfully!');
}
});
});
$('#clear-canvas').on('click', function() {
canvas.clear();
});
// Perspective Grid Functions
function drawOnePointPerspectiveGrid() {
// Implement one-point perspective grid
}
function drawTwoPointPerspectiveGrid() {
// Implement two-point perspective grid
}
function drawThreePointPerspectiveGrid() {
// Implement three-point perspective grid
}
function drawCubeInPerspective() {
// Implement cube drawing logic
}
function drawSphereInPerspective() {
// Implement sphere drawing logic
}
});
فایل CSS (perspective-drawer.css):
.perspective-drawer-container {
max-width: 1000px;
margin: 0 auto;
}
.canvas-container {
border: 2px solid #ddd;
margin: 20px 0;
}
#perspective-canvas {
background-color: white;
}
.perspective-controls,
.drawing-tools {
display: flex;
gap: 10px;
margin-bottom: 15px;
}
این کد یک چارچوب اولیه برای افزونه ترسیم پرسپکتیو در وردپرس ایجاد میکند. برای تکمیل کامل نیاز به توسعه بیشتر دارد، اما یک نقطه شروع خوب است.
نکات مهم:
- از Fabric.js برای ترسیم استفاده شده
- قابلیت ذخیرهسازی تصاویر
- انتخاب نوع پرسپکتیو
- ابزارهای رسم اولیه
برای تکمیل کامل نیاز به کدنویسی تخصصیتر در بخشهای گرید پرسپکتیو و ترسیم اشکال هندسی دارد.