1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
<?php
if ( !defined( 'ABSPATH' ) ) {
exit;
}
if ( !class_exists( 'WooCommerce_Delivery_Notes_Settings' ) ) {
class WooCommerce_Delivery_Notes_Settings {
public $id;
public function __construct() {
$this->id = 'wcdn-settings';
add_filter( 'woocommerce_settings_tabs_array', array( $this, 'add_settings_page' ), 200 );
add_action( 'woocommerce_settings_start', array( $this, 'add_assets' ) );
add_action( 'woocommerce_settings_' . $this->id, array( $this, 'output' ) );
add_action( 'woocommerce_settings_save_' . $this->id, array( $this, 'save' ) );
add_action( 'woocommerce_admin_field_wcdn_image_select', array( $this, 'output_image_select' ) );
add_action( 'wp_ajax_wcdn_settings_load_image', array( $this, 'load_image_ajax' ) );
add_filter( 'wcdn_get_settings', array( $this, 'generate_template_type_fields' ), 10, 2 );
}
public function add_assets() {
wp_enqueue_style( 'woocommerce-delivery-notes-admin', WooCommerce_Delivery_Notes::$plugin_url . 'css/admin.css' );
wp_enqueue_media();
wp_enqueue_script( 'woocommerce-delivery-notes-print-link', WooCommerce_Delivery_Notes::$plugin_url . 'js/jquery.print-link.js', array( 'jquery' ) );
wp_enqueue_script( 'woocommerce-delivery-notes-admin', WooCommerce_Delivery_Notes::$plugin_url . 'js/admin.js', array( 'jquery', 'custom-header', 'woocommerce-delivery-notes-print-link' ) );
$translation = array( 'resetCounter' => __( 'Do you really want to reset the counter to zero? This process can\'t be undone.', 'woocommerce-delivery-notes' ) );
wp_localize_script( 'woocommerce-delivery-notes-admin', 'WCDNText', $translation );
}
public function add_settings_page( $settings_tabs ) {
$settings_tabs[$this->id] = __( 'Print', 'woocommerce-delivery-notes' );
return $settings_tabs;
}
public function output() {
global $current_section;
$settings = $this->get_settings( $current_section );
woocommerce_admin_fields( $settings );
}
function save() {
global $current_section;
set_transient( 'wcdn_flush_rewrite_rules', true );
$settings = $this->get_settings( $current_section );
woocommerce_update_options( $settings );
}
public function get_settings( $section = '' ) {
$settings = apply_filters( 'wcdn_get_settings_no_section',
array(
array(
'title' => __( 'Template', 'woocommerce-delivery-notes' ),
'type' => 'title',
'desc' => $this->get_template_description(),
'id' => 'general_options'
),
array(
'title' => __( 'Style', 'woocommerce-delivery-notes' ),
'desc' => sprintf( __( 'The default print style. Read the <a href="%1$s">FAQ</a> to learn how to customize it.', 'woocommerce-delivery-notes' ), 'https://wordpress.org/plugins/woocommerce-delivery-notes/faq/', '#' ),
'id' => 'wcdn_template_style',
'class' => 'wc-enhanced-select',
'default' => '',
'type' => 'select',
'options' => $this->get_options_styles(),
'desc_tip' => false,
),
array(
'title' => __( 'Shop Logo', 'woocommerce-delivery-notes' ),
'desc' => '',
'id' => 'wcdn_company_logo_image_id',
'css' => '',
'default' => '',
'type' => 'wcdn_image_select',
'desc_tip' => __( 'A shop logo representing your business. When the image is printed, its pixel density will automatically be eight times higher than the original. This means, 1 printed inch will correspond to about 288 pixels on the screen.', 'woocommerce-delivery-notes' )
),
array(
'title' => __( 'Shop Name', 'woocommerce-delivery-notes' ),
'desc' => '',
'id' => 'wcdn_custom_company_name',
'css' => 'min-width:100%;',
'default' => '',
'type' => 'text',
'desc_tip' => __( 'The shop name. Leave blank to use the default Website or Blog title defined in WordPress settings. The name will be ignored when a Logo is set.', 'woocommerce-delivery-notes' ),
),
array(
'title' => __( 'Shop Address', 'woocommerce-delivery-notes' ),
'desc' => __( 'The postal address of the shop or even e-mail or telephone.', 'woocommerce-delivery-notes' ),
'id' => 'wcdn_company_address',
'css' => 'min-width:100%;min-height:100px;',
'default' => '',
'type' => 'textarea',
'desc_tip' => true,
),
array(
'title' => __( 'Complimentary Close', 'woocommerce-delivery-notes' ),
'desc' => __( 'Add a personal close, notes or season greetings.', 'woocommerce-delivery-notes' ),
'id' => 'wcdn_personal_notes',
'css' => 'min-width:100%;min-height:100px;',
'default' => '',
'type' => 'textarea',
'desc_tip' => true,
),
array(
'title' => __( 'Policies', 'woocommerce-delivery-notes' ),
'desc' => __( 'Add the shop policies, conditions, etc.', 'woocommerce-delivery-notes' ),
'id' => 'wcdn_policies_conditions',
'css' => 'min-width:100%;min-height:100px;',
'default' => '',
'type' => 'textarea',
'desc_tip' => true,
),
array(
'title' => __( 'Footer', 'woocommerce-delivery-notes' ),
'desc' => __( 'Add a footer imprint, instructions, copyright notes, e-mail, telephone, etc.', 'woocommerce-delivery-notes' ),
'id' => 'wcdn_footer_imprint',
'css' => 'min-width:100%;min-height:100px;',
'default' => '',
'type' => 'textarea',
'desc_tip' => true,
),
array(
'type' => 'sectionend',
'id' => 'general_options'
),
array(
'title' => __( 'Pages & Buttons', 'woocommerce-delivery-notes' ),
'type' => 'title',
'desc' => '',
'id' => 'display_options'
),
array(
'title' => __( 'Print Page Endpoint', 'woocommerce-delivery-notes' ),
'desc' => '',
'id' => 'wcdn_print_order_page_endpoint',
'css' => '',
'default' => 'print-order',
'type' => 'text',
'desc_tip' => __( 'The endpoint is appended to the accounts page URL to print the order. It should be unique.', 'woocommerce-delivery-notes' ),
),
array(
'title' => __( 'Email', 'woocommerce-delivery-notes' ),
'desc' => __( 'Show print link in customer emails', 'woocommerce-delivery-notes' ),
'id' => 'wcdn_email_print_link',
'default' => 'no',
'type' => 'checkbox',
'desc_tip' => __( 'This includes the emails for a new, processing and completed order. On top of that the customer invoice email also includes the link.', 'woocommerce-delivery-notes' )
),
array(
'title' => __( 'My Account', 'woocommerce-delivery-notes' ),
'desc' => __( 'Show print button on the "View Order" page', 'woocommerce-delivery-notes' ),
'id' => 'wcdn_print_button_on_view_order_page',
'default' => 'no',
'type' => 'checkbox',
'checkboxgroup' => 'start'
),
array(
'desc' => __( 'Show print buttons on the "My Account" page', 'woocommerce-delivery-notes' ),
'id' => 'wcdn_print_button_on_my_account_page',
'default' => 'no',
'type' => 'checkbox',
'checkboxgroup' => 'end'
),
array(
'type' => 'sectionend',
'id' => 'display_options'
),
array(
'title' => __( 'Invoice', 'woocommerce-delivery-notes' ),
'type' => 'title',
'desc' => '',
'id' => 'invoice_options'
),
array(
'title' => __( 'Numbering', 'woocommerce-delivery-notes' ),
'desc' => __( 'Create invoice numbers', 'woocommerce-delivery-notes' ),
'id' => 'wcdn_create_invoice_number',
'default' => 'no',
'type' => 'checkbox',
'desc_tip' => ''
),
array(
'title' => __( 'Next Number', 'woocommerce-delivery-notes' ),
'desc' => '',
'id' => 'wcdn_invoice_number_count',
'class' => 'create-invoice',
'css' => '',
'default' => 1,
'type' => 'number',
'desc_tip' => __( 'The next invoice number.', 'woocommerce-delivery-notes' )
),
array(
'title' => __( 'Number Prefix', 'woocommerce-delivery-notes' ),
'desc' => '',
'id' => 'wcdn_invoice_number_prefix',
'class' => 'create-invoice',
'css' => '',
'default' => '',
'type' => 'text',
'desc_tip' => __( 'This text will be prepended to the invoice number.', 'woocommerce-delivery-notes' )
),
array(
'title' => __( 'Number Suffix', 'woocommerce-delivery-notes' ),
'desc' => '',
'id' => 'wcdn_invoice_number_suffix',
'class' => 'create-invoice',
'css' => '',
'default' => '',
'type' => 'text',
'desc_tip' => __( 'This text will be appended to the invoice number.', 'woocommerce-delivery-notes' )
),
array(
'type' => 'sectionend',
'id' => 'invoice_options'
),
)
);
return apply_filters( 'wcdn_get_settings', $settings, $section );
}
public function get_setting_position( $id, $settings, $type = null ) {
foreach( $settings as $key => $value ) {
if( isset( $value['id'] ) && $value['id'] == $id ) {
return $key;
}
}
return false;
}
public function generate_template_type_fields( $settings, $section = '' ) {
$position = $this->get_setting_position( 'wcdn_email_print_link', $settings );
if( $position !== false ) {
$new_settings = array();
$template_registrations = WooCommerce_Delivery_Notes_Print::$template_registrations;
array_splice( $template_registrations, 0, 1 );
$end = count( $template_registrations ) - 1;
foreach( $template_registrations as $index => $template_registration ) {
$title = '';
$desc_tip = '';
$checkboxgroup = '';
if( $index == 0 ) {
$title = __( 'Admin', 'woocommerce-delivery-notes' );
$checkboxgroup = 'start';
} else if( $index == $end ) {
$desc_tip = __( 'The print buttons are available on the order listing and on the order detail screen.', 'woocommerce-delivery-notes' );
$checkboxgroup = 'end';
}
$new_settings[] = array(
'title' => $title,
'desc' => $template_registration['labels']['setting'],
'id' => 'wcdn_template_type_' . $template_registration['type'],
'default' => 'no',
'type' => 'checkbox',
'checkboxgroup' => $checkboxgroup,
'desc_tip' => $desc_tip
);
}
$settings = $this->array_merge_at( $settings, $new_settings, $position );
}
return $settings;
}
public function get_template_description() {
$description = '';
$args = array(
'post_type' => 'shop_order',
'post_status' => array( 'wc-pending', 'wc-processing', 'wc-on-hold', 'wc-completed', 'wc-cancelled', 'wc-refunded', 'wc-failed' ),
'posts_per_page' => 1
);
$query = new WP_Query( $args );
if( $query->have_posts() ) {
$results = $query->get_posts();
$test_id = $results[0]->ID;
$invoice_url = wcdn_get_print_link( $test_id, 'invoice' );
$delivery_note_url = wcdn_get_print_link( $test_id, 'delivery-note' );
$receipt_url = wcdn_get_print_link( $test_id, 'receipt' );
$description = sprintf( __( 'This section lets you customise the content. You can preview the <a href="%1$s" target="%4$s" class="%5$s">invoice</a>, <a href="%2$s" target="%4$s" class="%5$s">delivery note</a> or <a href="%3$s" target="%4$s" class="%5$s">receipt</a> template.', 'woocommerce-delivery-notes' ), $invoice_url, $delivery_note_url, $receipt_url, '_blank', '' );
}
return $description;
}
public function get_options_styles() {
$options = array();
foreach( WooCommerce_Delivery_Notes_Print::$template_styles as $template_style ) {
if( is_array( $template_style ) && isset( $template_style['type'] ) && isset( $template_style['name'] ) ) {
$options[$template_style['type']] = $template_style['name'];
}
}
return $options;
}
public function load_image_ajax() {
if ( empty( $_REQUEST['nonce'] ) || ! wp_verify_nonce( $_REQUEST['nonce'], 'woocommerce-settings' ) ) {
die();
}
if( empty( $_POST['attachment_id'] ) && is_numeric( $_POST['attachment_id'] ) ) {
die();
}
$wdn_image_attachmnet_id = sanitize_text_field( $_POST['attachment_id'] );
$this->create_image( $wdn_image_attachmnet_id );
exit;
}
public function create_image( $attachment_id ) {
$attachment_src = wp_get_attachment_image_src( $attachment_id, 'medium', false );
$orientation = 'landscape';
if( ( $attachment_src[1] / $attachment_src[2] ) < 1 ) {
$orientation = 'portrait';
}
?>
<img src="<?php echo $attachment_src[0]; ?>" class="<?php echo $orientation; ?>" alt="" />
<?php
}
public function output_image_select( $value ) {
if ( ! isset( $value['title_select'] ) ) {
$value['title_select'] = __( 'Select', 'woocommerce-delivery-notes' );
}
if ( ! isset( $value['title_remove'] ) ) {
$value['title_remove'] = __( 'Remove', 'woocommerce-delivery-notes' );
}
$field = WC_Admin_Settings::get_field_description( $value );
$description = $field['description'];
$tooltip_html = $field['tooltip_html'];
$option_value = WC_Admin_Settings::get_option( $value['id'], $value['default'] );
$class_name = 'wcdn-image-select';
?><tr valign="top">
<th scope="row" class="titledesc">
<label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?> <?php echo $tooltip_html; ?></label>
</th>
<td class="forminp image_width_settings">
<input name="<?php echo esc_attr( $value['id'] ); ?>" id="<?php echo esc_attr( $value['id'] ); ?>" type="hidden" value="<?php echo esc_attr( $option_value ); ?>" class="<?php echo $class_name; ?>-image-id <?php echo esc_attr( $value['class'] ); ?>" />
<div id="<?php echo esc_attr( $value['id'] ); ?>_field" class="<?php echo $class_name; ?>-field <?php echo esc_attr( $value['class'] ); ?>" style="<?php echo esc_attr( $value['css'] ); ?>">
<span id="<?php echo esc_attr( $value['id'] ); ?>_spinner" class="<?php echo $class_name; ?>-spinner spinner"></span>
<div id="<?php echo esc_attr( $value['id'] ); ?>_attachment" class="<?php echo $class_name; ?>-attachment <?php echo esc_attr( $value['class'] ); ?> ">
<div class="thumbnail">
<div class="centered">
<?php if( !empty( $option_value ) ) : ?>
<?php $this->create_image( $option_value ); ?>
<?php endif; ?>
</div>
</div>
</div>
<div id="<?php echo esc_attr( $value['id'] ); ?>_buttons" class="<?php echo $class_name; ?>-buttons <?php echo esc_attr( $value['class'] ); ?>">
<a href="#" id="<?php echo esc_attr( $value['id'] ); ?>_remove_button" class="<?php echo $class_name; ?>-remove-button <?php if( empty( $option_value ) ) : ?>hidden<?php endif; ?> button">
<?php echo esc_html( $value['title_remove'] ); ?>
</a>
<a href="#" id="<?php echo esc_attr( $value['id'] ); ?>_add_button" class="<?php echo $class_name; ?>-add-button <?php if( !empty( $option_value ) ) : ?>hidden<?php endif; ?> button" data-uploader-title="<?php echo esc_attr( $value['title'] ); ?>" data-uploader-button-title="<?php echo esc_attr( $value['title_select'] ); ?>">
<?php echo esc_html( $value['title_select'] ); ?>
</a>
</div>
</div>
<?php echo $description; ?>
</td>
</tr><?php
}
public function array_merge_at( $array, $insert, $position ) {
$new_array = array();
if( $position == 0 ) {
$new_array = array_merge( $insert, $array );
} else {
if( $position >= ( count( $array ) - 1 ) ) {
$new_array = array_merge($array, $insert);
} else {
$head = array_slice( $array, 0, $position );
$tail = array_slice( $array, $position );
$new_array = array_merge( $head, $insert, $tail );
}
}
return $new_array;
}
}
}
?>