Plugin Directory

Changeset 3451046


Ignore:
Timestamp:
01/31/2026 03:03:59 PM (2 months ago)
Author:
ajitdas
Message:

devs accounting updated

Location:
devs-accounting
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • devs-accounting/tags/1.1.9/devs-accounting.php

    r3418918 r3451046  
    1414
    1515if (!defined('ABSPATH'))
    16     exit; // Exit if accessed directly
     16    exit;
    1717
    1818/**
     
    4444        add_action('add_meta_boxes', [$this, 'register_item_metabox']);
    4545        add_action('save_post_items', [$this, 'save_item_meta']);
    46         add_action('save_post', [$this, 'handle_post_update'], 20, 3); 
     46        add_action('save_post', [$this, 'handle_post_update'], 20, 3);
    4747    }
    4848
     
    8686        ?>
    8787
    88         <div style="margin-bottom: 20px;">
    89             <p><strong>Item Enabled</strong></p>
     88        <div style="margin-bottom: 10px; display: flex; justify-content: space-between; align-items: center;">
     89            <label>
     90                <Strong>Status: </Strong>
     91            </label>
    9092            <label class="switch">
    9193                <input type="hidden" name="item_enabled" value="0">
     
    9395                <span class="slider"></span>
    9496            </label>
    95             <span style="margin-left: 10px; font-weight: bold;" id="toggle-status">
    96                 <?php echo $enabled ? 'ON' : 'OFF'; ?>
    97             </span>
     97
    9898        </div>
    9999
     
    136136            </p>
    137137        </div>
     138
    138139        <?php
    139     }
    140    
     140        $selected_taxes = [];
     141
     142        if ($item) {
     143            $selected_taxes = array_map('intval', $wpdb->get_col(
     144                $wpdb->prepare(
     145                    "SELECT tax_id FROM {$wpdb->prefix}dac_item_tax WHERE item_id = %d",
     146                    $item->id
     147                )
     148            ));
     149
     150        }
     151        $taxes = $wpdb->get_results(
     152            "SELECT id, tax_name FROM {$wpdb->prefix}dac_taxes ORDER BY tax_name ASC"
     153        );
     154        ?>
     155
     156        <p>
     157    <label><strong>Tax:</strong></label>
     158        <?php foreach ($taxes as $tax): ?>
     159            <label style="display:block; margin-bottom:4px;">
     160                <input type="checkbox"
     161                       name="item_tax[]"
     162                       value="<?php echo esc_attr($tax->id); ?>"
     163                    <?php checked(in_array((int) $tax->id, $selected_taxes, true)); ?>
     164                >
     165                <?php echo esc_html($tax->tax_name); ?>
     166            </label>
     167        <?php endforeach; ?>
     168   
     169</p>
     170
     171
     172        <?php
     173        $existing_meta = [];
     174        if ($item && !empty($item->meta)) {
     175            $decoded = json_decode($item->meta, true);
     176            if (is_array($decoded)) {
     177                $existing_meta = $decoded;
     178            }
     179        }
     180        ?>
     181
     182        <p><strong>Meta:</strong></p>
     183        <div id="item-meta-wrapper">
     184            <?php if (!empty($existing_meta)): ?>
     185                <?php foreach ($existing_meta as $meta): ?>
     186                    <div class="item-meta-row" style="display:flex; gap:8px; margin-bottom:6px; align-items:center;">
     187                        <input type="text" name="item_meta_name[]" value="<?php echo esc_attr($meta['meta_name']); ?>"
     188                            placeholder="Meta Name" class="widefat">
     189                        <input type="text" name="item_meta_value[]" value="<?php echo esc_attr($meta['meta_value']); ?>"
     190                            placeholder="Meta Value" class="widefat">
     191                        <button type="button" class="button remove-meta" onclick="removeItemMetaRow(this)">X</button>
     192                    </div>
     193                <?php endforeach; ?>
     194            <?php else: ?>
     195                <div class="item-meta-row" style="display:flex; gap:8px; margin-bottom:6px; align-items:center;">
     196                    <input type="text" name="item_meta_name[]" placeholder="Meta Name" class="widefat">
     197                    <input type="text" name="item_meta_value[]" placeholder="Meta Value" class="widefat">
     198                    <button type="button" class="button remove-meta" onclick="removeItemMetaRow(this)">X</button>
     199                </div>
     200            <?php endif; ?>
     201        </div>
     202
     203        <button type="button" class="button" onclick="addItemMetaRow()">+ Add Meta</button>
     204
     205        <script>
     206            function addItemMetaRow() {
     207                const wrapper = document.getElementById('item-meta-wrapper');
     208                const div = document.createElement('div');
     209                div.className = 'item-meta-row';
     210                div.style.display = 'flex';
     211                div.style.gap = '8px';
     212                div.style.marginBottom = '6px';
     213                div.style.alignItems = 'center';
     214
     215                div.innerHTML = `
     216        <input type="text" name="item_meta_name[]" placeholder="Meta Name" class="widefat">
     217        <input type="text" name="item_meta_value[]" placeholder="Meta Value" class="widefat">
     218        <button type="button" class="button remove-meta" onclick="removeItemMetaRow(this)">X</button>
     219    `;
     220                wrapper.appendChild(div);
     221            }
     222
     223            function removeItemMetaRow(button) {
     224                const row = button.parentNode;
     225                row.parentNode.removeChild(row);
     226            }
     227        </script>
     228
     229
     230
     231
     232        <?php
     233    }
     234
    141235    public function save_item_meta($post_id)
    142236    {
     
    178272        ];
    179273
     274        $meta_data = [];
     275
     276        if (!empty($_POST['item_meta_name']) && !empty($_POST['item_meta_value'])) {
     277
     278            $names = (array) $_POST['item_meta_name'];
     279            $values = (array) $_POST['item_meta_value'];
     280
     281            foreach ($names as $index => $name) {
     282
     283                $name = sanitize_text_field($name);
     284                $value = sanitize_text_field($values[$index] ?? '');
     285
     286                if ($name !== '' && $value !== '') {
     287                    $meta_data[] = [
     288                        'meta_name' => $name,
     289                        'meta_value' => $value,
     290                    ];
     291                }
     292            }
     293        }
     294
     295        $data['meta'] = !empty($meta_data) ? wp_json_encode($meta_data) : null;
     296
     297
    180298        $this->save_to_dac_items_only($post_id, $data);
    181     }
     299        $tax_id = isset($_POST['item_tax']) ? intval($_POST['item_tax']) : 0;
     300
     301        $tax_ids = [];
     302
     303        if (!empty($_POST['item_tax']) && is_array($_POST['item_tax'])) {
     304            $tax_ids = array_map('intval', $_POST['item_tax']);
     305        }
     306
     307        $this->save_item_taxes($post_id, $tax_ids);
     308
     309
     310
     311    }
     312
     313    private function save_item_taxes($post_id, $tax_ids)
     314    {
     315        global $wpdb;
     316
     317        $item_id = $wpdb->get_var(
     318            $wpdb->prepare(
     319                "SELECT id FROM {$wpdb->prefix}dac_items WHERE post_id = %d",
     320                $post_id
     321            )
     322        );
     323
     324        if (!$item_id)
     325            return;
     326
     327        $table = $wpdb->prefix . 'dac_item_tax';
     328
     329        $wpdb->delete($table, ['item_id' => $item_id]);
     330
     331        foreach ($tax_ids as $tax_id) {
     332            $wpdb->insert($table, [
     333                'item_id' => $item_id,
     334                'tax_id' => $tax_id,
     335            ], ['%d', '%d']);
     336        }
     337    }
     338
    182339
    183340    public function save_to_dac_items_only($post_id, $data)
     
    431588    function devsaccounting_load_scripts($hook)
    432589    {
    433         wp_enqueue_script(
    434             'devs-accounting-feedback-js',
    435             plugin_dir_url(__FILE__) . 'build/feedback.js',
    436             ['wp-element'],
    437             wp_rand(),
    438             true
    439         );
    440         wp_enqueue_style(
    441             'devs-accounting-style',
    442             plugin_dir_url(__FILE__) . 'build/index.css'
    443         );
    444 
     590   
     591        $plugin_data = get_file_data(
     592        __FILE__,
     593        array('Version' => 'Version'),
     594        'plugin'
     595        );
     596
     597        $asset_version = $plugin_data['Version'] ?? false;
     598           
     599   
    445600        if (isset($_GET['page']) && strpos($_GET['page'], 'devs-accounting') === 0) {
    446601            wp_enqueue_script(
     
    448603                plugin_dir_url(__FILE__) . 'build/admin.js',
    449604                ['wp-element'],
    450                 wp_rand(),
     605                $asset_version,
    451606                true
    452607            );
     
    456611            ]);
    457612
     613            wp_enqueue_script(
     614                'devs-accounting-feedback-js',
     615                plugin_dir_url(__FILE__) . 'build/feedback.js',
     616                ['wp-element'],
     617                $asset_version,
     618                true
     619            );
     620
    458621            wp_enqueue_style(
    459622                'devs-accounting-style',
     
    469632    public function checkShortcodesAndEnqueueScript($content)
    470633    {
     634        $plugin_data = get_file_data(
     635        __FILE__,
     636        array('Version' => 'Version'),
     637        'plugin'
     638        );
     639
     640        $asset_version = $plugin_data['Version'] ?? false;
     641       
    471642        $shortcodes = [
    472643            'devsaccounting_invoice',
     
    484655                    DEVSACCOUNTING_URL . 'build/frontend.js',
    485656                    ['wp-element'],
    486                     wp_rand(),
     657                    $asset_version,
    487658                    true
    488659                );
  • devs-accounting/trunk/devs-accounting.php

    r3418918 r3451046  
    1414
    1515if (!defined('ABSPATH'))
    16     exit; // Exit if accessed directly
     16    exit;
    1717
    1818/**
     
    4444        add_action('add_meta_boxes', [$this, 'register_item_metabox']);
    4545        add_action('save_post_items', [$this, 'save_item_meta']);
    46         add_action('save_post', [$this, 'handle_post_update'], 20, 3); 
     46        add_action('save_post', [$this, 'handle_post_update'], 20, 3);
    4747    }
    4848
     
    8686        ?>
    8787
    88         <div style="margin-bottom: 20px;">
    89             <p><strong>Item Enabled</strong></p>
     88        <div style="margin-bottom: 10px; display: flex; justify-content: space-between; align-items: center;">
     89            <label>
     90                <Strong>Status: </Strong>
     91            </label>
    9092            <label class="switch">
    9193                <input type="hidden" name="item_enabled" value="0">
     
    9395                <span class="slider"></span>
    9496            </label>
    95             <span style="margin-left: 10px; font-weight: bold;" id="toggle-status">
    96                 <?php echo $enabled ? 'ON' : 'OFF'; ?>
    97             </span>
     97
    9898        </div>
    9999
     
    136136            </p>
    137137        </div>
     138
    138139        <?php
    139     }
    140    
     140        $selected_taxes = [];
     141
     142        if ($item) {
     143            $selected_taxes = array_map('intval', $wpdb->get_col(
     144                $wpdb->prepare(
     145                    "SELECT tax_id FROM {$wpdb->prefix}dac_item_tax WHERE item_id = %d",
     146                    $item->id
     147                )
     148            ));
     149
     150        }
     151        $taxes = $wpdb->get_results(
     152            "SELECT id, tax_name FROM {$wpdb->prefix}dac_taxes ORDER BY tax_name ASC"
     153        );
     154        ?>
     155
     156        <p>
     157    <label><strong>Tax:</strong></label>
     158        <?php foreach ($taxes as $tax): ?>
     159            <label style="display:block; margin-bottom:4px;">
     160                <input type="checkbox"
     161                       name="item_tax[]"
     162                       value="<?php echo esc_attr($tax->id); ?>"
     163                    <?php checked(in_array((int) $tax->id, $selected_taxes, true)); ?>
     164                >
     165                <?php echo esc_html($tax->tax_name); ?>
     166            </label>
     167        <?php endforeach; ?>
     168   
     169</p>
     170
     171
     172        <?php
     173        $existing_meta = [];
     174        if ($item && !empty($item->meta)) {
     175            $decoded = json_decode($item->meta, true);
     176            if (is_array($decoded)) {
     177                $existing_meta = $decoded;
     178            }
     179        }
     180        ?>
     181
     182        <p><strong>Meta:</strong></p>
     183        <div id="item-meta-wrapper">
     184            <?php if (!empty($existing_meta)): ?>
     185                <?php foreach ($existing_meta as $meta): ?>
     186                    <div class="item-meta-row" style="display:flex; gap:8px; margin-bottom:6px; align-items:center;">
     187                        <input type="text" name="item_meta_name[]" value="<?php echo esc_attr($meta['meta_name']); ?>"
     188                            placeholder="Meta Name" class="widefat">
     189                        <input type="text" name="item_meta_value[]" value="<?php echo esc_attr($meta['meta_value']); ?>"
     190                            placeholder="Meta Value" class="widefat">
     191                        <button type="button" class="button remove-meta" onclick="removeItemMetaRow(this)">X</button>
     192                    </div>
     193                <?php endforeach; ?>
     194            <?php else: ?>
     195                <div class="item-meta-row" style="display:flex; gap:8px; margin-bottom:6px; align-items:center;">
     196                    <input type="text" name="item_meta_name[]" placeholder="Meta Name" class="widefat">
     197                    <input type="text" name="item_meta_value[]" placeholder="Meta Value" class="widefat">
     198                    <button type="button" class="button remove-meta" onclick="removeItemMetaRow(this)">X</button>
     199                </div>
     200            <?php endif; ?>
     201        </div>
     202
     203        <button type="button" class="button" onclick="addItemMetaRow()">+ Add Meta</button>
     204
     205        <script>
     206            function addItemMetaRow() {
     207                const wrapper = document.getElementById('item-meta-wrapper');
     208                const div = document.createElement('div');
     209                div.className = 'item-meta-row';
     210                div.style.display = 'flex';
     211                div.style.gap = '8px';
     212                div.style.marginBottom = '6px';
     213                div.style.alignItems = 'center';
     214
     215                div.innerHTML = `
     216        <input type="text" name="item_meta_name[]" placeholder="Meta Name" class="widefat">
     217        <input type="text" name="item_meta_value[]" placeholder="Meta Value" class="widefat">
     218        <button type="button" class="button remove-meta" onclick="removeItemMetaRow(this)">X</button>
     219    `;
     220                wrapper.appendChild(div);
     221            }
     222
     223            function removeItemMetaRow(button) {
     224                const row = button.parentNode;
     225                row.parentNode.removeChild(row);
     226            }
     227        </script>
     228
     229
     230
     231
     232        <?php
     233    }
     234
    141235    public function save_item_meta($post_id)
    142236    {
     
    178272        ];
    179273
     274        $meta_data = [];
     275
     276        if (!empty($_POST['item_meta_name']) && !empty($_POST['item_meta_value'])) {
     277
     278            $names = (array) $_POST['item_meta_name'];
     279            $values = (array) $_POST['item_meta_value'];
     280
     281            foreach ($names as $index => $name) {
     282
     283                $name = sanitize_text_field($name);
     284                $value = sanitize_text_field($values[$index] ?? '');
     285
     286                if ($name !== '' && $value !== '') {
     287                    $meta_data[] = [
     288                        'meta_name' => $name,
     289                        'meta_value' => $value,
     290                    ];
     291                }
     292            }
     293        }
     294
     295        $data['meta'] = !empty($meta_data) ? wp_json_encode($meta_data) : null;
     296
     297
    180298        $this->save_to_dac_items_only($post_id, $data);
    181     }
     299        $tax_id = isset($_POST['item_tax']) ? intval($_POST['item_tax']) : 0;
     300
     301        $tax_ids = [];
     302
     303        if (!empty($_POST['item_tax']) && is_array($_POST['item_tax'])) {
     304            $tax_ids = array_map('intval', $_POST['item_tax']);
     305        }
     306
     307        $this->save_item_taxes($post_id, $tax_ids);
     308
     309
     310
     311    }
     312
     313    private function save_item_taxes($post_id, $tax_ids)
     314    {
     315        global $wpdb;
     316
     317        $item_id = $wpdb->get_var(
     318            $wpdb->prepare(
     319                "SELECT id FROM {$wpdb->prefix}dac_items WHERE post_id = %d",
     320                $post_id
     321            )
     322        );
     323
     324        if (!$item_id)
     325            return;
     326
     327        $table = $wpdb->prefix . 'dac_item_tax';
     328
     329        $wpdb->delete($table, ['item_id' => $item_id]);
     330
     331        foreach ($tax_ids as $tax_id) {
     332            $wpdb->insert($table, [
     333                'item_id' => $item_id,
     334                'tax_id' => $tax_id,
     335            ], ['%d', '%d']);
     336        }
     337    }
     338
    182339
    183340    public function save_to_dac_items_only($post_id, $data)
     
    431588    function devsaccounting_load_scripts($hook)
    432589    {
    433         wp_enqueue_script(
    434             'devs-accounting-feedback-js',
    435             plugin_dir_url(__FILE__) . 'build/feedback.js',
    436             ['wp-element'],
    437             wp_rand(),
    438             true
    439         );
    440         wp_enqueue_style(
    441             'devs-accounting-style',
    442             plugin_dir_url(__FILE__) . 'build/index.css'
    443         );
    444 
     590   
     591        $plugin_data = get_file_data(
     592        __FILE__,
     593        array('Version' => 'Version'),
     594        'plugin'
     595        );
     596
     597        $asset_version = $plugin_data['Version'] ?? false;
     598           
     599   
    445600        if (isset($_GET['page']) && strpos($_GET['page'], 'devs-accounting') === 0) {
    446601            wp_enqueue_script(
     
    448603                plugin_dir_url(__FILE__) . 'build/admin.js',
    449604                ['wp-element'],
    450                 wp_rand(),
     605                $asset_version,
    451606                true
    452607            );
     
    456611            ]);
    457612
     613            wp_enqueue_script(
     614                'devs-accounting-feedback-js',
     615                plugin_dir_url(__FILE__) . 'build/feedback.js',
     616                ['wp-element'],
     617                $asset_version,
     618                true
     619            );
     620
    458621            wp_enqueue_style(
    459622                'devs-accounting-style',
     
    469632    public function checkShortcodesAndEnqueueScript($content)
    470633    {
     634        $plugin_data = get_file_data(
     635        __FILE__,
     636        array('Version' => 'Version'),
     637        'plugin'
     638        );
     639
     640        $asset_version = $plugin_data['Version'] ?? false;
     641       
    471642        $shortcodes = [
    472643            'devsaccounting_invoice',
     
    484655                    DEVSACCOUNTING_URL . 'build/frontend.js',
    485656                    ['wp-element'],
    486                     wp_rand(),
     657                    $asset_version,
    487658                    true
    488659                );
Note: See TracChangeset for help on using the changeset viewer.