Automation
AutomationWP-Cron

WP-Cron

The following action hooks are provided, to be invoked from within WP-Cron:

gatographql__execute_query

This hook receives the following parameters (in this same order):

#Mandatory?ParamDescription
1$queryThe GraphQL query to execute
2$variablesGraphQL variables
3$operationNameThe operation name to execute
4$executeAsUserThe user to log-in to execute the query
5$schemaConfigurationIDOrSlugThe schema configuration ID (as an int) or slug (as a string) to apply when executing the query. Passing null will use the default value, and passing -1 means "use no schema configuration"

The $executeAsUser parameter is needed if the query requires the user to be logged-in, such as when executing a mutation:

  • If provided, the user with given ID (as an int) or username (as a string) will be logged-in right before executing the GraphQL query, and logged-out immediately afterwards.
  • If not provided, no user will be logged-in when executing the query.

gatographql__execute_persisted_query

This hook receives the following parameters (in this same order):

#Mandatory?ParamDescription
1$persistedQueryIDOrSlugThe Persisted Query ID (as an int) or slug (as a string)
2$variablesGraphQL variables
3$operationNameThe operation name to execute
4$executeAsUserThe user to log-in to execute the query

Notice that the schema configuration to apply is already selected within the persisted query.

Examples

The following WP-Cron event executes hook gatographql__execute_persisted_query to send a daily email indicating the number of new comments added to the site:

  • In the last 24 hs
  • In the last 1 year
  • Since the beginning of the month
  • Since the beginning of the year

We create a Persisted Query with slug "daily-stats-by-email-number-of-comments" and content:

query CountComments {
  DATE_ISO8601: _env(name: DATE_ISO8601) @remove
 
  timeToday: _time
  dateToday: _date(format: $__DATE_ISO8601, timestamp: $__timeToday)
  
  timeYesterday: _intSubtract(subtract: 86400, from: $__timeToday)
  dateYesterday: _date(format: $__DATE_ISO8601, timestamp: $__timeYesterday)
  
  time1YearAgo: _intSubtract(subtract: 31536000, from: $__timeToday)
  date1YearAgo: _date(format: $__DATE_ISO8601, timestamp: $__time1YearAgo)
 
  timeBegOfThisMonth: _makeTime(hour: 0, minute: 0, second: 0, day: 1)
  dateBegOfThisMonth: _date(format: $__DATE_ISO8601, timestamp: $__timeBegOfThisMonth)
 
  timeBegOfThisYear: _makeTime(hour: 0, minute: 0, second: 0, month: 1, day: 1)
  dateBegOfThisYear: _date(format: $__DATE_ISO8601, timestamp: $__timeBegOfThisYear)
  
  commentsAddedInLast24Hs: commentCount(filter: { dateQuery: { after: $__dateYesterday } } )
    @export(as: "commentsAddedInLast24Hs")
  commentsAddedInLast1Year: commentCount(filter: { dateQuery: { after: $__date1YearAgo } } )
    @export(as: "commentsAddedInLast1Year")
  commentsAddedSinceBegOfThisMonth: commentCount(filter: { dateQuery: { after: $__dateBegOfThisMonth } } )
    @export(as: "commentsAddedSinceBegOfThisMonth")
  commentsAddedSinceBegOfThisYear: commentCount(filter: { dateQuery: { after: $__dateBegOfThisYear } } )
    @export(as: "commentsAddedSinceBegOfThisYear")
}
 
query CreateEmailMessage @depends(on: "CountComments") {
  emailMessageTemplate: _strConvertMarkdownToHTML(
    text: """
 
This is the number of comments added to the site:
 
| Period | # Comments added |
| --- | --- |
| **In the last 24 hs**: | {$commentsAddedInLast24Hs} |
| **In the last 365 days**: | {$commentsAddedInLast1Year} |
| **Since begginning of this month**: | {$commentsAddedSinceBegOfThisMonth} |
| **Since begginning of this year**: | {$commentsAddedSinceBegOfThisYear} |
 
    """
  )
  emailMessage: _strReplaceMultiple(
    search: [
      "{$commentsAddedInLast24Hs}",
      "{$commentsAddedInLast1Year}",
      "{$commentsAddedSinceBegOfThisMonth}",
      "{$commentsAddedSinceBegOfThisYear}"
    ],
    replaceWith: [
      $commentsAddedInLast24Hs,
      $commentsAddedInLast1Year,
      $commentsAddedSinceBegOfThisMonth,
      $commentsAddedSinceBegOfThisYear
    ],
    in: $__emailMessageTemplate
  )
    @export(as: "emailMessage")
}
 
mutation SendDailyStatsByEmailNumberOfComments(
  $to: [String!]!
)
  @depends(on: "CreateEmailMessage")
{
  _sendEmail(
    input: {
      to: $to
      subject: "Daily stats: Number of new comments"
      messageAs: {
        html: $emailMessage
      }
    }
  ) {
    status
  }
}

Then, we schedule the WP-Cron event, either via PHP:

\wp_schedule_event(
  time(),
  'daily',
  'gatographql__execute_persisted_query',
  [
    'daily-stats-by-email-number-of-comments',
    [
      'to' => ['admin@yoursite.com']
    ],
    'SendDailyStatsByEmailNumberOfComments',
    1 // This is the admin user's ID
  ]
);

Or via the WP-Crontrol plugin:

  • Event type: Standard cron event
  • Hook name: gatographql__execute_persisted_query
  • Arguments: ["daily-stats-by-email-number-of-comments",{"to":["admin@yoursite.com"]},"SendDailyStatsByEmailNumberOfComments",1]
  • Recurrence: Once Daily

New entry in WP-Crontrol