| 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 |
// Only use secure link if enabled in settings and available |
| 64 |
$secure_links_enabled = get_option('easy_invoice_pro_enable_secure_links', 'no') === 'yes'; |
| 65 |
if ($secure_links_enabled && class_exists('\EasyInvoicePro\Controllers\PermalinkController')) { |
| 66 |
$secure_url = \EasyInvoicePro\Controllers\PermalinkController::getInvoiceSecureLinkUrl($invoice->getId()); |
| 67 |
if ($secure_url) { |
| 68 |
$url = $secure_url; |
| 69 |
} |
| 70 |
} |
| 71 |
$text = !empty($atts['text']) ? $atts['text'] : $invoice->getNumber(); |
| 72 |
$class = esc_attr($atts['class']); |
| 73 |
$target = esc_attr($atts['target']); |
| 74 |
|
| 75 |
return sprintf( |
| 76 |
'<a href="%s" class="%s" target="%s">%s</a>', |
| 77 |
esc_url($url), |
| 78 |
$class, |
| 79 |
$target, |
| 80 |
esc_html($text) |
| 81 |
); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Render quote URL shortcode |
| 86 |
* |
| 87 |
* @param array $atts Shortcode attributes |
| 88 |
* @return string Rendered shortcode |
| 89 |
*/ |
| 90 |
public function renderQuoteUrl($atts) { |
| 91 |
$atts = shortcode_atts([ |
| 92 |
'id' => 0, |
| 93 |
'number' => '', |
| 94 |
'text' => '', |
| 95 |
'class' => 'easy-quote-url', |
| 96 |
'target' => '_blank' |
| 97 |
], $atts, 'easy_quote_url'); |
| 98 |
|
| 99 |
// Get quote by ID or number |
| 100 |
$quote = null; |
| 101 |
if (!empty($atts['id'])) { |
| 102 |
$quote = \EasyInvoice\Providers\QuoteServiceProvider::getQuoteRepository()->find($atts['id']); |
| 103 |
} elseif (!empty($atts['number'])) { |
| 104 |
$quote = \EasyInvoice\Providers\QuoteServiceProvider::getQuoteRepository()->findByNumber($atts['number']); |
| 105 |
} |
| 106 |
|
| 107 |
if (!$quote) { |
| 108 |
return '<span class="easy-invoice-error">' . __('Quote not found', 'easy-invoice') . '</span>'; |
| 109 |
} |
| 110 |
|
| 111 |
$url = get_permalink($quote->getId()); |
| 112 |
$secure_links_enabled = get_option('easy_invoice_pro_enable_secure_links', 'no') === 'yes'; |
| 113 |
if ($secure_links_enabled && class_exists('\EasyInvoicePro\Controllers\PermalinkController')) { |
| 114 |
$secure_url = \EasyInvoicePro\Controllers\PermalinkController::getQuoteSecureLinkUrl($quote->getId()); |
| 115 |
if ($secure_url) { |
| 116 |
$url = $secure_url; |
| 117 |
} |
| 118 |
} |
| 119 |
$text = !empty($atts['text']) ? $atts['text'] : $quote->getNumber(); |
| 120 |
$class = esc_attr($atts['class']); |
| 121 |
$target = esc_attr($atts['target']); |
| 122 |
|
| 123 |
return sprintf( |
| 124 |
'<a href="%s" class="%s" target="%s">%s</a>', |
| 125 |
esc_url($url), |
| 126 |
$class, |
| 127 |
$target, |
| 128 |
esc_html($text) |
| 129 |
); |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Add shortcode help to the help tab |
| 134 |
*/ |
| 135 |
public function addShortcodeHelp() { |
| 136 |
$screen = get_current_screen(); |
| 137 |
|
| 138 |
if ($screen && ($screen->id === 'easy-invoice_page_easy-invoice-settings' || $screen->id === 'edit-easy_invoice')) { |
| 139 |
$screen->add_help_tab([ |
| 140 |
'id' => 'easy-invoice-shortcodes', |
| 141 |
'title' => __('Shortcodes', 'easy-invoice'), |
| 142 |
'content' => $this->getShortcodeHelpContent() |
| 143 |
]); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Get shortcode help content |
| 149 |
* |
| 150 |
* @return string Help content |
| 151 |
*/ |
| 152 |
private function getShortcodeHelpContent() { |
| 153 |
$content = '<h2>' . __('Available Shortcodes', 'easy-invoice') . '</h2>'; |
| 154 |
|
| 155 |
$content .= '<h3><code>[easy_invoice_url]</code></h3>'; |
| 156 |
$content .= '<p>' . __('Displays a link to an invoice.', 'easy-invoice') . '</p>'; |
| 157 |
$content .= '<h4>' . __('Attributes:', 'easy-invoice') . '</h4>'; |
| 158 |
$content .= '<ul>'; |
| 159 |
$content .= '<li><code>id</code> - ' . __('Invoice ID (required if number is not provided)', 'easy-invoice') . '</li>'; |
| 160 |
$content .= '<li><code>number</code> - ' . __('Invoice number (required if id is not provided)', 'easy-invoice') . '</li>'; |
| 161 |
$content .= '<li><code>text</code> - ' . __('Link text (default: invoice number)', 'easy-invoice') . '</li>'; |
| 162 |
$content .= '<li><code>class</code> - ' . __('CSS class for the link (default: easy-invoice-url)', 'easy-invoice') . '</li>'; |
| 163 |
$content .= '<li><code>target</code> - ' . __('Link target (default: _blank)', 'easy-invoice') . '</li>'; |
| 164 |
$content .= '</ul>'; |
| 165 |
$content .= '<h4>' . __('Examples:', 'easy-invoice') . '</h4>'; |
| 166 |
$content .= '<pre><code>[easy_invoice_url id="123" text="View Invoice"]</code></pre>'; |
| 167 |
$content .= '<pre><code>[easy_invoice_url number="INV-001" text="Click here to view"]</code></pre>'; |
| 168 |
|
| 169 |
$content .= '<hr>'; |
| 170 |
|
| 171 |
$content .= '<h3><code>[easy_quote_url]</code></h3>'; |
| 172 |
$content .= '<p>' . __('Displays a link to a quote.', 'easy-invoice') . '</p>'; |
| 173 |
$content .= '<h4>' . __('Attributes:', 'easy-invoice') . '</h4>'; |
| 174 |
$content .= '<ul>'; |
| 175 |
$content .= '<li><code>id</code> - ' . __('Quote ID (required if number is not provided)', 'easy-invoice') . '</li>'; |
| 176 |
$content .= '<li><code>number</code> - ' . __('Quote number (required if id is not provided)', 'easy-invoice') . '</li>'; |
| 177 |
$content .= '<li><code>text</code> - ' . __('Link text (default: quote number)', 'easy-invoice') . '</li>'; |
| 178 |
$content .= '<li><code>class</code> - ' . __('CSS class for the link (default: easy-quote-url)', 'easy-invoice') . '</li>'; |
| 179 |
$content .= '<li><code>target</code> - ' . __('Link target (default: _blank)', 'easy-invoice') . '</li>'; |
| 180 |
$content .= '</ul>'; |
| 181 |
$content .= '<h4>' . __('Examples:', 'easy-invoice') . '</h4>'; |
| 182 |
$content .= '<pre><code>[easy_quote_url id="456" text="View Quote"]</code></pre>'; |
| 183 |
$content .= '<pre><code>[easy_quote_url number="QT-001" text="Click here to view"]</code></pre>'; |
| 184 |
|
| 185 |
return $content; |
| 186 |
} |
| 187 |
} |
| 188 |
|