- WordPress core y plugins - Tema Twenty Twenty-Four configurado - Plugin allow-unfiltered-html.php simplificado - .gitignore configurado para excluir wp-config.php y uploads 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
248 lines
8.1 KiB
PHP
Executable File
248 lines
8.1 KiB
PHP
Executable File
<?php
|
|
/**
|
|
* Plugin Name: CalculaTG
|
|
* Description: A plugin to show calculator to calculate concreto
|
|
* Author: Suresh N, Tamilselvi V
|
|
* Author URI: https://www.deshtech.co.in/
|
|
* Version: 1.0.1
|
|
*/
|
|
|
|
define('CALCULATG_TEXT_DOMAIN', 'calculatg');
|
|
define('CALCULATG_VERSION', '1.0.1');
|
|
define('CALCULATG_PLUGIN_DIR', dirname(__FILE__) );
|
|
|
|
|
|
require_once CALCULATG_PLUGIN_DIR . '/lib/PhpSpreadsheet-master/vendor/autoload.php';
|
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
|
|
|
|
class CalculaTG {
|
|
|
|
public function __construct() {
|
|
|
|
add_shortcode('calculatg', [ $this, 'shortcode_handler' ]);
|
|
|
|
add_action('admin_menu', [ $this, 'admin_menu' ]);
|
|
|
|
add_action( 'wp_ajax_calculatg_export_excel', [ $this, 'export_excel' ] );
|
|
add_action( 'wp_ajax_nopriv_calculatg_export_excel', [ $this, 'export_excel' ] );
|
|
|
|
}
|
|
|
|
public function admin_menu() {
|
|
add_options_page(
|
|
__('CalculaTG', CALCULATG_TEXT_DOMAIN),
|
|
__('CalculaTG', CALCULATG_TEXT_DOMAIN),
|
|
'manage_options',
|
|
'calculatg-settings',
|
|
[ $this, 'calculatg_settings' ]
|
|
);
|
|
}
|
|
|
|
public function calculatg_settings() {
|
|
|
|
if ( isset($_POST['calculatg_settings']) ) {
|
|
update_option('calculatg_settings', $_POST['calculatg_settings']);
|
|
}
|
|
|
|
$calculatg_settings = get_option('calculatg_settings', array());
|
|
|
|
?>
|
|
<style type="text/css">
|
|
label { display: inline-block; min-width: 250px; }
|
|
.row { margin-bottom: 20px; }
|
|
</style>
|
|
|
|
<div class="wrap">
|
|
<h2>CalculaTG Settings</h2>
|
|
<p> </p>
|
|
|
|
<form action="" method="post">
|
|
<div class="row">
|
|
<label>Excel Export - Allowed Roles</label>
|
|
<select name="calculatg_settings[excel_export_allowed_roles][]" multiple size="4">
|
|
<?php
|
|
$editable_roles = array_reverse( get_editable_roles() );
|
|
|
|
foreach ( $editable_roles as $role => $details ) {
|
|
$name = translate_user_role( $details['name'] );
|
|
// Preselect specified role.
|
|
if ( in_array($role, $calculatg_settings['excel_export_allowed_roles']) ) {
|
|
echo "\n\t<option selected='selected' value='" . esc_attr( $role ) . "'>$name</option>";
|
|
} else {
|
|
echo "\n\t<option value='" . esc_attr( $role ) . "'>$name</option>";
|
|
}
|
|
}
|
|
?>
|
|
</select>
|
|
</div>
|
|
|
|
<div class="row">
|
|
<label>Restricted user - Redirect URL</label>
|
|
<input type="text" name="calculatg_settings[restricted_user_redirect_url]" value="<?php echo isset($calculatg_settings['restricted_user_redirect_url']) ? $calculatg_settings['restricted_user_redirect_url'] : ''; ?>" class="regular-text ltr">
|
|
</div>
|
|
|
|
<p> </p>
|
|
<?php submit_button(); ?>
|
|
</form>
|
|
</div>
|
|
<?php
|
|
}
|
|
|
|
public function export_excel() {
|
|
|
|
$wp_current_user = wp_get_current_user();
|
|
$calculatg_settings = get_option('calculatg_settings', array());
|
|
|
|
if( empty($wp_current_user) || empty($wp_current_user->roles) || count ( array_intersect( $calculatg_settings['excel_export_allowed_roles'], $wp_current_user->roles ) ) === 0 ) {
|
|
|
|
wp_send_json(
|
|
array(
|
|
'redirect' => true,
|
|
'redirect_url' => $calculatg_settings['restricted_user_redirect_url']
|
|
)
|
|
);
|
|
}
|
|
|
|
if ( isset($_POST['check']) ) {
|
|
wp_send_json(
|
|
array(
|
|
'redirect' => false,
|
|
'redirect_url' => ''
|
|
)
|
|
);
|
|
}
|
|
|
|
$type = sanitize_text_field( $_POST['type'] );
|
|
|
|
switch($type) {
|
|
case '1':
|
|
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load( CALCULATG_PLUGIN_DIR . '/files/F1.xlsx' );
|
|
$sheet = $spreadsheet->getActiveSheet();
|
|
|
|
$post_data = array_map('sanitize_text_field', $_POST);
|
|
$post_data = array_map('stripslashes', $post_data);
|
|
|
|
$sheet->setCellValue('B8', $post_data['application_str']);
|
|
$sheet->setCellValue('B10', $post_data['resistance_str']);
|
|
$sheet->setCellValue('B12', $post_data['aggregate_origin_str']);
|
|
$sheet->setCellValue('B14', $post_data['gravel_size_str']);
|
|
$sheet->setCellValue('E14', $post_data['sand_moisture_str']);
|
|
$sheet->setCellValue('B16', $post_data['volume_liters_str']);
|
|
|
|
$sheet->setCellValue('E20', $post_data['tab_str']);
|
|
|
|
if ( $post_data['active_tab'] == '0' ) {
|
|
|
|
$sheet->setCellValue('E16', $post_data['tab1_number_of_stirrers']);
|
|
$sheet->setCellValue('B20', $post_data['tab1_approximate_volume']);
|
|
$sheet->setCellValue('B22', $post_data['tab1_agua']);
|
|
$sheet->setCellValue('E22', $post_data['tab1_agua_botes']);
|
|
$sheet->setCellValue('B24', $post_data['tab1_cemento']);
|
|
$sheet->setCellValue('E24', $post_data['tab1_cemento_sacos']);
|
|
$sheet->setCellValue('B26', $post_data['tab1_arena']);
|
|
$sheet->setCellValue('E26', $post_data['tab1_arena_botes']);
|
|
$sheet->setCellValue('B28', $post_data['tab1_grava']);
|
|
$sheet->setCellValue('E28', $post_data['tab1_grava_botes']);
|
|
|
|
$sheet->setCellValue('F22', 'litros');
|
|
$sheet->setCellValue('F24', 'litros');
|
|
$sheet->setCellValue('F26', 'litros');
|
|
$sheet->setCellValue('F28', 'litros');
|
|
|
|
}
|
|
else if ( $post_data['active_tab'] == '1' ) {
|
|
|
|
$sheet->setCellValue('B20', $post_data['tab2_manufacturer_volume']);
|
|
$sheet->setCellValue('B22', $post_data['tab2_agua']);
|
|
$sheet->setCellValue('E22', $post_data['tab2_agua_botes']);
|
|
$sheet->setCellValue('B24', $post_data['tab2_cemento']);
|
|
$sheet->setCellValue('E24', $post_data['tab2_cemento_sacos']);
|
|
$sheet->setCellValue('B26', $post_data['tab2_arena']);
|
|
$sheet->setCellValue('E26', $post_data['tab2_arena_botes']);
|
|
$sheet->setCellValue('B28', $post_data['tab2_grava']);
|
|
$sheet->setCellValue('E28', $post_data['tab2_grava_botes']);
|
|
|
|
}
|
|
else if ( $post_data['active_tab'] == '2' ) {
|
|
|
|
$sheet->setCellValue('B20', $post_data['tab3_approximate_volume']);
|
|
$sheet->setCellValue('B22', $post_data['tab3_agua']);
|
|
$sheet->setCellValue('E22', $post_data['tab3_agua_botes']);
|
|
$sheet->setCellValue('B24', $post_data['tab3_cemento']);
|
|
$sheet->setCellValue('E24', $post_data['tab3_cemento_sacos']);
|
|
$sheet->setCellValue('B26', $post_data['tab3_arena']);
|
|
$sheet->setCellValue('E26', $post_data['tab3_arena_botes']);
|
|
$sheet->setCellValue('B28', $post_data['tab3_grava']);
|
|
$sheet->setCellValue('E28', $post_data['tab3_grava_botes']);
|
|
|
|
}
|
|
else if ( $post_data['active_tab'] == '3' ) {
|
|
|
|
$sheet->setCellValue('B20', $post_data['tab4_manufacturer_volume']);
|
|
$sheet->setCellValue('B22', $post_data['tab4_agua_botes']);
|
|
$sheet->setCellValue('E22', $post_data['tab3_agua_botes_2']);
|
|
$sheet->setCellValue('B24', $post_data['tab4_cemento_sacos']);
|
|
$sheet->setCellValue('E24', $post_data['tab4_cemento_sacos_2']);
|
|
$sheet->setCellValue('B26', $post_data['tab4_arena_botes']);
|
|
$sheet->setCellValue('E26', $post_data['tab4_arena_botes_2']);
|
|
$sheet->setCellValue('B28', $post_data['tab4_grava_botes']);
|
|
$sheet->setCellValue('E28', $post_data['tab4_grava_botes_2']);
|
|
|
|
}
|
|
|
|
$writer = new Xlsx($spreadsheet);
|
|
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
|
|
header('Content-Disposition: attachment; filename="Dosification Concreto.xlsx"');
|
|
$writer->save("php://output");
|
|
|
|
//$writer->save(CALCULATG_PLUGIN_DIR . '/temp/Dosification Concreto.xlsx');
|
|
|
|
break;
|
|
}
|
|
|
|
exit;
|
|
}
|
|
|
|
public function shortcode_handler( $atts = array() ) {
|
|
$atts = shortcode_atts( array(
|
|
'type' => '1',
|
|
), $atts, CALCULATG_TEXT_DOMAIN );
|
|
|
|
extract( $atts );
|
|
|
|
wp_enqueue_style('calculatg-css', plugin_dir_url(__FILE__) . '/css/calculatg.css', '', CALCULATG_VERSION);
|
|
wp_enqueue_style('jquery-ui-tabs', '//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css', '', CALCULATG_VERSION);
|
|
|
|
wp_enqueue_script("jquery-ui-tabs");
|
|
|
|
$calculatg_settings = get_option('calculatg_settings', array());
|
|
|
|
$html = '<div class="calculatg-wrap">';
|
|
|
|
ob_start();
|
|
|
|
switch( $type ) {
|
|
case '1':
|
|
default:
|
|
wp_enqueue_script('calculatg-js', plugin_dir_url(__FILE__) . '/js/dosification-concreto.js', ['jquery'], CALCULATG_VERSION, true);
|
|
wp_localize_script( 'calculatg-js', 'calculatg_obj',
|
|
array(
|
|
'ajaxurl' => admin_url( 'admin-ajax.php' )
|
|
)
|
|
);
|
|
include_once 'files/dosification-concreto.html';
|
|
break;
|
|
}
|
|
|
|
$html .= ob_get_clean();
|
|
|
|
$html .= '</div>';
|
|
|
|
return $html;
|
|
}
|
|
|
|
}
|
|
|
|
$calculatg = new CalculaTG;
|