forked from lightward/mechanic-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadvanced-scheduled-section-publishing.json
29 lines (29 loc) · 6.89 KB
/
advanced-scheduled-section-publishing.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
{
"docs": "This advanced task allows you to schedule section publishing and unpublishing for your store. To use this task, enter a datetime to publish, a theme ID, and template names paired with one or more section IDs to publish or unpublish. If the configured datetime matches the current task run time at a scheduled 10 minute interval, then it will publish and/or unpublish the theme sections as configured.\n\n__Important Notes:__\n- The datetime to publish must be in the format of \"YYYY-MM-DD HH:MM\" with a 24 hour clock time having minutes that are a multiple of 10, since that is the smallest scheduler interval available.\n- Template names should be entered without the _.json_ suffix (e.g. index, page.contact).\n- Template names can appear in both the publish and unpublish keyval configurations, but they may only appear once in each since they are the left-hand keys.\n- Section IDs should be entered in the right-hand value fields, paired with their respective template.\n- Multiple section IDs being (un)published for the same template must be entered on multiple lines of the same entry field.\n\n_Example configuration_\n- Datetime to publish: __2022-07-31 13:30__\n- Theme ID: __1234567890__\n- Template names and section IDs to publish:\n - __index__\n - _collage_\n - _123456abcdef_\n- Template names and section IDs to unpublish:\n - __index__\n - _234561bcdefa_",
"halt_action_run_sequence_on_error": false,
"name": "Advanced: Scheduled section publishing",
"online_store_javascript": null,
"options": {
"datetime_to_publish__required": null,
"theme_id__number_required": null,
"template_names_and_section_ids_to_publish__keyval_multiline": {},
"template_names_and_section_ids_to_unpublish__keyval_multiline": {}
},
"order_status_javascript": null,
"perform_action_runs_in_sequence": false,
"preview_event_definitions": [],
"script": "{% assign datetime_to_publish = options.datetime_to_publish__required %}\n{% assign theme_id = options.theme_id__number_required %}\n{% assign template_names_and_section_ids_to_publish = options.template_names_and_section_ids_to_publish__keyval_multiline %}\n{% assign template_names_and_section_ids_to_unpublish = options.template_names_and_section_ids_to_unpublish__keyval_multiline %}\n\n{% assign template_names\n = template_names_and_section_ids_to_publish\n | keys\n | default: array\n%}\n{% assign template_names\n = template_names_and_section_ids_to_unpublish\n | keys\n | default: array\n | concat: template_names\n | uniq\n%}\n\n{% if template_names == blank %}\n {% error \"Configure at least one template name and section ID to publish or unpublish.\" %}\n {% break %}\n{% endif %}\n\n{% if event.topic contains \"mechanic/scheduler/\" or event.topic == \"mechanic/user/trigger\" %}\n {% if event.topic contains \"mechanic/scheduler/\" %}\n {% assign datetime = \"now\" | date: \"%Y-%m-%d %H:%M\" %}\n\n {% unless datetime_to_publish == datetime or event.preview %}\n {% log\n message: \"Configured datetime to publish does not match run date; ending this task run.\",\n run_date: datetime,\n datetime_to_publish: datetime_to_publish\n %}\n {% break %}\n {% endunless %}\n {% endif %}\n\n {% for template_name in template_names %}\n {% if template_name == blank %}\n {% continue %}\n {% endif %}\n\n {% assign section_ids_to_publish = template_names_and_section_ids_to_publish[template_name] | split: newline %}\n {% assign section_ids_to_unpublish = template_names_and_section_ids_to_unpublish[template_name] | split: newline %}\n\n {% log\n template_name: template_name,\n section_ids_to_publish: section_ids_to_publish,\n section_ids_to_unpublish: section_ids_to_unpublish\n %}\n\n {% capture template_path %}templates/{{ template_name }}.json{% endcapture %}\n\n {% action %}\n {\n \"type\": \"shopify\",\n \"options\": [\n \"get\",\n \"/admin/themes/{{ theme_id }}/assets.json?asset[key]={{ template_path }}\"\n ],\n \"meta\": {\n \"template_path\": {{ template_path | json }},\n \"section_ids_to_publish\": {{ section_ids_to_publish | json }},\n \"section_ids_to_unpublish\": {{ section_ids_to_unpublish | json }}\n }\n }\n {% endaction %}\n {% endfor %}\n\n{% elsif event.topic == \"mechanic/actions/perform\" %}\n {% comment %}\n -- NOTE: Shopify will throw a 404 error on the GET if the asset is not accessible, halting that action, so no error checking is needed here\n {% endcomment %}\n\n {% assign template_path = action.meta.template_path %}\n {% assign section_ids_to_publish = action.meta.section_ids_to_publish %}\n {% assign section_ids_to_unpublish = action.meta.section_ids_to_unpublish %}\n\n {% unless template_path or event.preview %}\n {% comment %}\n -- stop processing if a meta value is not present (i.e. theme asset update actions)\n {% endcomment %}\n {% break %}\n {% endunless %}\n\n {% assign asset = action.run.result.asset %}\n\n {% if event.preview %}\n {% assign asset = hash %}\n {% assign asset[\"value\"] = hash | json %}\n {% endif %}\n\n {% assign asset_value = asset.value | parse_json %}\n {% assign section_names = asset_value.sections | keys %}\n\n {% assign matched_sections_to_publish = array %}\n {% assign matched_sections_to_unpublish = array %}\n\n {% for section_name in section_names %}\n {% if section_ids_to_publish contains section_name %}\n {% assign matched_sections_to_publish = matched_sections_to_publish | push: section_name %}\n {% assign asset_value[\"sections\"][section_name][\"disabled\"] = false %}\n\n {% elsif section_ids_to_unpublish contains section_name %}\n {% assign matched_sections_to_unpublish = matched_sections_to_unpublish | push: section_name %}\n {% assign asset_value[\"sections\"][section_name][\"disabled\"] = true %}\n {% endif %}\n {% endfor %}\n\n {% log\n template_path: template_path,\n matched_sections_to_publish: matched_sections_to_publish,\n matched_sections_to_unpublish: matched_sections_to_unpublish\n %}\n\n {% unless matched_sections_to_publish != blank or matched_sections_to_unpublish != blank or event.preview %}\n {% log \"No matched sections to publish or unpublish; no update will be made.\" %}\n {% break %}\n {% endunless %}\n\n {% action \"shopify\" %}\n [\n \"put\",\n \"/admin/themes/{{ theme_id }}/assets.json\",\n {\n \"asset\": {\n \"key\": {{ template_path | json }},\n \"value\": {{ asset_value | json | json }}\n }\n }\n ]\n {% endaction %}\n{% endif %}\n",
"subscriptions": [
"mechanic/scheduler/10min",
"mechanic/user/trigger",
"mechanic/actions/perform"
],
"subscriptions_template": "mechanic/scheduler/10min\nmechanic/user/trigger\nmechanic/actions/perform",
"tags": [
"Advanced",
"Publish",
"Schedule",
"Sections",
"Unpublish"
]
}