├── .gitignore ├── README.md └── wpai-ssi-client-add-on.php /.gitignore: -------------------------------------------------------------------------------- 1 | _notes 2 | config.codekit 3 | .DS_Store 4 | *.LCK 5 | *.svn 6 | __assets/* -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # wpai-ssi-client-add-on 2 | Client Add On for WP All Import Starter Plugin 3 | 4 | This is a base plugin with standard features for extending WP All Import 5 | -------------------------------------------------------------------------------- /wpai-ssi-client-add-on.php: -------------------------------------------------------------------------------- 1 | $value) { 72 | $value = trim($value); 73 | if (trim($value) == '') { 74 | // no value, preserve count of values 75 | $new_value[] = $value; 76 | continue; 77 | } 78 | if (preg_match('#^(https?:)?//#is', $value)) { 79 | // alreadh contains domain part, keep it 80 | $new_value[] = $value; 81 | continue; 82 | } 83 | 84 | $location = 'http'; 85 | if (is_ssl()) { 86 | $location .= 's'; 87 | } 88 | $location .= '://'.$_SERVER['HTTP_HOST'].'/'.$folder_name.'/'.$value; 89 | $new_value[] = $location; 90 | 91 | } 92 | $value = implode(',', $new_value); 93 | return $value; 94 | } // end function image_import_loc 95 | 96 | new ssi_wpai_client_add_on(); 97 | 98 | class ssi_wpai_client_add_on { 99 | 100 | public function __construct() { 101 | add_action('init', array($this, 'init'), 20); 102 | add_action('pmxi_before_xml_import', array($this, 'before_import'), 10, 1); 103 | add_filter('wp_all_import_is_post_to_update', array($this, 'is_post_to_update'), 10, 3); 104 | add_filter('wp_all_import_is_post_to_create', array($this, 'is_post_to_create'), 10, 2); 105 | add_action('pmxi_update_post_meta', array($this, 'update_post_meta'), 10, 3); 106 | add_action('pmxi_attachment_uploaded', array($this, 'attachment_uploaded'), 10, 3); 107 | add_action('pmxi_gallery_image', array($this, 'update_attachment'), 10, 3); 108 | add_action('pmxi_saved_post', array($this, 'post_saved'), 10, 1); 109 | add_action('pmxi_after_xml_import', array($this, 'after_import'), 10, 1); 110 | } // end public function __construct 111 | 112 | public function init() { 113 | // hook 'init' 114 | // runs in init to do any setup that might be required 115 | } // end public function init 116 | 117 | public function before_import($import_id) { 118 | // hook pmxi_before_xml_import 119 | /* 120 | This hook is called before WP All Import starts the import process. 121 | It is generally used if you are performing cron based imports and 122 | you need to run some code before WP All Import starts the import process. 123 | */ 124 | 125 | } // end public function before_import 126 | 127 | public function is_post_to_create($nodes, $import_id) { 128 | // hook wp_all_import_is_post_to_create 129 | /* 130 | undocumented wp all import hook it works the same as is_post_to_update 131 | this filter gets a list of the nodes, columns of data to be imported 132 | and the import ID. This can be used to check to see if the data is valid 133 | if not valid you can return false to abort the insertion of the new post 134 | */ 135 | $valid = true; 136 | 137 | return $valid; 138 | } // end public function is_post_to_create 139 | 140 | public function is_post_to_update($post_id, $nodes, $import_id) { 141 | // hook wp_all_import_is_post_to_update 142 | /* 143 | This filter can be called to determine if a post should be updated or skipped. 144 | It takes the post ID and, optionally, an array of XML nodes. The returned 145 | value should be either true to update the post or false to skip it. 146 | 147 | *** IMPORTANT NOTE *** 148 | the people that built the import plugin do not know how to use filters properly. 149 | They don't send the default value when calling the filter, only the other parameters. 150 | This filter must return either true or false and cannot simply return the default 151 | value as with a correctly constructed filter. 152 | 153 | I need to investigate what is in nodes and exaclty when this is called 154 | since this will be the primary function that will determine if input is 155 | valid and skip any invalid rows I'm going to have to test the import and write these values to a 156 | file so I can see what's in the arguments 157 | 158 | */ 159 | 160 | $valid = true; 161 | 162 | return $valid; 163 | } // end public function is_post_to_update 164 | 165 | public function update_post_meta($post_id, $meta_key, $meta_value) { 166 | // hook pmxi_update_post_meta 167 | /* 168 | This hook is called after WP All Import creates/updates a post meta. 169 | */ 170 | } // end public function update_post_meta 171 | 172 | public function attachment_uploaded($post_id, $attachment_id, $file_path) { 173 | // hook pmxi_attachment_uploaded 174 | /* 175 | This hook is called after WP All Import creates/updates a post attachment file 176 | */ 177 | } // end public function attachment_uploaded 178 | 179 | public function update_attachment($post_id, $attachment_id, $file_path) { 180 | //hook pmxi_gallery_image 181 | /* 182 | This hook is called after WP All Import creates/updates a post attachment image 183 | */ 184 | } // end public function update_attachment 185 | 186 | public function post_saved($post_id) { 187 | // hook pmxi_saved_post 188 | /* 189 | This hook is called after WP All Import creates or updates a post. 190 | It is generally used if you need to perform further actions on the 191 | imported data, like serialize it, use an API for geocoding coordinates, 192 | use it for some other purpose like comment generation, etc 193 | */ 194 | if (function_exists('FWP')) { 195 | // if facetwp is installed, do indexing 196 | FWP()->indexer->index($post_id); 197 | } 198 | 199 | if (class_exists('acf')) { 200 | $this->acf_update_fields($post_id); 201 | } 202 | 203 | } // end public function post_saved 204 | 205 | public function after_import($import_id) { 206 | // hook pmxi_after_xml_import 207 | /* 208 | This hook is called after WP All Import finishes an import. 209 | It is generally used if you are performing cron based imports and 210 | you need to do some clean up after the import is complete. 211 | */ 212 | 213 | if (function_exists('FWP')) { 214 | // if facet wp is installed, run the indexer on this page 215 | FWP()->indexer->index(); 216 | } 217 | 218 | } // end public function after_import 219 | 220 | private function acf_update_fields($post_id, $fields=false) { 221 | // this function will update any checkbox, radio or select fields imported 222 | // to new add choice values to these fields when new values are present in the import 223 | // this currently only works for top level fields 224 | // plan to make it work in repeaters, flex fields, etc, later 225 | 226 | // much of this is copied directly from the same process in ACF that allows 227 | // addning new field choices 228 | if ($fields === false) { 229 | $fields = get_field_objects($post_id); 230 | } 231 | foreach ($fields as $field) { 232 | if (!in_array($field['type'], array('select', 'checkbox', 'radio'))) { 233 | // skip field 234 | continue; 235 | } 236 | $value = get_field($field['key'], $post_id, false); 237 | if (empty($value)) { 238 | // no value, skip field 239 | continue; 240 | } 241 | if (!is_array($value)) { 242 | $value = array($value); 243 | } 244 | $selector = $field['ID'] ? $field['ID'] : $field['key']; 245 | $field = acf_get_field($selector, true); 246 | if (!$field['ID']) { 247 | // could not get field 248 | continue; 249 | } 250 | $update = false; 251 | foreach ($value as $v) { 252 | // unslash (fixes serialize single quote issue) 253 | $v = wp_unslash($v); 254 | // sanitize (remove tags) 255 | $v = sanitize_text_field($v); 256 | if (isset($field['choices'][$v])) { 257 | // value already exists 258 | continue; 259 | } 260 | $update = true; 261 | // append 262 | $field['choices'][$v] = $v; 263 | } // end foreac value 264 | if ($update) { 265 | // added values 266 | acf_update_field($field); 267 | } 268 | } // end foreach field 269 | 270 | } // end private function acf_update_fields 271 | 272 | private function write_to_file($value, $comment='') { 273 | // this function for testing & debuggin only 274 | $file = dirname(__FILE__).'/-data-'.date('Y-m-d-h-i').'.txt'; 275 | $handle = fopen($file, 'a'); 276 | ob_start(); 277 | if ($comment) { 278 | echo $comment.":\r\n"; 279 | } 280 | if (is_array($value) || is_object($value)) { 281 | print_r($value); 282 | } elseif (is_bool($value)) { 283 | var_dump($value); 284 | } else { 285 | echo $value; 286 | } 287 | echo "\r\n\r\n"; 288 | fwrite($handle, ob_get_clean()); 289 | fclose($handle); 290 | } // end private function write_to_file 291 | 292 | } // end class ssi_wpai_client_add_on 293 | 294 | ?> --------------------------------------------------------------------------------