VariableProductTitle
7 years ago
.gitkeep
7 years ago
WpaeInvalidPhpException.php
7 years ago
WpaeInvalidStringException.php
7 years ago
WpaeMethodNotFoundException.php
7 years ago
WpaePhpInterpreterErrorHandler.php
7 years ago
WpaeString.php
7 years ago
WpaeTooMuchRecursionException.php
7 years ago
WpaeXmlProcessor.php
7 years ago
XmlCsvExport.php
7 years ago
XmlExportACF.php
7 years ago
XmlExportComment.php
7 years ago
XmlExportCpt.php
7 years ago
XmlExportEngine.php
7 years ago
XmlExportFiltering.php
7 years ago
XmlExportMediaGallery.php
7 years ago
XmlExportTaxonomy.php
7 years ago
XmlExportUser.php
7 years ago
XmlExportWooCommerce.php
7 years ago
XmlExportWooCommerceCoupon.php
7 years ago
XmlExportWooCommerceOrder.php
7 years ago
XmlGoogleMerchants.php
7 years ago
XmlSpec.php
7 years ago
XmlGoogleMerchants.php
161 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Class XmlGoogleMerchants |
| 5 | */ |
| 6 | final class XmlGoogleMerchants |
| 7 | { |
| 8 | /** |
| 9 | * @var bool |
| 10 | */ |
| 11 | private $export_id = false; |
| 12 | /** |
| 13 | * @var array |
| 14 | */ |
| 15 | private $add_data = array(); |
| 16 | |
| 17 | /** |
| 18 | * @var array |
| 19 | * |
| 20 | * https://support.google.com/merchants/answer/160589?hl=en |
| 21 | * |
| 22 | */ |
| 23 | private $required_fields = array( |
| 24 | array( |
| 25 | 'name' => 'g:id', |
| 26 | 'type' => 'ID', |
| 27 | // 'label' => 'id' |
| 28 | ), |
| 29 | array( |
| 30 | 'name' => 'title', |
| 31 | 'type' => 'Title', |
| 32 | // 'label' => 'title' |
| 33 | ), |
| 34 | array( |
| 35 | 'name' => 'link', |
| 36 | 'type' => 'Permalink', |
| 37 | // 'label' => 'permalink' |
| 38 | ), |
| 39 | array( |
| 40 | 'name' => 'description', |
| 41 | 'type' => 'Content', |
| 42 | // 'label' => 'content' |
| 43 | ), |
| 44 | array( |
| 45 | 'name' => 'g:image_link', |
| 46 | 'type' => 'Image Url', |
| 47 | // 'options' => '{"is_export_featured":true,"is_export_attached":false,"image_separator":"|"}' |
| 48 | ), |
| 49 | array( |
| 50 | 'name' => 'g:price', |
| 51 | 'type' => 'Regular Price', |
| 52 | ), |
| 53 | // array( |
| 54 | // 'name' => 'g:condition', |
| 55 | // 'type' => 'woo', |
| 56 | // 'label' => '_regular_price' |
| 57 | // ) |
| 58 | ); |
| 59 | |
| 60 | /** |
| 61 | * XmlGoogleMerchants constructor. |
| 62 | * @param $id |
| 63 | * @param array $additional_data |
| 64 | */ |
| 65 | public function __construct($id, $additional_data = array()) |
| 66 | { |
| 67 | $this->export_id = $id; |
| 68 | $this->add_data = $additional_data; |
| 69 | |
| 70 | if ( ! empty($this->export_id)) |
| 71 | { |
| 72 | add_filter('wp_all_export_xml_header', array( &$this, 'wpae_xml_header'), 10, 2); |
| 73 | add_filter('wp_all_export_additional_data', array( &$this, 'wpae_additional_data'), 10, 3); |
| 74 | add_filter('wp_all_export_xml_footer', array( &$this, 'wpae_xml_footer'), 10, 2); |
| 75 | add_filter('wp_all_export_main_xml_tag', array( &$this, 'wpae_main_xml_tag'), 10, 2); |
| 76 | add_filter('wp_all_export_record_xml_tag', array( &$this, 'wpae_record_xml_tag'), 10, 2); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * @param $header |
| 82 | * @param $export_id |
| 83 | * @return string |
| 84 | */ |
| 85 | public function wpae_xml_header($header, $export_id) |
| 86 | { |
| 87 | if ( $export_id == $this->export_id ) |
| 88 | { |
| 89 | $header .= "\n<rss version=\"2.0\" xmlns:g=\"http://base.google.com/ns/1.0\">"; |
| 90 | } |
| 91 | return $header; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * @param $add_data |
| 96 | * @param $options |
| 97 | * @param $export_id |
| 98 | * @return array |
| 99 | */ |
| 100 | public function wpae_additional_data($add_data, $options, $export_id) |
| 101 | { |
| 102 | if ( $export_id == $this->export_id && ! empty($this->add_data)) |
| 103 | { |
| 104 | $add_data = array_merge($add_data, $this->add_data); |
| 105 | } |
| 106 | return $add_data; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * @param $footer |
| 111 | * @param $export_id |
| 112 | * @return string |
| 113 | */ |
| 114 | public function wpae_xml_footer($footer, $export_id) |
| 115 | { |
| 116 | if ( $export_id == $this->export_id ) |
| 117 | { |
| 118 | $footer = "</rss>"; |
| 119 | } |
| 120 | return $footer; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * @param $tag |
| 125 | * @param $export_id |
| 126 | * @return string |
| 127 | */ |
| 128 | public function wpae_main_xml_tag($tag, $export_id ) |
| 129 | { |
| 130 | return ( $export_id == $this->export_id ) ? 'channel' : $tag; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * @param $tag |
| 135 | * @param $export_id |
| 136 | * @return string |
| 137 | */ |
| 138 | public function wpae_record_xml_tag($tag, $export_id ) |
| 139 | { |
| 140 | return ( $export_id == $this->export_id ) ? 'item' : $tag; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * |
| 145 | */ |
| 146 | public function get_required_fields() |
| 147 | { |
| 148 | $xml_template = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<rss version=\"2.0\" xmlns:g=\"http://base.google.com/ns/1.0\">\n"; |
| 149 | $xml_template .= "\t<chanel>"; |
| 150 | $xml_template .= "\n\t\t<!-- BEGIN LOOP -->"; |
| 151 | $xml_template .= "\n\t\t<item>"; |
| 152 | foreach ($this->required_fields as $field){ |
| 153 | $xml_template .= "\n\t\t\t<" . $field['name'] . ">{" . $field['type'] . "}</" . $field['name'] . ">"; |
| 154 | } |
| 155 | $xml_template .= "\n\t\t</item>"; |
| 156 | $xml_template .= "\n\t\t<!-- END LOOP -->"; |
| 157 | $xml_template .= "\n\t</chanel>"; |
| 158 | $xml_template .= "\n</rss>"; |
| 159 | return $xml_template; |
| 160 | } |
| 161 | } |