PageRenderTime 53ms CodeModel.GetById 6ms RepoModel.GetById 0ms app.codeStats 0ms

/settings.php

https://github.com/wp-plugins/2performant-product-importer
PHP | 377 lines | 341 code | 28 blank | 8 comment | 21 complexity | 80f4997338cff164ae8b0bff6d6a1328 MD5 | raw file
  1. <?php
  2. if ( is_admin() ) :
  3. function tp_register_settings() {
  4. $settings = array(
  5. 'connection' => array(
  6. 'label' => 'Connection settings',
  7. 'settings' => array(
  8. 'network' => array(
  9. 'type' => 'text',
  10. 'label' => 'Network API URL',
  11. 'description' => 'E.g. <code>http://api.network.com</code>'
  12. ),
  13. 'username' => array(
  14. 'type' => 'text',
  15. 'label' => 'Username'
  16. ),
  17. 'password' => array(
  18. 'type' => 'password',
  19. 'label' => 'Password'
  20. )
  21. )
  22. ),
  23. 'add_feed' => array(
  24. 'label' => 'When adding from a feed',
  25. 'settings' => array(
  26. 'post_type' => array(
  27. 'type' => 'custom',
  28. 'callback' => 'tp_render_post_type_select',
  29. 'label' => 'Destination post type',
  30. 'description' => 'Will products be posts, pages or another kind of custom posts',
  31. 'options' => get_post_types( array( 'public' => true ), 'objects' ),
  32. 'default' => 'post'
  33. ),
  34. 'post_status' => array(
  35. 'type' => 'select',
  36. 'label' => 'Default post status',
  37. 'description' => 'The state of the post immediately after adding it from a feed',
  38. 'options' => array(
  39. 'draft' => 'Draft',
  40. 'publish' => 'Published'
  41. ),
  42. 'default' => 'draft'
  43. ),
  44. 'update_batch_size' => array(
  45. 'type' => 'text',
  46. 'label' => 'Update batch size',
  47. 'description' => 'The maximum number of products to update in a single step',
  48. 'default' => 50
  49. ),
  50. 'trash_expired' => array(
  51. 'type' => 'checkbox',
  52. 'label' => 'Trash expired products',
  53. 'description' => 'Check this to send posts containing products which are no longer available to trash when updating all products',
  54. 'default' => false
  55. )
  56. )
  57. ),
  58. 'fields' => array(
  59. 'label' => 'Metadata to get when mass-importing',
  60. 'settings' => array(
  61. 'fields' => array(
  62. 'type' => 'custom',
  63. 'label' => 'Product fields',
  64. 'callback' => 'tp_render_options_fields_fields'
  65. ),
  66. 'other_fields' => array(
  67. 'type' => 'custom',
  68. 'callback' => 'tp_render_options_fields_other_fields',
  69. 'label' => 'Other custom fields'
  70. )
  71. )
  72. ),
  73. 'templates' => array(
  74. 'label' => 'Inserting products into post',
  75. 'settings' => array(
  76. 'template' => array(
  77. 'type' => 'textarea',
  78. 'label' => 'User-defined output template',
  79. 'default' => '<div class="tp-product-info">
  80. <div class="tp-product-thumbnail">
  81. <a href="%aff_link%">
  82. <img src="%image_url%" />
  83. </a>
  84. </div>
  85. <div class="tp-product-meta">
  86. <span class="tp-product-brand">%brand%</span>
  87. <span class="tp-product-title">%title%</span>
  88. <span class="tp-product-price">%price%</span>
  89. </div>
  90. </div>'
  91. )
  92. )
  93. )
  94. );
  95. // register_setting( 'tp-options-group', 'tp_options_post_type' );
  96. // register_setting( 'tp-options-group', 'tp_options_fields' );
  97. foreach ( $settings as $section_id => $section ) {
  98. register_setting( 'tp-options-group', "tp_options_{$section_id}" );
  99. add_settings_section( "tp_options_{$section_id}", __( $section['label'], 'tppi' ), 'tp_section_callback', 'tp-options' );
  100. $values = get_option( "tp_options_{$section_id}" );
  101. $defaults = ( $values === false );
  102. if ( $values === false ) {
  103. $values = array();
  104. }
  105. foreach ( $section['settings'] as $setting_id => $setting ) {
  106. $setting['id'] = "tp_options_{$section_id}_{$setting_id}";
  107. $setting['name'] = "tp_options_{$section_id}[{$setting_id}]";
  108. $setting['value'] = isset( $values[$setting_id] ) ? $values[$setting_id] : ( $values[$setting_id] = isset( $setting['default'] ) ? $setting['default'] : null );
  109. add_settings_field( "tp_options_{$section_id}_{$setting_id}", __( $setting['label'], 'tppi' ), 'tp_render_field', 'tp-options', "tp_options_{$section_id}", $setting );
  110. }
  111. if ( $defaults )
  112. update_option( "tp_options_{$section_id}", $values );
  113. }
  114. }
  115. function tp_section_callback() {}
  116. function tp_render_field( $setting ) {
  117. extract( $setting );
  118. $type = empty( $type ) ? 'text' : $type;
  119. $class = isset( $class ) ? $class : array();
  120. $class = is_array( $class ) ? $class : array ( $class );
  121. $output = '';
  122. switch ( $type ) {
  123. case 'select':
  124. if ( isset( $options ) && is_array( $options ) ) {
  125. foreach ( $options as $key => $option ) {
  126. $selected = $key == $value ? 'selected="selected"' : '';
  127. $output .= "<option value='".esc_attr($key)."' {$selected}>" . esc_attr( $option ) . "</option>";
  128. }
  129. }
  130. $class = implode( ' ', $class );
  131. $output = "<select name='" . esc_attr( $name ) . "' id='" . esc_attr( $id ) . "' class='".esc_attr( $class ) . "'>$output</select>";
  132. break;
  133. case 'text':
  134. case 'password':
  135. $class = array_merge( array ( 'regular-text' ), $class );
  136. $class = implode( ' ', $class );
  137. $output = "<input type='" . esc_attr( $type ) . "' class='" . esc_attr( $class ) . "' name='" . esc_attr( $name ) . "' id='" . esc_attr( $id ) . "' value='" . esc_attr( $value ) . "' />";
  138. break;
  139. case 'textarea':
  140. $class = array_merge( array ( 'large-text', 'code' ), $class );
  141. $class = implode( ' ', $class );
  142. $output = "<textarea rows='10' cols='50' class='" . esc_attr( $class ) . "' name='" . esc_attr( $name ) . "' id='" . esc_attr( $id ) . "'>" . esc_attr( $value ) . "</textarea>";
  143. break;
  144. case 'checkbox':
  145. $class = implode( ' ', $class );
  146. $output = "<input type='checkbox' class='" . esc_attr( $class ) . "' name='" . esc_attr( $name ) . "' id='" . esc_attr( $id ) . "' ".((boolean)($value) ? "checked='checked'" : "")." />";
  147. break;
  148. case 'custom':
  149. if ( isset( $callback ) ) {
  150. $output .= call_user_func( $callback, $setting );
  151. }
  152. break;
  153. default:
  154. trigger_error( 'Invalid setting type', E_WARNING );
  155. }
  156. if ( isset( $description ) )
  157. $output .= " <span class='description'>" . esc_attr( $description ) . "</span>";
  158. echo $output;
  159. }
  160. function tp_render_post_type_select( $setting ) {
  161. extract( $setting );
  162. $type = empty( $type ) ? 'text' : $type;
  163. $class = isset( $class ) ? $class : array();
  164. $class = is_array( $class ) ? $class : array ( $class );
  165. if ( isset( $options ) && is_array( $options ) ) {
  166. foreach ( $options as $option ) {
  167. $selected = $option->name == $value ? 'selected="selected"' : '';
  168. $output .= "<option value='{$option->name}' {$selected}>{$option->label}</option>";
  169. }
  170. }
  171. $class = implode( ' ', $class );
  172. $output = "<select name='$name' id='$id' class='$class'>$output</select>";
  173. return $output;
  174. }
  175. $field_names = array(
  176. 'brand',
  177. 'price',
  178. 'product-store-id',
  179. 'category',
  180. 'created-at',
  181. 'subcategory',
  182. 'delta',
  183. 'title',
  184. 'campaign-id',
  185. 'updated-at',
  186. 'url',
  187. 'id',
  188. 'other-data',
  189. 'update-type',
  190. 'caption',
  191. 'clicks',
  192. 'promoted',
  193. 'unique-code',
  194. 'description',
  195. 'prid',
  196. 'active',
  197. 'image_url',
  198. 'image_urls',
  199. 'aff_link'
  200. );
  201. function tp_render_options_fields_fields( $setting ) {
  202. extract($setting);
  203. if ( ! $value )
  204. $value = array(
  205. 'brand' => array(
  206. 'type' => 'text',
  207. 'label' => 'Product brand name',
  208. 'value' => '%brand%'
  209. ),
  210. 'title' => array(
  211. 'type' => 'text',
  212. 'label' => 'Product name',
  213. 'value' => '%title%'
  214. ),
  215. 'description' => array(
  216. 'type' => 'textarea',
  217. 'label' => 'Product description',
  218. 'value' => '%description%'
  219. ),
  220. 'price' => array(
  221. 'type' => 'text',
  222. 'label' => 'Product price',
  223. 'value' => '%price%'
  224. ),
  225. 'aff_link' => array(
  226. 'type' => 'text',
  227. 'label' => 'Affiliate link',
  228. 'value' => '%aff_link%'
  229. ),
  230. );
  231. foreach ( $value as $kk => $vv ) {
  232. $value[$kk]['selectable_type'] = true;
  233. }
  234. ?>
  235. <p class="description"><?php printf( __( 'See <a href="#" id="%1$s">help section</a> above for details', 'tppi' ), 'tp_options_fields_fields_help' ); ?></p>
  236. <script type="text/javascript"><!--//<![CDATA[
  237. var tp_options_fields_fields = <?php echo json_encode( $value ); ?>,
  238. tp_options_fields_fields_name = '<?php echo $name?>';
  239. //]]--></script><a name="tp_options_fields_fields" id="tp_options_fields_fields_anchor"></a>
  240. <?php
  241. //*
  242. echo "<table id='tp_fields_fields' class='fields'>";
  243. echo "<tr class='head'><th></th><th scope='column'>" . __( 'Key', 'tppi' ) . "</th><th scope='column'>" . __( 'Value', 'tppi' ) . "</th></tr>";
  244. foreach ( $value as $i => $p ) {
  245. echo "<tr class='product_field tp_$i'><th scope='row'>" . $p['label'] . "</th><td class='product_field_key'><em>" . esc_attr( $i ) . "</em></td><td class='product_field_value'>" . esc_attr( $p['value'] );
  246. foreach ( $p as $j => $v ) {
  247. echo "<input type='hidden' name='{$name}[{$i}][{$j}]' id='{$id}_{$i}_{$j}' value='$v' />";
  248. }
  249. echo "</td></tr>";
  250. }
  251. echo "</table>";
  252. //*/
  253. }
  254. function tp_render_options_fields_other_fields( $setting ) {
  255. extract($setting);
  256. if ( ! $value )
  257. $value = array();
  258. ?>
  259. <p class="description"><?php printf( __( 'See <a href="#" id="%1$s">help section</a> above for details', 'tppi' ), 'tp_options_fields_other_fields_help' ); ?></p>
  260. <script type="text/javascript"><!--//<![CDATA[
  261. var tp_options_fields_other_fields = <?php echo json_encode( $value ); ?>,
  262. tp_options_fields_other_fields_name = '<?php echo $name?>';
  263. //]]--></script><a name="tp_options_fields_other_fields" id="tp_options_fields_other_fields_anchor"></a>
  264. <?php
  265. //*
  266. echo "<table id='tp_fields_other_fields' class='fields'>";
  267. echo "<tr class='head'><th></th><th scope='column'>" . __( 'Key', 'tppi' ) . "</th><th scope='column'>" . __( 'Value', 'tppi' ) . "</th></tr>";
  268. foreach ( $value as $i => $p ) {
  269. echo "<tr class='product_field tp_$i'><th scope='row'>" . $p['label'] . "</th><td class='product_field_key'><em>" . esc_attr( $i ) . "</em></td><td class='product_field_value'>" . esc_attr( $p['value'] );
  270. foreach ( $p as $j => $v ) {
  271. echo "<input type='hidden' name='{$name}[{$i}][{$j}]' id='{$id}_{$i}_{$j}' value='$v' />";
  272. }
  273. echo "</td></tr>";
  274. }
  275. echo "</table>";
  276. //*/
  277. }
  278. function tp_plugin_settings() {
  279. include_once 'api.php';
  280. if ( ! current_user_can( 'manage_options' ) ) {
  281. wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
  282. }
  283. $errors = tp_verify_connection();
  284. ?>
  285. <style type="text/css">
  286. table.tp-field-table th {
  287. font-weight: bold;
  288. }
  289. table.tp-field-table textarea {
  290. height: 100px;
  291. }
  292. </style>
  293. <div class="wrap">
  294. <?php if ( function_exists( 'screen_icon' ) ) screen_icon(); ?><h2>2Performant Product Importer</h2>
  295. <?php if ( ! empty( $errors ) ) : ?>
  296. <div id="setting-error-options_error" class="error settings-error">
  297. <?php foreach ( $errors as $e ) : ?>
  298. <p><?php _e( $e, 'tppi' ); ?></p>
  299. <?php endforeach; ?>
  300. </div>
  301. <?php endif; ?>
  302. <form name="form" action="options.php" method="post">
  303. <?php settings_fields( 'tp-options-group' ); ?>
  304. <?php do_settings_sections('tp-options'); ?>
  305. <p class="submit">
  306. <input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
  307. </p>
  308. </form>
  309. </div><?php
  310. }
  311. function tp_plugin_settings_help( $contextual_help, $screen_id, $screen ) {
  312. global $tp_plugin_settings_page;
  313. global $field_names;
  314. if ( $screen_id != $tp_plugin_settings_page )
  315. return $contextual_help;
  316. $desc = sprintf( __( 'The new %1$s from %2$s: %3$s', 'tppi' ), '%title%', '%brand%', '%caption%' );
  317. $result = sprintf( __( 'The new %1$s from %2$s: %3$s', 'tppi' ), 'men\'s sneakers', 'Nike', 'Now on sale!' );
  318. ob_start();
  319. ?>
  320. <p><?php _e( 'This is your command center for the 2Performant Product Importer.' ); ?></p>
  321. <h3><?php _e( 'Connection settings', 'tppi' ); ?></h3>
  322. <p><strong><?php _e( 'Network API URL', 'tppi' ); ?></strong> - <?php printf( __( 'If you\'re unsure, ask your affiliate network operator for this. It\'s usually %1$s, where %2$s is your network.', 'tppi' ), '<code>api.network.com</code>', '<code>network.com</code>' ); ?></p>
  323. <p><strong><?php _e( 'Username', 'tppi' ); ?> &amp; <?php _e( 'Password', 'tppi' ); ?></strong> - <?php _e( 'Self explanatory.', 'tppi' ); ?></p>
  324. <h3><?php _e( 'When adding from a feed', 'tppi' ); ?></h3>
  325. <p><strong><?php _e( 'Destination post type', 'tppi' ); ?></strong> - <?php printf( __( 'Wordpress supports <a href="%1$s" target="_blank">Custom Post Types</a>, which you can use to create your own custom post type (e.g. %2$s) to import and show off products. Or you can opt for the built-in post types.', 'tppi' ), 'http://codex.wordpress.org/Custom_Post_Types', '<code>product</code>' ); ?></p>
  326. <p><strong><?php _e( 'Default post status', 'tppi' ); ?></strong> - <?php _e( 'When you import a product from a feed you can have the destination post wait for you to publish it or you can immediately send it to your target audience.', 'tppi' ); ?></p>
  327. <p><strong><?php _e( 'Update batch size', 'tppi' ); ?></strong> - <?php _e( 'When you are updating the products, loading too many at once could fill up your server memory. Therefore, you can select how many products should be updated at once by customizing this setting.', 'tppi' ); ?></p>
  328. <h3><?php _e( 'Metadata to get when mass-importing', 'tppi' ); ?></h3>
  329. <p><strong><?php _e( 'Product fields', 'tppi' ); ?></strong> - <?php printf( __('These are the fields you can use in your custom theme. The syntax is %1$s, where %2$s is the WordPress field name and %3$s is the value of the product info field from 2Performant.', 'tppi'), "<code>tp_the_product_field( 'wp-key' )</code>", '<code>wp-key</code>', '<code>%product-key%</code>' ); ?></p>
  330. <p><strong><?php _e( 'Other custom fields', 'tppi' ); ?></strong> - <?php printf( __( '<a href="%1$s" target="_blank">Custom fields</a> that can originally be set by other plugins, but whose values you want to override. For example you could set %2$s\'s %3$s custom field to something like %4$s in order to get %5$s.', 'tppi'), 'http://codex.wordpress.org/Custom_Fields', "<a href='http://wordpress.org/extend/plugins/all-in-one-seo-pack/' target='_blank'>All in One SEO Pack</a>", '<code>_aioseop_description</code>', '<code>'.$desc.'</code>', '<em>'.$result.'</em>' ); ?></p>
  331. <p><?php echo sprintf( __('Possible product info fields are: %1$s.', 'tppi'), '<code>%' . implode( '%</code>, <code>%', $field_names ) . '%</code>' ); ?></p>
  332. <h3><?php _e( 'Inserting products into post', 'tppi' ); ?></h3>
  333. <p><strong><?php _e( 'User-defined output template', 'tppi' ); ?></strong> - <?php printf( __('This defines how the products inserted into the post via the button on the <abbr title="What You See Is What You Get">WYSIWYG</abbr> (visual) editor will look like. You can use the product info fields mentioned above using the %1$s syntax described above.', 'tppi'), '<code>%product-info-field%</code>' ); ?></p>
  334. <p><strong><?php _e( 'For more information' ); ?></strong></p>
  335. <?php
  336. $contextual_help = ob_get_contents() . $contextual_help;
  337. ob_end_clean();
  338. return $contextual_help;
  339. }
  340. endif;
  341. function tp_get_option( $group, $name, $default = false ) {
  342. $option = get_option( sprintf( 'tp_options_%s', $group ), array( $name => $default ) );
  343. if( !is_array($option) )
  344. $option = array( $name => $default );
  345. if( !isset($option[$name]) )
  346. $option[$name] = $default;
  347. return $option[$name];
  348. }
  349. ?>