PluginProbe
Easy Invoice – Invoice Generator, PDF Quotes & Payments / 2.1.0
Easy Invoice – Invoice Generator, PDF Quotes & Payments v2.1.0
2.4.3 2.4.4 2.4.2 2.4.0 2.4.1 2.3.8 2.3.7 2.3.6 2.3.5 2.3.4 2.3.3 2.3.2 2.3.1 2.2.0 2.1.21 2.1.20 2.1.19 2.1.18 2.1.0 2.1.1 2.1.10 2.1.11 2.1.12 2.1.13 2.1.14 All 60 releases
easy-invoice / includes / Shortcodes / ShortcodeManager.php

ShortcodeManager.php in Easy Invoice – Invoice Generator, PDF Quotes & Payments 2.1.0, at includes/Shortcodes/ShortcodeManager.php

173 lines 6.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Shortcode Manager for Easy Invoice Free
4 *
5 * Manages shortcodes for invoice and quote URLs
6 *
7 * @package EasyInvoice
8 * @subpackage Shortcodes
9 * @since 2.0.0
10 */
11
12 namespace EasyInvoice\Shortcodes;
13
14 if (!defined('ABSPATH')) {
15 exit;
16 }
17
18 /**
19 * ShortcodeManager Class
20 */
21 class ShortcodeManager {
22
23 /**
24 * Constructor
25 */
26 public function __construct() {
27 // Register shortcodes
28 add_shortcode('easy_invoice_url', [$this, 'renderInvoiceUrl']);
29 add_shortcode('easy_quote_url', [$this, 'renderQuoteUrl']);
30
31 // Add shortcode info to help tab
32 add_action('admin_head', [$this, 'addShortcodeHelp']);
33 }
34
35 /**
36 * Render invoice URL shortcode
37 *
38 * @param array $atts Shortcode attributes
39 * @return string Rendered shortcode
40 */
41 public function renderInvoiceUrl($atts) {
42 $atts = shortcode_atts([
43 'id' => 0,
44 'number' => '',
45 'text' => '',
46 'class' => 'easy-invoice-url',
47 'target' => '_blank'
48 ], $atts, 'easy_invoice_url');
49
50 // Get invoice by ID or number
51 $invoice = null;
52 if (!empty($atts['id'])) {
53 $invoice = \EasyInvoice\Providers\InvoiceServiceProvider::getInvoiceRepository()->find($atts['id']);
54 } elseif (!empty($atts['number'])) {
55 $invoice = \EasyInvoice\Providers\InvoiceServiceProvider::getInvoiceRepository()->findByNumber($atts['number']);
56 }
57
58 if (!$invoice) {
59 return '<span class="easy-invoice-error">' . __('Invoice not found', 'easy-invoice') . '</span>';
60 }
61
62 $url = get_permalink($invoice->getId());
63 $text = !empty($atts['text']) ? $atts['text'] : $invoice->getNumber();
64 $class = esc_attr($atts['class']);
65 $target = esc_attr($atts['target']);
66
67 return sprintf(
68 '<a href="%s" class="%s" target="%s">%s</a>',
69 esc_url($url),
70 $class,
71 $target,
72 esc_html($text)
73 );
74 }
75
76 /**
77 * Render quote URL shortcode
78 *
79 * @param array $atts Shortcode attributes
80 * @return string Rendered shortcode
81 */
82 public function renderQuoteUrl($atts) {
83 $atts = shortcode_atts([
84 'id' => 0,
85 'number' => '',
86 'text' => '',
87 'class' => 'easy-quote-url',
88 'target' => '_blank'
89 ], $atts, 'easy_quote_url');
90
91 // Get quote by ID or number
92 $quote = null;
93 if (!empty($atts['id'])) {
94 $quote = \EasyInvoice\Providers\QuoteServiceProvider::getQuoteRepository()->find($atts['id']);
95 } elseif (!empty($atts['number'])) {
96 $quote = \EasyInvoice\Providers\QuoteServiceProvider::getQuoteRepository()->findByNumber($atts['number']);
97 }
98
99 if (!$quote) {
100 return '<span class="easy-invoice-error">' . __('Quote not found', 'easy-invoice') . '</span>';
101 }
102
103 $url = get_permalink($quote->getId());
104 $text = !empty($atts['text']) ? $atts['text'] : $quote->getNumber();
105 $class = esc_attr($atts['class']);
106 $target = esc_attr($atts['target']);
107
108 return sprintf(
109 '<a href="%s" class="%s" target="%s">%s</a>',
110 esc_url($url),
111 $class,
112 $target,
113 esc_html($text)
114 );
115 }
116
117 /**
118 * Add shortcode help to the help tab
119 */
120 public function addShortcodeHelp() {
121 $screen = get_current_screen();
122
123 if ($screen && ($screen->id === 'easy-invoice_page_easy-invoice-settings' || $screen->id === 'edit-easy_invoice')) {
124 $screen->add_help_tab([
125 'id' => 'easy-invoice-shortcodes',
126 'title' => __('Shortcodes', 'easy-invoice'),
127 'content' => $this->getShortcodeHelpContent()
128 ]);
129 }
130 }
131
132 /**
133 * Get shortcode help content
134 *
135 * @return string Help content
136 */
137 private function getShortcodeHelpContent() {
138 $content = '<h2>' . __('Available Shortcodes', 'easy-invoice') . '</h2>';
139
140 $content .= '<h3><code>[easy_invoice_url]</code></h3>';
141 $content .= '<p>' . __('Displays a link to an invoice.', 'easy-invoice') . '</p>';
142 $content .= '<h4>' . __('Attributes:', 'easy-invoice') . '</h4>';
143 $content .= '<ul>';
144 $content .= '<li><code>id</code> - ' . __('Invoice ID (required if number is not provided)', 'easy-invoice') . '</li>';
145 $content .= '<li><code>number</code> - ' . __('Invoice number (required if id is not provided)', 'easy-invoice') . '</li>';
146 $content .= '<li><code>text</code> - ' . __('Link text (default: invoice number)', 'easy-invoice') . '</li>';
147 $content .= '<li><code>class</code> - ' . __('CSS class for the link (default: easy-invoice-url)', 'easy-invoice') . '</li>';
148 $content .= '<li><code>target</code> - ' . __('Link target (default: _blank)', 'easy-invoice') . '</li>';
149 $content .= '</ul>';
150 $content .= '<h4>' . __('Examples:', 'easy-invoice') . '</h4>';
151 $content .= '<pre><code>[easy_invoice_url id="123" text="View Invoice"]</code></pre>';
152 $content .= '<pre><code>[easy_invoice_url number="INV-001" text="Click here to view"]</code></pre>';
153
154 $content .= '<hr>';
155
156 $content .= '<h3><code>[easy_quote_url]</code></h3>';
157 $content .= '<p>' . __('Displays a link to a quote.', 'easy-invoice') . '</p>';
158 $content .= '<h4>' . __('Attributes:', 'easy-invoice') . '</h4>';
159 $content .= '<ul>';
160 $content .= '<li><code>id</code> - ' . __('Quote ID (required if number is not provided)', 'easy-invoice') . '</li>';
161 $content .= '<li><code>number</code> - ' . __('Quote number (required if id is not provided)', 'easy-invoice') . '</li>';
162 $content .= '<li><code>text</code> - ' . __('Link text (default: quote number)', 'easy-invoice') . '</li>';
163 $content .= '<li><code>class</code> - ' . __('CSS class for the link (default: easy-quote-url)', 'easy-invoice') . '</li>';
164 $content .= '<li><code>target</code> - ' . __('Link target (default: _blank)', 'easy-invoice') . '</li>';
165 $content .= '</ul>';
166 $content .= '<h4>' . __('Examples:', 'easy-invoice') . '</h4>';
167 $content .= '<pre><code>[easy_quote_url id="456" text="View Quote"]</code></pre>';
168 $content .= '<pre><code>[easy_quote_url number="QT-001" text="Click here to view"]</code></pre>';
169
170 return $content;
171 }
172 }
173